使用安装向导部署Windows应用程序时出现问题

时间:2010-05-20 11:46:55

标签: c# winforms

我有一些xml文件,在我的应用程序中使用。它们与应用程序存储在同一文件夹中,位于子文件夹DATA:“C:\ MyProject \ DATA \”中。 要获取DATA文件夹路径,请使用以下代码:

 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\"; 
 }

因此,当我想部署我的应用程序时,我创建一个安装项目,并将DATA文件夹添加到Application文件夹。但是在我安装程序f.e. “C:\ Project”(DATA文件夹 - “C:\ Project \ DATA”我收到错误:“找不到文件夹C:\ DATA”。 我需要更改以使部署后的工作正常进行。为什么它在1级以上查找DATA文件夹?

2 个答案:

答案 0 :(得分: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);

答案 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和Release版本的话,那就是这个:

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

您必须添加using System.IO;才能生效。

相关问题