Search code examples
windowsregistryconsole-applicationregedit

Restricted resource access when auto run EXE over HKEY_LOCAL_MACHINE registry key


I have an EXE file, and i whant to launch it every windows start for all users. am adding a registry key on : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
My EXE works fine, but it can't access to any other file (.txt) even the required file is in the same location. it seems like an access denied. why?

My Exe is a .NET application (C# console application) reading a text file content.


Solution

  • Solution from @HansPassant :

    Always use full path names for files, c:\foo\bar\baz.txt. Just baz.txt will not work, the Environment.CurrentDirectory is not set where you hope it is. Use AppData, if necessary use Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) to know where your EXE was installed.