Application.Executablepath in C# has mixed separator characters

試著忘記壹切 提交于 2019-12-11 07:57:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!