2

I wrote a program which writes to a text file and needs to read from it on every computer startup.

I used the Registry to add my program to the Startup programs,and wrote this simpl code:

private void Form1_Load(object sender, EventArgs e)
{
   try
   {
      Process.Start("settings.txt");
   }
   catch(Exception ex)
   {
      MessageBox.Show(ex.ToString());
   }
  }

private void button1_Click(object sender, EventArgs e)
{
    File.WriteAllText("settings.txt", "Test Writing");
}

Surprisingly, I'm getting an error:

The system cannot find the file specified

exception the next time i reboot my computer and that program runs...like the files doesn't exist...

More over, when I launch the program(manually) it does find the file and it starts it(I'm suppose to read from that text file,but right now I'm just trying to launch it).

I also tried to print the program execution path, but both tries(manual launch and the windows Startup launch) print the same path.

Does anyone have any idea for my case?

Thanks.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
Slashy
  • 1,841
  • 3
  • 23
  • 42
  • not surprising. If that file is not in the same folder as your app it wont be found. – Ňɏssa Pøngjǣrdenlarp Dec 17 '16 at 20:57
  • Yeah, my guess would be that when you run the program manually, its "start" location or current path is different than its current path when the system starts it for you. – Douglas Dwyer Dec 17 '16 at 21:28
  • See https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application/837501#837501 on how to find the path of your application. – sgmoore Dec 17 '16 at 21:29
  • This answer might help: http://stackoverflow.com/questions/15653921/get-current-folder-path – Klinger Dec 17 '16 at 21:30

1 Answers1

1

You need to get path relative to your executable file.

Try addinng this AppDomain.CurrentDomain.BaseDirectory like:

Process.Start(AppDomain.CurrentDomain.BaseDirectory + "settings.txt");
Markiian Benovskyi
  • 2,137
  • 22
  • 29