获取目录:非静态字段,方法或属性所需的对象引用

时间:2015-01-22 21:33:01

标签: c#

您好我无法获取可执行文件的目录名称:

最初我有这一行,它工作正常,但是当我通过Python启动exe时,它失败了,我在这篇文章中详述了我的问题(Python launch exe file error

const String LocalUpdateFile = @".\Updates.dat";

所以我将我的代码更改为解决了我的问题:

const String LocalUpdateFile = @"C:\XX\Updates.dat";

但是我希望目录路径是动态的,所以我使用它但是我被卡住了:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

const String LocalUpdateFile = appPath+@"\UpdateInfo.dat";

它告诉我这个错误:

  

错误1非静态字段,方法或属性'xx.appPath'需要对象引用xx \ Updater.cs 20 34 xx

有人在乎解释发生了什么吗? :(

1 个答案:

答案 0 :(得分:2)

您无法基于非静态字段定义常量。试试这个,改变是在第二个代码行中:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string LocalUpdateFile = appPath+@"\UpdateInfo.dat";

或者,如果您声明方法/属性之外的字段:

static string appPath = Path.GetDirectoryName(Application.ExecutablePath);
const string LocalUpdateFile = appPath+@"\UpdateInfo.dat";