New Features of Windows Vista Development (2)-What is your file?

Source: Internet
Author: User

New Features of Windows Vista Development (2)-What is your file?

In fact, this is not a new feature. At best, it is a best practice for Vista development. But for various reasons, this problem occurs in almost all software products that require Vista compatibility upgrades, the reason is of course there are historical factors, but in the final analysis, our development is not standardized.

Let's get a little farther away. Is innovation or specification required for software development? I have seen many students who have just graduated from school think of innovation at will and cheat in order to show off. When I first joined SSW, the biggest inadaptability to the company was the wide variety of detailed specifications. Once heard of this, "Being good is a habit." When you learn from others' experiences and use these experiences unconsciously every day, then you will find that the efficiency of doing things will be greatly improved, and everything is so natural. Of course, the process of learning norms is painful, but it is always rewarding. Back on the software development topic, I have always felt that the best way to learn programming is to write and compile and run others' code. I remember that when I went to school, I learned the C language. When I compiled all the code by myself, I found that programming was actually so simple. This habit has always followed me to learn any new technologies such as PHP, SQL, Java, and. net.

Sorry, it's a little too far. One major improvement in Vista is the extensive application of UAC. When we cannot use administrator permissions, we find that many simple operations have become so complicated. For example, we are used to storing application configuration files in folders such as/program files/Application name. In this way, you will find that normal users/administrators cannot write data without elevation of permissions, resulting in application crash. Of course, this is not a problem if your program has not added UAC manifest.

File System Virtual redirection File System Visualization

We can find the reason from the following experiment:

First, use the editplus tool to create a text file and put it in the/program files/directory:


You will find that the program will not encounter any problems, the file can be stored normally, but when you use the File Manager to open the/program files/directory, the file does not exist.

The actual file is actually "redirected" to the current user's appdata directory:

In fact, this is a backward compatible feature in Vista: File System visualization ). Its function is to redirect access to protected content from applications that are not compatible with Vista to locations where users have access permissions, that is,/users/username/appdata/local/virualstore /. In this folder, you can find directories such as program files and windows. By default, normal users do not have permission to access these directories, so the corresponding access is directed here.

When UAC manifest is added to your program, the compatibility protection of Vista is also lost. At this time, you must use the correct path in the program to put the configuration file, log files are written to your own appdata. We can see from the following notepad demonstration that, because notepad has upgraded Vista, the "no permission" error message will be returned when accessing the/program files/directory:

If we compare the following editplus and notepad EXE files, we can see the difference:

The following code is added to the manifest file of the notepad EXE file:

 

<Trustinfo xmlns = "urn: Schemas-Microsoft-com: ASM. V3">
<Security>
<Requestedprivileges>
<Requestedexecutionlevel level = "asinvoker" UIAccess = "false"/>
</Requestedprivileges>
</Security>
</Trustinfo>

 

The above code tells the UAC on Vista that notepad now uses the asinvoker permission, which is the same as the caller's permission. I mentioned in the UAC article, the user on Vista is a standard user by default and has no permission to access/program files/, which is why notepad fails to be saved.

Multi-user application environment

Since windows began to support multiple users, the concept of User Directories has been in place, but we have never paid enough attention to this issue in software development, many applications write some user configuration content to the/program files/Application name directory. Many people do not understand this problem. The contents should be stored in the/program files/Application name directory and stored in the/users/username/appdata directory. In fact, we can distinguish it as follows:

/Program files/Application name: saves the application configurations that are fixed during installation. These configurations should not be changed by individual users, such as fixed gateways, server addresses, the dynamic link library list and program version information of the application.

/Users/username/appdata: stores content that can be modified by the user, and this configuration file should be dynamically created by the program during the first running of the application. For example, the window location, size, Last login time, user name, cookie, and so on.

Why cannot I create this file by the installer? The main purpose is to support unattended deployment. In this case, the installer does not know the specific situation of individual users, so the file cannot be correctly created.

In fact, the simplest way to create this file is to use the configuration module of applicationblock in. NET Framework, because it will automatically process file read/write and creation; it is completely transparent to the application.

Obtain the correct system path

Another question is how to obtain these directories correctly in the application. In fact, it is very easy to use the environment. getfolderpath () method, we can easily obtain the directories we need:

The above listview uses the following code for filling:

Foreach (system. environment. specialfolder folder in Enum. getvalues (typeof (environment. specialfolder )))
...{
Listviewitem folderitem = new listviewitem (
New String []... {folder. tostring (), environment. getfolderpath (folder )});
Listview1.items. Add (folderitem );
}

 

Here, we traverse the environment. specialfolder Enumeration type and extract all the special paths of the system. Note:

Enviornment. specialfolder. applicationdata: This value will point to the roaming directory in the appdata of the current user. The only difference between this directory and local is that when the roaming function is configured in your ad, the contents of this directory are stored on the server and pre-deployed when the user logs on to another computer. It is generally used in large enterprises to provide a better login environment.

Backward compatible with XP file systems

When talking about this, you must have doubts, because on XP, the path of the user directory is:

/Users and settings/, instead of/users/, may feel that there is a problem. In fact, Vista has already prepared for this.

If you enter the XP path in a vista environment, you can still access the XP directory normally. In fact, Vista uses a technology that has been used for many years on UNIX to achieve this function.

When you type DIR/a, there are many things called <junction>, which are actually "file system links ", vista uses these links to point the XP-style directory to the correct Vista path. To ensure normal access to XP applications. If necessary, you can use the mklink command to create the required link.

Therefore, if the application needs to be backward compatible, it should still use XP-style paths. Of course, I mean if you must specify hard code paths. If possible, try to use environment to obtain the correct path.

Related Resources

CFF explorer manifest viewing tool:
Http://programmerstools.org/node/307

Best practices for correct use of Windows Vista UAC standard user environment:
Developer best practices and guidelines for applications in a least privileged environment:
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnlong/html/accprotvista. asp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.