2010-05-20 150 views
0

我有一些xml文件,這些文件在我的應用程序中使用。它們存儲在與應用程序相同的文件夾中,位於子文件夾DATA:「C:\ MyProject \ DATA \」中。 獲取數據的文件夾路徑我用這個代碼:使用安裝嚮導部署Windows應用程序的問題

static public string GetDataFolderPath() 
{ 
     string s = System.IO.Directory.GetCurrentDirectory().Replace(@"\bin\Debug", ""); 
     int i = s.LastIndexOf(@"\"); 
     s = s.Substring(0, i); 
     i = s.LastIndexOf(@"\"); 
     s= s.Substring(0, i); 
     return s + @"\Data\"; 
} 

所以,當我想部署我的應用程序中,我創建了一個安裝項目,並添加數據文件夾到應用程序文件夾。但是,我安裝程序f.e.之後。 「C:\ Project」(DATA文件夾 - 「C:\ Project \ DATA」中出現錯誤:「找不到文件夾C:\ DATA」 部署後我需要更改以使其工作。嗎?1個水平

回答

0

試試這個,它可能會更好地工作:

public static string GetDataFolderPath() 
{ 
#if DEBUG 
    // This will be executed in Debug build 
    string path = Directory.GetCurrentDirectory().Replace(@"\bin\Debug", ""); 
#else 
    // This will be executed in Release build 
    string path = Directory.GetCurrentDirectory(); 
#endif 
    return Path.Combine(path, "Data"); 
} 

或者只是這個,如果你想要一個Debug和發佈版本:

public static string GetDataFolderPath() 
{ 
    string path = Directory.GetCurrentDirectory().Replace(@"\bin\Debug", ""); 
    return Path.Combine(path, "Data"); 
} 

您必須添加using System.IO;這個工作。

+0

非常感謝,這就是我一直在尋找 – Mike 2010-05-21 07:03:24

0

也許當前目錄中的數據文件夾(啓動程序期間)是不一樣的一個組件說謊

嘗試:?

//get the full location of the assembly 
string fullPath = System.Reflection.Assembly.GetAssembly(typeof(<your class name>)).Location; 

//get the folder that's in 
string theDirectory = Path.GetDirectoryName(fullPath); 

string codeBase = Assembly.GetExecutingAssembly().CodeBase; 
UriBuilder uri = new UriBuilder(codeBase); 
string path = Uri.UnescapeDataString(uri.Path); 
return Path.GetDirectoryName(path);