问题
I'm using someone else's code (licensed) on two different machines. On one machine, the Application.ExecutablePath returns the result the programmer must have expected, on the other it does not. Both are Windows 7 machines.
On my machine, the Application.ExecutablePath returns something like:
"C:\\Dir1\\Dir2\\Dir3/bin/Debug/APP.EXE"
On the other machine, it returns
"C:\\Dir1\\Dir2\\Dir3\\bin/Debug/APP.EXE"
The programmer obviously expected the second return string, because the code does this:
string path = Application.ExecutablePath;
short found = (short)path.LastIndexOf(@"\");
if (found > -1)
{
path = path.Substring(0, found);
}
try
{
foreach (string File in Directory.GetFiles(path + @"\Res\Patterns\", "*.xml"))
{
found = (short)File.LastIndexOf(@"\");
if (found > -1)
//... use files found
and the directory of files is present in both machines under Dir3, so it is found on the other machine but not on mine. I can't find any information on when and where Windows decides to return the forward slash (like a URL path) vs. the UNC path using "\". Why would this code work differently on different machines?
回答1:
I am guessing that the path you simplified to C:\\Dir1\\Dir2\\Dir3/bin/debug
actually had a hash (#) in the Dir3 name.
This is a quirk with Application.ExecutablePath
apparently. You can use Assembly.GetEntryAssembly().Location
instead, which returns consistent results.
来源:https://stackoverflow.com/questions/15296688/application-executablepath-in-c-sharp-has-mixed-separator-characters