2013-03-08 172 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,它會返回一致的結果。