I designed a small program based on the original program, which you can see below
private void RegisterAtStartupButton_Click(object sender, EventArgs e)
{
var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
var value = key.GetValue(Application.ProductName, false)?.ToString();
if (value == null)
{
key.SetValue(Application.ProductName, Application.ExecutablePath);
}
var path = Path.Combine(Directory.GetCurrentDirectory(), "Memory.txt");
var text = File.Exists(path)
? File.ReadAllText(path)
: "1";
MessageBox.Show(text);
File.WriteAllText(path, (Convert.ToInt32(text) + 1).ToString());
}
This program counts the number of executions of this method and saves it in a text file
Also, register the program in the Windows startup to run it after restarting Windows and logging in to the same user account.
By restarting Windows and logging in to the same account, the program will run automatically, now if we click on the button that invokes the function, the following error will be displayed
System.UnauthorizedAccessException: Access to the path 'C:\WINDOWS\system32\Memory.txt' is denied.
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, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost)
at System.IO.File.WriteAllText(String path, String contents)
at StartupCounter.MainForm.MainForm_Load(Object sender, EventArgs e) in C:\Users\User\Source\Repos\StartupCounter\StartupCounter\MainForm.cs:line 23
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I am betting when it works you are running the program the first time it is from the folder where Memory.txt is. That way when you do GetCurrentDirectory it finds the file.
However, when you restart and run in the startup case, the current directory is the systems directory.
The exception is not helpful, but I guess you don't have permission to read from there in this context.
So to fix this you need to not use GetCurrentDirectory and instead use the full path.
Or even better, store Memory.txt in the application data specific location, see Where is the correct place to store my application specific data?