0

We have a C# backend application here at work that I've written. It saves it's state to a txt file so that when it starts up again it'll have the details it needs to continue working. If I add a key to the registry to start the app when the user signs on (HKCU) then the app will start but it fails to read it's state txt file.

I don't know what's happening here. The txt file has to be in the same folder as the app and I load it like this:

String savepath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), SaveFile);
if (File.Exists(savepath)) LoadState();

And I actually read the file using:

String[] lines = File.ReadAllLines(SaveFile);

None of this is really complicated but because the contents of the txt file aren't being loaded, I am assuming that either 1) the File.Exists() is coming back false or 2) it's coming back true and File.ReadAllLines() is coming back empty.

If I close the program and immediately re-run it then it reads the file just fine. What can I do to have my app read it's file when the computer starts up?

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
  • `File.Exists` will return false if you don't have access – Sriram Sakthivel Sep 12 '13 at 20:05
  • If you can't debug it (I presume you can't since it happens during the computer startup), you can add MessageBox-es or write to another file what is actually happening in the app. Something like an old school JS debugging. – Nikola Davidovic Sep 12 '13 at 20:08

3 Answers3

2

Actually, you not checking and not opening the same file (you're passing absolute path to File.Exists and relatively path to File.ReadAllLines), if the current directory is different from Path.GetDirectoryName(Application.ExecutablePath), which is the case when you start your program on boot (I guess it should be %WINDIR%\System32, but I am not sure - however, it is definitively not your application folder).

String savepath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), SaveFile);
if (File.Exists(savepath)) LoadState();
    String[] lines = File.ReadAllLines(SaveFile);

should be

String savepath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), SaveFile);
if (File.Exists(savepath)) LoadState();
    String[] lines = File.ReadAllLines(savepath);

It will fail for the first time because the working directory will be different when you start it manually and when Windows start it on startup.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • I think this is true and that you are right, maybe he should write the file path to the registry too. That would be the simplest solution. – Nikola Davidovic Sep 12 '13 at 20:13
0

Try removing the File.Exists() check (temporarily) and see if it will return a more detailed message. It will help diagnose specifically why it is failing. There is a chance the file simply doesn't exist--in which case you should log the full path it's looking for, because it may not be what you expect. Also there might be permissions issue if the process is being started under a different identity.

STW
  • 44,917
  • 17
  • 105
  • 161
0

You should be sure that when you execute your app it has the same permissions than when it's executed by the system, otherwise it will not be able to access the file.

Also, you may want to check if the file is locked by some cause, and may check this thread: How to check for file lock?

Community
  • 1
  • 1
Roberto Luis Bisbé
  • 2,080
  • 1
  • 15
  • 22