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

时间:2022-04-02 23:29:34

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?

是否有一个等于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".

这样我就得到了像@“C:\ Users \ \ My Documents \ Foo”这样的东西,但我需要的是@“\\ someservername \ users $ \ \ My Documents \ Foo”。

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

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

Any other suggestions?

还有其他建议吗?

6 个解决方案

#1


11  

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

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

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.

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

#2


7  

Is there an environment variable that equals SpecialFolder.MyDocuments?

是否有一个等于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).

答案很长:仍然没有。您可以在命令提示符中键入“set”以查看所有当前环境变量。我在我的个人资料中找不到任何我的文档文件夹(在WinXP和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).

此外,扩展“%USERPROFILE%\我的文档”将是不正确的,因为用户的文档文件夹可能在其他任何地方(例如,在我的家庭PC上我总是将我的文件改为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);

#3


1  

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 API?

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

#4


1  

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

你究竟想做什么?你有什么理由不能只使用Path.Combine吗?

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

#5


1  

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:

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

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

#6


1  

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

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

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

#1


11  

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

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

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.

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

#2


7  

Is there an environment variable that equals SpecialFolder.MyDocuments?

是否有一个等于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).

答案很长:仍然没有。您可以在命令提示符中键入“set”以查看所有当前环境变量。我在我的个人资料中找不到任何我的文档文件夹(在WinXP和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).

此外,扩展“%USERPROFILE%\我的文档”将是不正确的,因为用户的文档文件夹可能在其他任何地方(例如,在我的家庭PC上我总是将我的文件改为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);

#3


1  

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 API?

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

#4


1  

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

你究竟想做什么?你有什么理由不能只使用Path.Combine吗?

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

#5


1  

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:

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

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

#6


1  

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

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

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