0

I've created an WFA app and I decided to test it on my PC so I made an installator for it. My programme is collaborating with .txt file inside the same folder so in my code I get its execution path by AppDomain.CurrentDomain.BaseDirectory and add to it "myTxt.txt" to gain txt path. Then I read get its content via File.ReadAllLines() to my string array.

string path = AppDomain.CurrentDomain.BaseDirectory;
string text = String.Concat(path,"mytxt.txt");
string[] content = File.ReadAllLines("mytxt.txt") // relative path

And here comes the problem. Whenever my programme is set in "Program files" I can open .txt but no content is loaded to the array. Otherwise when it is set in Program files(x86) everything works great. Why is that happening? How shall I solve the problem?

Dartek12
  • 172
  • 1
  • 2
  • 12
  • I would suggesting using `System.IO.Path.Combine` instead of `String.Concat` on the second line. I don't understand your code though, why are you finding the full path and then not using it on the next line? What happens when you use `text` as your parameter value? – ohiodoug Mar 01 '16 at 23:22
  • The full path is used to determine if mtxt.txt file exists or should I create a one – Dartek12 Mar 01 '16 at 23:24
  • 2
    You can only store your program in c:\program files if your EXE's platform target is AnyCPU or x64. So it will run as a 64-bit process on a 64-bit operating system. The default is x86 and any file open requests will be redirected to to c:\program files (x86) instead. Fix it by right-clicking your EXE project > Properties > Build tab > Platform target setting. Untick "Prefer 32-bit" if you see it. Or sure, storing your program in the 32-bit directory. – Hans Passant Mar 01 '16 at 23:25
  • Thank you Hans, that has fixed it :) – Dartek12 Mar 02 '16 at 15:22

1 Answers1

0

Hans comment above is correct, but I believe there is more to it besides the targeted/execution platform. First, AppDomain.BaseDirectory is not necessarily the same as the exe folder. And second, ReadAllLines method, when called with a relative file path, will apply it to the CurrentDirectory. You can test this with the following code.

File.ReadAllLines("test.txt");
Directory.SetCurrentDirectory(@"c:\temp");
File.ReadAllLines("test.txt");

Another point to call attention to is that AppDomain.BaseDirectory never changes, while the CurrentDirectory can (and probably will) change many times.

So, if you know the application path and if the mytxt.txt file sits in a fixed relative path from the application path, you could fix your code by correcting the relative path passed as argument for the ReadAllLines function. Otherwise, you could build the full path using Path.Combine, (as suggested by ohiodoug above) and use it instead of the relative path. I recommend the later option.

Below you find a few links to posts and documentations relevant to your question:

Should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?

Why AppDomain.CurrentDomain.BaseDirectory not contains “bin” in asp.net app?

AppDomain.BaseDirectory Property

File.ReadAllLines Method (String)

Community
  • 1
  • 1
EduardoCMB
  • 392
  • 2
  • 17