Reputation: 15725
I'm working on an app that saves a file in Path.GetDirectoryName(FilePath)
where FilePath = Application.ExecutablePath
that contains some information about licensing. If I run the program from VS it works OK but if I make an installer and install and then run it, the program thinks that the file already exists. I changed my program to show on a message box my FilePath
and whether File.Exists(FilePath)
returns true
or false
. So I looked in that path, enabled showing hidden and system files, F5'd several times and nothing. The file doesn't exist, but File.Exists(FilePath)
returns true. Any idea why cold this be happening and how can I work around it?
I'm using Windows Vista, Visual Studio 2010, C# and created my installer with VS's Setup Project.
Edit: My path is: C:\Program Files (x86)\Helium\License.xml.
This is part of my code:
MessageBox.Show("LicenseFileName: " + LicenseFileName); // LicenseFileName: C:\Program Files (x86)\Helium\License.xml
System.Diagnostics.Process.Start(LicenseFileName); // Nothing happens
MessageBox.Show("File.Exists(LicenseFileName): " + File.Exists(LicenseFileName)); // File.Exists(LicenseFileName): true
Forgot to say that I already had the application installed before so the file used to exist. I uninstalled using Control Panel.
Upvotes: 9
Views: 5617
Reputation: 33146
For anyone using Unity3D (whose authors never learned to write C# correctly -- they require "/" everywhere, instead of using the .net platform-local file APIs): this method always returns true if you pass it absolute paths using Unity's linux-style Path.DirectorySeparatorChar. I don't know how it's resolving them - there's nothing in the VirtualStore, nothing in the GetCurrentDirectory that could possibly match - and I don't see how you can debug File.Exists (horrible API).
So if your path looks like this (a valid Unity URL) and you're on Windows:
C:/project/filename.txt
... C# will always return "yes, it exists".
But if you convert it to the correct Windows path:
C:\\project\filename.txt
... C# will return true/false depending on whether the file exists.
**Note: formatting bugs in stackoverflow are corrupting the second path above. To be very clear: it should display the letter "C", followed by a colon, followed by TWO backslashes, and the words 'project' and 'file' are separated by one backslash.
Note: if you only have one backslash ... File.Exists again fails and returns incorrect values (always true). Microsoft's docs say the extra slash is not needed, but experimentally: it is.
Upvotes: 0
Reputation: 8265
If you are installing to a system folder, it is possible that Windows file virtualization kicked in and created a per user copy of the files. So your files may be located somewhere in %userprofile%\AppData\Local\VirtualStore folder
Upvotes: 23