如何在C#中编程环境变量,而不使用环境枚举? [英] How to resolve environment variables programatically in C# without using Environment Enum?

查看:254
本文介绍了如何在C#中编程环境变量,而不使用环境枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

展开我的文档的环境变量

我们经常在Windows Xp,7,Vista,Server 2003-2008R2和未来的Windows版本中的命令提示符中使用%%分隔的环境变量。例如:

We often use %% delimited environment variables in Command prompt in Windows Xp, 7, Vista, Server 2003-2008R2 and possibly in future windows versions. Examples are:

%windir%
%systemroot%
%temp%

我需要使用C#编程解决这些环境变量。

I need to be able to programatically resolve these environment variables using C#.

请不要建议 Environmen.SpecialFolder ,因为我从上述格式的webservice获取价值,我需要能够以编程方式解决它们并确定这些服务器上的路径我的方案是一个class libarary项目,我可以需要/可以使用 Server.MapPath

Please do not suggest Environmen.SpecialFolder , because i am getting values from a webservice in the above formats and i need to able to programmatically resolve them and determine physical location of these paths on server. My scenario is a class libarary project and i may NOT require/be able to use Server.MapPath

我正在寻找一种内置或自定义的通用方法,可以帮助我以语法方式解析所有环境变量,并从这些变量计算本地物理路径,只需命令提示符或Windows资源管理器或运行命令即可!

I am looking for a generic method builtin or custom that can help me resolve all environment variables pro grammatically and calculate local physical paths from these variables just Command Prompt or Windows Explorer or Run Command does !

我看到与StackOverFlow类似的问题,但找不到任何标记的答案。请注意, Environment.Xxxxx枚举不适合我正在工作的场景。

I have seen questions similar to these on StackOverFlow but couldn't find any marked answers. Please note that Environment.Xxxxx Enum doesnt cater for the scenario i am working through.

任何帮助都不胜感激。我正在使用.NET 4.0 Complete Framework Profile with C#。

Any help is appreciated. I am using .NET 4.0 Complete Framework Profile with C#.

推荐答案

可以使用 。例如:

var resolvedPath = Path.GetFullPath("%WINDIR%");


或者如果您只想扩展字符串中的变量,请使用 Environment.ExpandEnvironmentVariables

var expanded = ExpandEnvironmentVariables(
    "This is my %WINDIR% in %SYSTEMROOT%, and temp is %TEMP%")

这篇关于如何在C#中编程环境变量,而不使用环境枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆