VS2008中的记事本路径
在我的应用程序中,我定义了以下内容:
public static readonly string NOTIFY = "%windir%\\notepad.exe";
我可以在 Win7 上的“运行”命令中输入“NOTEPAD”的文本值机,记事本将打开。
但是,在我的 Visual Studio C# 项目中,Write Line 例程每次都会触发:
if (!File.Exists(NOTEPAD)) {
Console.WriteLine("File Not Found: " + NOTEPAD);
}
Does Visual Studio not recognize %windir%
?
In my application, I have defined the following:
public static readonly string NOTEPAD = "%windir%\\notepad.exe";
I can type in the text value of NOTEPAD into the Run command on my Win7 machine, and Notepad will open.
However, from within my Visual Studio C# project, the Write Line routine will fire every time:
if (!File.Exists(NOTEPAD)) {
Console.WriteLine("File Not Found: " + NOTEPAD);
}
Does Visual Studio not understand %windir%
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以让环境类为您执行此操作,就像到目前为止的其他答案所建议的那样手动扩展变量,就像
Run
命令所做的那样:请参阅http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx< /a>
Instead of expanding the variable manually as suggested by the other answers so far, you can have the Environment class do this for you just like the
Run
command does:See http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx
当查看我的 Windows XP 盒子时,记事本的位置是:
不:
您还需要确保这些环境变量已正确解析 - 使用
Environment.GetEnvironmentVariable
和Path.Combine
建立正确的路径:When looking on my windows XP box, the location of notepad is:
Not:
You also need to make sure that these environment variables are resolved correctly - use
Environment.GetEnvironmentVariable
andPath.Combine
to build up the correct path:仔细看看班级环境。环境变量是SystemRoot,所以你可以使用
Environment.GetEnvironmentVariable("windir") (或类似的东西)
http://msdn.microsoft.com/en-us/library/system.environment.getenvironmentvariable.aspx
控制台将 %windir% 环境变量“解析”为正确的路径。您需要使用上述函数在您的应用程序中执行相同的操作。
Just have a closer Look at the Class Environment. The Environment Variable is SystemRoot, so you can use
Environment.GetEnvironmentVariable("windir") (or something like that)
http://msdn.microsoft.com/en-us/library/system.environment.getenvironmentvariable.aspx
The console "Resolves" the %windir% environment variable to the correct path. You need to use the above function to do the same within your application.
使用Environment.GetEnvironmentVariable("windir");
所以你可以这样声明:
Use
Environment.GetEnvironmentVariable("windir")
;So you could declare it like this: