c#打开文件,路径以%userprofile%开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9993561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:02:42  来源:igfitidea点击:

c# open file, path starting with %userprofile%

c#file

提问by Istrebitel

I have a simple problem. I have a path to a file in user directory that looks like this:

我有一个简单的问题。我在用户目录中有一个文件路径,如下所示:

%USERPROFILE%\AppData\Local\MyProg\settings.file

When I try to open it as a file

当我尝试将其作为文件打开时

ostream = new FileStream(fileName, FileMode.Open);

It spits error because it tries to add %userprofile%to the current directory, so it becomes:

它吐出错误,因为它试图添加%userprofile%到当前目录,所以它变成:

C:\Program Files\MyProg\%USERPROFILE%\AppData\Local\MyProg\settings.file

How do I make it recognise that a path starting with %USERPROFILE%is an absolute, not a relative path?

我如何让它认识到以 开头的路径%USERPROFILE%是绝对路径,而不是相对路径?

PS: I cannot use

PS:我不能用

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Because I need to just open the file by its name. User specifies the name. If user specifies "settings.file", I need to open a file relative to program dir, if user specifies a path starting with %USERPROFILE%or some other thing that converts to C:\something, I need to open it as well!

因为我只需要按文件名打开文件。用户指定名称。如果用户指定“settings.file”,我需要打开一个相对于程序目录的文件,如果用户指定的路径以%USERPROFILE%或其他一些转换为 C:\something 的东西开头,我也需要打开它!

采纳答案by Oded

Use Environment.ExpandEnvironmentVariableson the path before using it.

使用Environment.ExpandEnvironmentVariables前在路径上使用。

var pathWithEnv = @"%USERPROFILE%\AppData\Local\MyProg\settings.file";
var filePath = Environment.ExpandEnvironmentVariables(pathWithEnv);

using(ostream = new FileStream(filePath, FileMode.Open))
{
   //...
}

回答by David M

Use the Environment.ExpandEnvironmentVariablesstatic method:

使用Environment.ExpandEnvironmentVariables静态方法:

string fileName= Environment.ExpandEnvironmentVariables(fileName);
ostream = new FileStream(fileName, FileMode.Open);

回答by Andras Zoltan

Try using ExpandEnvironmentVariableson the path.

尝试在路径上使用ExpandEnvironmentVariables

回答by NeverJr

You can use the Environment.Usernameconstant as well. Both of the %USERPROFILE%and this Environment variable points the same( which is the currently logged user). But if you choose this way, you have to concatenate the path by yourself.

您也可以使用Environment.Username常量。两者的%USERPROFILE%和这个环境变量指向相同(这是当前登录用户)。但是如果你选择这种方式,你必须自己连接路径。

回答by john rains

I use this in my Utilities library.

我在我的实用程序库中使用它。

using System;
namespace Utilities
{
    public static class MyProfile
   {
        public static string Path(string target)
        {
            string basePath = 
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + 
@"\Automation\";
            return basePath + target;
        }
    }
}

So I can simply use e.g. "string testBenchPath = MyProfile.Path("TestResults");"

所以我可以简单地使用例如“string testBenchPath = MyProfile.Path("TestResults");”