views:

707

answers:

1

Hi,

I am unable to create a file under "program files" folder on my Windows 7 64-bit machine in VS 2008 WPF C# code. The error I get on the following code

        myFile = File.Create(logFile);

is the following. (this is the innerException stack trace).

      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.File.Create(String path)
   at MyFirm.MyPricingApp.UI.App.InitializeLogging() in C:\Projects\MyPricingApp\App.xaml.cs:line 150
   at MyFirm.MyPricingApp.UI.App.Application_Startup(Object sender, StartupEventArgs e) in C:\Projects\MyPricingApp\App.xaml.cs:line 38
   at System.Windows.Application.OnStartup(StartupEventArgs e)
   at System.Windows.Application.<.ctor>b__0(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

this seems like it has something to do with UAC in Windows 7, because why else would I get this since my user is already Admin on the machine ?!

Also since the WinIOError has SECURITY_ATTRIBUTES, I am thinking this has something to do with a "new way" security is handled in Windows 7.

I tried to browse to the "Program Files" folder, under which the log folder and file were to be created. I can create the folder by hand, but when i try to create the file, I get the similar "Access Denied" exception.

+2  A: 

Since UAC is enabled, programs do not run with administrative privileges, even though your account is an administrator.
In order to have administrative privileges, you need to right-click the application and click Run as Administrator.

However, your program should not write anything to Program Files after it's installed, except when installing updates.
Instead, you should store your log file in the user's Application Data folder.

SLaks
Hi,I am modifying an existing production App. So changing the folder location from under "Program Files" to some other location is not an option :)
Shiva
Then you'll always need to run as administrator, and your users will hate you for it. You could modify the permissions of the folder in Program Files to grant everyone write access, but that would create a security vulnerability. You _really_ should move the log file.
SLaks
Thanks SLaksYeah I know about security vulnerability! But how do I change the UAC or whatever to have my Program run successfully for now ? I am new to Windows 7. I understand the best practices about not storing files and data under Program Files, but like I said before, changing this in the app right now is not an option. I will review your recommendations and try to get it changed after this code release due this week...
Shiva
You need to change the ACLs for the folder in your install process to grant write access.
SLaks
ok thanks again! i will try that....
Shiva
@SLaksThanks for your suggested fixes! I disabled the User's UAC and it worked!Also, I found out that the log file path is set in app.config to C:\program files\..., so I moved the log file location to Application Data folder and re-enabled the UAC :)
Shiva
If you moved the log file to Application Data, you shouldn't disable UAC.
SLaks
Yeah I re-enabled UAC after moving the log file.
Shiva