Hi guys/gals,

I've recently developed a piece of software and it currently uses 1 external .txt file (located in the bin\debug folder of the project folder) that is used to store data long term.

The program works wonderfully when i use the visual studio 2008 debugger, but when i either publish it, use the .exe from the project folder or save it as a release version it gives me various errors with regards to not being able to find the .txt which its trying to read.

Does anyone have any idea's as to why this might be?

Many thanks,
Connor

Dani AI

Generated

: this happens because the app is relying on a relative working directory and is installed into a protected location. was right to ask about the path, and is also correct: Windows protects the Program Files folder so you can’t reliably write there. The reliable pattern is: ship any read-only template in the install folder, but copy user-writable data into a per-user (or per-machine) data folder on first run and always read/write that copy.

Practical checklist:

  • Mark the shipped .txt as Content and set “Copy to Output Directory” so the installer includes it.
  • On startup, check a per-user data folder (create it if missing), copy the shipped template there on first run, then always use that path for subsequent reads/writes.
  • If you need the data shared between all users, have the installer create a folder under the machine-wide application data location and set appropriate ACLs.
  • Log and surface exceptions (UnauthorizedAccessException) to confirm permission problems during testing.

Example (VB.NET): copy a shipped default file to a per-user app-data folder and then use that file for I/O.

Dim installFolder = My.Application.Info.DirectoryPath
Dim dataFolder = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData, "YourCompany", "YourApp")
Dim dataFile = IO.Path.Combine(dataFolder, "data.txt")

If Not IO.Directory.Exists(dataFolder) Then IO.Directory.CreateDirectory(dataFolder)

If Not IO.File.Exists(dataFile) Then
    Dim source = IO.Path.Combine(installFolder, "data.txt")
    If IO.File.Exists(source) Then IO.File.Copy(source, dataFile) Else IO.File.WriteAllText(dataFile, String.Empty)
End If

' Now always read/write using dataFile
Dim content = IO.File.ReadAllText(dataFile)
IO.File.WriteAllText(dataFile, content & vbCrLf & "Added line")

For details on the platform locations you can use, see the .NET docs for special folders and the VB special-directories helper:

Recommended Answers

All 5 Replies

Wich is the path you use to open the file?

well im using the stream read/write statements so i dont have to define a path, but its in the project>bin>debug folder.

Are you running on Vista or Win7? Do you install to the Program Files directory?

windows 7 and i run the installer to the default directory, which is the program files directory.

By default on Vista and Windows 7 you can not write to the Program Files directory. External Files need to go to the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) . I wrote all about that here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.