1

I created a winform application and then created a setup of that application. this application records some info at Application.StartupPath in a file. unfortunately i got exception when i try to write the file 'Access to is denied'. Please guide me how can i get rid of that..

Thanks

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
makki
  • 2,097
  • 3
  • 23
  • 34
  • 1
    The responsible feature is known as UAC, included and enabled in Windows Vista and 7 by default. It increases the security of your computers, but can cause some headaches for app developers used to the previously insecure practices. I have [more information available here](http://stackoverflow.com/questions/5210575/does-windows-7-have-the-same-problem-as-vista/5210642#5210642). – Cody Gray - on strike Apr 17 '11 at 07:48

2 Answers2

10

You should never assume that the application startup path is writable by anyone besides system administrators, especially on modern Windows systems.

Instead of storing your file there, I'd suggest you use the folder returned by Environment.GetFolderPath(SpecialFolder.ApplicationData). That folder is guaranteed to be writable by the current user.

You can find the Microsoft guidelines about this issue here.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Thanks for your quick response, I agree, but i need to remove that file on uninstall. How could i delete that file on uninstall from AppData folder? – makki Apr 17 '11 at 07:37
  • i found answer to my comment thanks for your guide. http://stackoverflow.com/questions/1117866/how-to-delete-folder-at-uninstallation-in-c-net-application – makki Apr 17 '11 at 07:42
  • 3
    @makki, I don't think you can do that. Your uninstaller would have to enumerate all the user accounts on the system and look up the file in each profile. Also, if [roaming user profiles](http://en.wikipedia.org/wiki/Roaming_user_profile) are enabled, your file will end up being stored in a central location and being deployed on-demand to an unknown number of workstations. One way to handle that situation is not to remove the file during uninstall. The other advantage is that the users will get their settings back if the application is later reinstalled. – Frédéric Hamidi Apr 17 '11 at 07:43
  • 1
    Yup, you're not *supposed* to delete those folders. Users want their settings back when they reinstall the application after a hiatus. That's by-design. – Cody Gray - on strike Apr 17 '11 at 07:49
  • Hmmmm... great Hamidi. You are guru :) – makki Apr 17 '11 at 07:52
6

That's typical - you shouldn't be writing to the "Program" area of your application. You should be writing to a data area of the file system - perhaps the user's settings area, or a common application settings area.

Basically the policy was toughened up (in Vista, I believe) to try to discourage programs from doing exactly what you're currently doing. The best approach isn't to work round it - it's to change where your application stores its settings.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • What are the recommended APIs that wont get you in trouble? Also, what if the configuration should be written by an installer or a separate app? – GregC Apr 17 '11 at 07:30
  • 3
    @GregC, an installer usually can write to the startup folder (after all, it just deployed the application *right there*), so it can use that folder to store configuration files. Of course, those files should only contain shared and read-only data. – Frédéric Hamidi Apr 17 '11 at 07:32