How do you keep Windows UAC (User Account Control) from asking for permission every time your program is run?  How do you avoid access denied errors when your program saves data?  By putting your program’s files in the Microsoft intended locations you can prevent these bothersome prompts and troublesome errors.

Why Do Permission Prompts Appear?

Before Windows Vista, figuring out where to put program files was easy: put all program and data files in c:\Program Files.  Now with Windows Vista or Windows 7, if you aren’t careful about where you put your program’s files, the user may be asked for permission every time he wants to run your program.  He may not even be permitted to make updates to any of his data files.

According to Microsoft, http://www.microsoft.com/windows/windows-7/features/user-account-control.aspx, “User Account Control helps reduce malware infection by helping prevent potentially harmful programs from making changes to your computer.”  Unfortunately, the same UAC could prevent users from running your programs or saving data from them.

Use Microsoft Intended Data Locations

The less desirable way to work around this issue would be to have the user adjust his User Account Control settings so he is not asked for permission when he runs programs that must make changes on his computer.  But this would make his computer more susceptible to malware.

The preferred way to work around this issue is to put the files for your program in Microsoft’s intended location for each type of file.  Microsoft claims that their recommended data locations have been around since long before Windows Vista.  Microsoft just started enforcing those recommendations with Windows Vista.

I don’t know if you’ve been around long enough, but I remember years ago when Windows first provided the new user data directory called c:\Documents and Settings.  I did not use that new directory for my program’s data files. I found it much easier to maintain my program files and program data if I kept them all in one place.  Unfortunately, that is no longer allowed.

Where should you put your files?  That depends on what they are being used for:

Program Files

Program files are files placed on the computer during install and never changed. They belong in c:\Program Files or c:\Program Files (x86). These files are generally shared by all users.

Program Data – Shared

Shared program data files are files placed on the computer when a program is installed and meant to be shared by all users, but may be changed by the program. The difference between shared program data and shared user data (described below) is that the user never directly interacts with shared program data. Shared program data files belong in the common program data directory, c:\ProgramData.

Program Data – Not Shared

Non-shared program data files are those files that are placed on the computer when a program is installed but may be changed, and may be different for individual users. Non-shared program data files belong in the user’s program data directory, c:\Users\{username}\AppData\Roaming.

User Data – Shared

Shared user data is data created by the user, for all users. These files belong in the shared user directory, c:\Users\Public.

User Data – Not Shared

Non-shared user data is data created by the user, for his own use. These data files belong in the user’s data directory, c:\Users\{username}.

How to Reference the Correct Data Locations

Though it may seem messy to write your code to access the correct data locations, all the above data locations may be referenced by using System Environment Variables: http://en.wikipedia.org/wiki/Environment_variable

Conclusion

Following the Microsoft intended data locations means that your program files and program data will end up scattered throughout the computer file system.  However, if your program installer puts all the files in the correct locations, and your program is coded to access its data in those locations, your users should not receive those irritating UAC prompts and errors.

Related posts:

  1. Welcome to This Little Program Went to Market
  2. What is Software Deployment?

About the Author


Comments are closed.

More Do-It-Yourself Java Games

More Do-It-Yourself Java Games: An Introduction to Java Graphics and Event-Driven Programming is the second book of the Do-It-Yourself Java Games series. You'll learn to create windows and dialogs, to add buttons and input fields, to use images and drawings, and to respond to keyboard input and mouse clicks and drags. You'll create 10 more games including several puzzles, a dice game, a word game, and a card game.

This book assumes you either have an understanding of basic Java programming or you have read the first book, Do-It-Yourself Java Games: An Introduction to Java Computer Programming. Read more.

Do-It-Yourself Java Games

Do-It-Yourself Java Games: An Introduction to Java Computer Programming uses a unique "discovery learning" approach to teach computer programming: learn Java programming techniques more by doing Java programming than by reading about them.

Through extensive use of fill-in blanks, with easy one-click access to answers, you will be guided to write complete programs yourself, starting with the first lesson. You'll create puzzle and game programs like Choose An Adventure, Secret Code, Hangman, Crazy Eights, and many more, and discover how, when, and why Java programs are written the way they are. Read more

Step-by-Step Tutorial

Many of the tips, techniques, and tools discussed in this blog are demonstrated in a detailed step-by-step tutorial in the book, This Little Program Went to Market, by Annette Godtland.

The book takes a computer program through the entire process of creating, deploying and distributing a program, then selling and marketing it (or any other product) on the Internet. Read more.