3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mask
  • 647
  • 2
  • 9
  • 21
  • http://stackoverflow.com/questions/7288444/how-to-give-read-write-permissions-to-a-folder-during-installation-using-net –  May 31 '13 at 04:53
  • Search for run your application with admin rights. http://stackoverflow.com/questions/7666408/how-to-request-administrator-permissions-when-the-program-starts – Furqan Ashraf May 31 '13 at 06:44
  • 1
    @FurqanAshraf This is almost never the correct way to solve the problem. Programs that don't *need* administrator rights should not ask for it, it is much preferable to fix your program to do what ever you need to without needing to elevate (like changing your program to save in the correct location). If you do have to elevate it is *preferred* to still run your program as a lower privilege and [only elevate when you need to do the task that requires administrator privileges](http://stackoverflow.com/questions/573086/how-to-elevate-privileges-only-when-required?rq=1). – Scott Chamberlain May 31 '13 at 14:01

1 Answers1

12

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.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Thanks for the reply. How can i view that log file. I just logged some txt but i couldn't get that savePath . – Mask May 31 '13 at 05:37
  • I got now , i had to create that directory first. I used CommonApplicationData. – Mask May 31 '13 at 06:02