File Access Problem

MADMAN69UK

Member
Joined
Jun 23, 2004
Messages
6
Programming Experience
Beginner
Hey there.

I have written a small program just to test a few bits of code out before I insert it into a larger program. Everythings works other than a little problem when opening a file.

Basically, the program is supposed to change the location of a .txt file. If I leave the .txt file in the BIN directory and run the prog and select the file, it opens it fine. If however I copy the same .txt file to C:\ for example, then try to open it I get the 'File Not Found!' message which I entered into the program for errors.

Does anyone have any idea what I can do to prevent this? I assume it's something to do with the openfiledialog but I'm not really sure.

Cheers.


-----Edit-----
After further testing, if I copy the compiled .exe and the .txt file to C:\ and run the program then it works fine. It seems to only work when the .txt file is still in the same folder as the .exe file. Is there some code like '%App Path%/Data.txt' or something to that effect which I have to use? Like with DOS when you'd use '%SystemRoot%'.
 
Last edited:
Application.StartupPath + "\MyFile.txt" would access MyFile.txt in the same directory as your exe, otherwise you should specify a path for the file aswell.
 
What about when the application is installed to a different location? Wouldn't the path be different and the file not found?

I incorporated a 'location.ini' file with the path stored in it. I also added a button labelled 'Change Location', when I click the button it calls an OpenFileDialog box, when I browse to a different file that isn't in the same directory as the program it gives the 'File Not Found!' error.

Do you think the Application.Path with resolve this issue?

Sorry to sound like a spanner, I'm a relative newbie to VS.NET.
 
I can do better than supply the code, I'll put the actual program up for you :)

Ok, the idea is, the program reads a file location from an .ini file included in the program directory. I use the data.txt file as the file to select. If you try clicking Reset Location, that will change the textbox back to it's default data.txt value, then if you click change file location and select the data.txt file in the programs own directory it will display the path there fine also. However, if you move the data.txt file to say... the windows desktop, then use the program to navigate to the file and try to click it, the error message pops up. Quite a frustrating little problem really.

Anywho, hopefully someone will see an easy solution to this. You can download the ZIP containing the program HERE.

Thanks.
 
That's odd, I didn't realize it worked like that.

What's happening is when a file is selected in the OpenFielDialog, the working directory is changed to the directory that the selected file is in. When you try to open Location.ini, the app is looking in that working directory (the one that contains the Data.txt file that you selected in the openFileDialog).

In the MenuItem4_Click procedure, change two lines to these two lines:

VB.NET:
sw = New StreamWriter(File.Open(Application.StartupPath & "\Location.ini", _
  FileMode.Truncate))

sr = New StreamReader(File.Open(Application.StartupPath & "\Location.ini", _
  FileMode.Open))
 
yeah, that's a clever 'Feature' of the dialog boxes, when you use them is sets the CurrentDirectory (or whatever it's called nowadays) to be the selected folder. that's why you should ALWAYS specify a path. have even had problems in the past with not specifying a path, then using a dialog box in a different application (Word i think it was) and it changed my working directory and it went BOOM!!! (back in the days when i was still learning about error handling :)
 
Back
Top