展开“我的文档”的环境变量

发布于 2024-10-12 18:32:27 字数 697 浏览 14 评论 0原文

我知道我可以读取这样的环境变量:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

但是,如果我可以做这样的事情,那对我来说真的很有帮助:

Environment.ExpandEnvironmentVariables(@"%MyDocuments%\Foo");

Is there anenvironmentvariables that equals SpecialFolder.MyDocuments?

我也尝试做这样的事情,但这并没有达到预期的结果:

Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\My Documents\Foo");

这样我最终得到了类似 @"C:\Users\\My Documents\Foo" 但我需要的是 @"\\someservername\users$\\My Documents\Foo"

编辑:我的目标不是对环境变量或之后的部分进行硬编码。

还有其他建议吗?

I know I can read environment variables like this:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

However, it would be really helpful to me if I could do something like this:

Environment.ExpandEnvironmentVariables(@"%MyDocuments%\Foo");

Is there an environement variable that equals SpecialFolder.MyDocuments?

I also tried to do something like this, but this doesn't lead to the expected result:

Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\My Documents\Foo");

This way I ended up with something like @"C:\Users\<MyUser>\My Documents\Foo" but I what I need is @"\\someservername\users$\<MyUser>\My Documents\Foo".

EDIT: My Goal is NOT to hardcode either environment variable nor the part after that.

Any other suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不爱素颜 2024-10-19 18:32:27

否,MyDocuments 特殊文件夹没有环境变量(对于 SpecialFolder 枚举的大多数成员也是如此)。

查看此代码段,它可能正是您的情况寻找。

它允许您执行类似的操作:

string fullPath = SpecialFolder.ExpandVariables(@"%MyDocuments%\Foo");

注意:SpecialFolder.ExpandVariables 是上面代码片段中引入的辅助类的静态方法。

No there is no environment variable for the MyDocuments special folder (the same is true for most members of the SpecialFolder enumeration).

Check out this snippet, it might be exactly what you are searching for.

It allows you to do something like that:

string fullPath = SpecialFolder.ExpandVariables(@"%MyDocuments%\Foo");

Note: SpecialFolder.ExpandVariables is a static method of a helper class introduced in the above snippet.

夏有森光若流苏 2024-10-19 18:32:27

是否有等于 SpecialFolder.MyDocuments 的环境变量?

简短回答:否。

详细回答:
还是不行。您可以在命令提示符中输入“set”来查看当前的所有环境变量。我在我的个人资料中找不到任何文档文件夹(在 WinXP 和 Win7 上尝试过)。

另外,扩展 "%USERPROFILE%\My Documents" 是不正确的,因为用户的文档文件夹可能位于其他任何地方(例如,在我的家用电脑上,我总是将其更改为 D:\Documents< /代码>)。

如果您确实需要使用环境变量,一种解决方案可能是自己设置变量:

// this environment variable is created for the current process only
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Environment.SetEnvironmentVariable("MYDOCUMENTS", documents);

另一种解决方案可能是在路径中使用“假”环境变量并自行扩展它,例如:

string path = "%MYDOCUMENTS%\\Foo"; // read from config

// expand real env. vars
string expandedPath1 = Environment.ExpandEnvironmentVariables(path);

// expand our "fake" env. var
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string expandedPath2 = path.Replace("%MYDOCUMENTS%", documents);

Is there an environment variable that equals SpecialFolder.MyDocuments?

Short answer: No.

Long answer:
Still no. You can type "set" into a Command Prompt to see all you current environment variables. I couldn't find any for my documents folder on my profile (tried on WinXP and Win7).

Also, expanding "%USERPROFILE%\My Documents" would be incorrect since the user's documents folder could be anywhere else (e.g., on my home PC I always change mine to D:\Documents).

If you really need to use environment variables, one solution might be to set the variable yourself:

// this environment variable is created for the current process only
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Environment.SetEnvironmentVariable("MYDOCUMENTS", documents);

Another solution might be to use a "fake" environment variable in the path and expand it yourself, something like:

string path = "%MYDOCUMENTS%\\Foo"; // read from config

// expand real env. vars
string expandedPath1 = Environment.ExpandEnvironmentVariables(path);

// expand our "fake" env. var
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string expandedPath2 = path.Replace("%MYDOCUMENTS%", documents);
熊抱啵儿 2024-10-19 18:32:27

你到底想做什么?有什么理由不能只使用 路径.组合

string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string foo = Path.Combine(docs, "Foo");

What exactly are you trying to do? Is there any reason why you can't just use Path.Combine?

string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string foo = Path.Combine(docs, "Foo");
冷月断魂刀 2024-10-19 18:32:27

我不确定是否有好的方法可以做到这一点,但与其尝试进行环境扩展来获取路径,为什么不使用 Path.Combine API 呢?

Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
  "Foo");

I'm not sure if there is a good way to do this but instead of trying to do environment expansion to get the path why not use the Path.Combine API instead?

Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
  "Foo");
漫雪独思 2024-10-19 18:32:27

您可以使用 Environment.GetEnvironmentVariable 方法扩展环境变量。鉴于您的评论,我建议将您的路径分成 2 个单独的配置设置,以便更轻松地扩展它:

string variablePath = "%appdata%".Trim('%'); //read from some config setting
string appdataPath = Environment.GetEnvironmentVariable(variablePath);
string subdir = "foo";  //some other config setting
string myDir = Path.Combine(appdataPath, subdir);

You can expand environment variables using then Environment.GetEnvironmentVariable method. Given your comment, I would suggest breaking your path up into 2 separate config settings to make expanding it easier:

string variablePath = "%appdata%".Trim('%'); //read from some config setting
string appdataPath = Environment.GetEnvironmentVariable(variablePath);
string subdir = "foo";  //some other config setting
string myDir = Path.Combine(appdataPath, subdir);
心清如水 2024-10-19 18:32:27

不,它不存在。最简单的检查方法是从命令行运行“set”并查看自己。

Start-> run -> cmd
set
set |findstr /i documents

No it does not exist. The easiest way to check is to run "set" from command line and see yourself.

Start-> run -> cmd
set
set |findstr /i documents
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文