Click here to Skip to main content
14,643,428 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have written a windows forms application. Before I run the form, I write the startup time to a log textfile. This is done in the Program.Main() method.
When I run the program, everything works perfect.

But now I want to start the application on windows logon. So I have written the path+exe-name of the application in LM/Software/Microsoft/Windows/Currentversion/Run. But when I log on to windows, an UnauthorizedAccessException is thrown with the following stack trace:

<small>System.IO.__Error.WinIOError(Int32, System.String)
System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean)
System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)
System.IO.StreamWriter.CreateFile(System.String, Boolean)
System.IO.StreamWriter..ctor(System.String, Boolean, System.Text.Encoding, Int32)
System.IO.StreamWriter..ctor(System.String, Boolean</small>


Can anyone tell me what I am missing here?
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Looks like you don't have access to the file using this login.
Make sure this login has access to the directory path.
   
v2
Comments
Wannes Geysen 30-Sep-11 14:51pm
   
I use my own login. And when I am logged in and run the application then, there is no exception
Rate this:
Please Sign up or sign in to vote.

Solution 2

The registry key you mentioned (HKLM/Software/Microsoft/Windows/CurrentVersion/Run) starts applications when windows is started, so at that time no user has logged on yet and your application does not have the right credentials to perform the write action you want.

If you want the program to start when a specific user logs onto Windows use the registry key HKCU/Software/Microsoft/Windows/CurrentVersion/Run.

You could use the All Users Startup folder when you want the application to start for all users who logon to a machine.
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 3

Does your app open or create a file using relative path?
If so, the cause of the problem is a difference of current directory.

If you run the app yourself, the current directory is the directory where the app is located.

If you use either HKLM\...\Run or HKCU\...\Run the current directory is the root of your login account.

The solution: set a proper current directory before accessing any file or always use absolute path.
   
Comments
Wannes Geysen 3-Oct-11 4:39am
   
thanks, this solves the mystery. If the program started automatically, the directory was c:\Windows\System32 instead of the directory of the executing assembly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100