Slashy
Slashy

Reputation: 1881

The system cannot find the file specified c#

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.

Upvotes: 2

Views: 2286

Answers (1)

Markiian Benovskyi
Markiian Benovskyi

Reputation: 2161

You need to get path relative to your executable file.

Try addinng this AppDomain.CurrentDomain.BaseDirectory like:

Process.Start(AppDomain.CurrentDomain.BaseDirectory + "settings.txt");

Upvotes: 1

Related Questions