2010-05-20 151 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);