2013-03-08 20 views
1

我在两台不同的机器上使用别人的代码(许可)。在一台机器上,Application.ExecutablePath返回程序员必须预期的结果,而另一台机器却没有。两者都是Windows 7机器。C#中的Application.Executablepath具有混合分隔符

在我的机器,在Application.ExecutablePath返回类似:

"C:\\Dir1\\Dir2\\Dir3/bin/Debug/APP.EXE" 

在其他计算机上,它返回

"C:\\Dir1\\Dir2\\Dir3\\bin/Debug/APP.EXE" 

程序员显然预计第二返回的字符串,因为代码的功能这个:

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 

和文件的目录是存在于两个Dir3下的机器,所以它可以在另一台机器上找到,但不在我的机器上。我无法找到有关何时何地Windows决定使用“\”返回正斜杠(如URL路径)与UNC路径的任何信息。为什么这些代码在不同的机器上工作不同?

回答

1

我猜你简化为C:\\Dir1\\Dir2\\Dir3/bin/debug的路径实际上在Dir3名称中有一个散列(#)。

这显然是Application.ExecutablePath的怪癖。您可以使用Assembly.GetEntryAssembly().Location,它会返回一致的结果。