展开环境变量,我的文档环境变量、我的文档

2023-09-03 02:27:01 作者:五行缺钱

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

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");

有等于SpecialFolder.MyDocuments的environement变量?

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");

这样,我结束了像 @C:\用户\< MYUSER> \我的文档\富,但我什么,我需要的是 @\\ someservername \用户$ \< MYUSER> \我的文档\富。

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".

编辑:我的目标是不是要拼命code无论是环境变量,也没有在那之后的部分。

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

任何其他建议?

推荐答案

没有没有为我的文档的特殊文件夹没有环境变量(同样适用于大多数成员在 SpecialFolder 枚举)。

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

查看this段,它可能是你正在寻找什么的。

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");

注:SpecialFolder.ExpandVariables在上面的代码中引入了一个辅助类的静态方法

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

 
精彩推荐