0

I assumed (and we all know what that gets you) that My.Computer.Filesystem pointed to where the exe was installed. Most of the time, that premise is holding true. However, occasionally it is pointing to C:\Windows\System32 and my code is failing due to permissions.

So where is it really pointing and why the difference?

Thanks

user3850146
  • 97
  • 1
  • 11
  • 1
    `My.Computer.FileSystem` points to a class holding properties and methods used to handle files and directories, it doesn't point to a specific path. Please be more specific and share the code you are experiencing problems with. – Visual Vincent Jan 17 '17 at 20:44
  • add a path to the file name and you wont wonder where it was written to – Ňɏssa Pøngjǣrdenlarp Jan 17 '17 at 20:49
  • The exe is not always installed in the same location. – user3850146 Jan 17 '17 at 20:51
  • The code is 'My.Computer.FileSystem.WriteAllText("done.txt", "0", False, Encoding.ASCII)' and the error is "Access to the path 'C:/Windows/system32/done.txt' is denied." – user3850146 Jan 17 '17 at 20:53
  • 1
    Who said anything about the EXE? You wont always have access to that folder either. For some time now WIndows has provided a set of folders in Users and ProgramData for ...program data – Ňɏssa Pøngjǣrdenlarp Jan 17 '17 at 20:54
  • 1
    Are you after [Application.StartupPath](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath(v=vs.110).aspx?) – Bugs Jan 17 '17 at 20:54
  • 1
    That's a relative path. It points to the application's working directory (in that case `System32`). It is Windows who points your application there, not `My.Computer.FileSystem`. – Visual Vincent Jan 17 '17 at 20:55
  • 4
    Not for nothing, but you have asked 11 questions and gotten 11 answers but not accepted any of them and never ever voted. If you are not in a position to provide answers, you could at help others by just by accepting answers and upvoting good posts you see. Accepting and UpVotes helps others find good answers. The [Tour] explains how SO works. – Ňɏssa Pøngjǣrdenlarp Jan 17 '17 at 21:13
  • Point taken and duly chastised! – user3850146 Jan 17 '17 at 21:38

1 Answers1

2

Instead of using a relative path use Path.Combine() together with Application.StartupPath. The latter will get the directory where the .exe was started from.

My.Computer.FileSystem.WriteAllText(Path.Combine(Application.StartupPath, "done.txt"), "0", False, Encoding.ASCII)
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75