Mask
Mask

Reputation: 657

File access denied while accessing from program files

I have a small Winform aplication developed in VS 2010 and C#. And created setup also.

I have put application config file in the application folder path for saving user credentials. After the installation application can't access the config file because I given program installation default path as follows:

[ProgramFilesFolder][Manufacturer][ProductName]

So I need to keep my user credential somewhere else permanently. I need both read and write permissions. Also I need to log the application exceptions.

Upvotes: 3

Views: 4569

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

The place you are supposed to save user data is in one of the following Environment.SpecialFolder locations

  • ApplicationData - The directory that serves as a common repository for application-specific data for the current roaming user. A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.
  • LocalApplicationData - The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.
  • CommonApplicationData - The directory that serves as a common repository for application-specific data that is used by all users.

You get the path by using Enviorment.GetFolderPath

var savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)),
                            "MyAppName");
//This should return the path  %UserProfile%\Roaming\MyAppName\

For the programs logs I would use CommonApplicationData so the logs from several users are all collected in one location.

Upvotes: 12

Related Questions