C# 获取 %AppData% 的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/867485/
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-05 04:51:05  来源:igfitidea点击:

C# getting the path of %AppData%

c#.netpath

提问by ant2009

C# 2008 SP1

C# 2008 SP1

I am using the code below:

我正在使用下面的代码:

dt.ReadXml("%AppData%\DateLinks.xml");

However, I am getting an exception that points to the location of where my application is running from:

但是,我收到一个异常,指向我的应用程序运行的位置:

Could not find a part of the path 'D:\Projects\SubVersionProjects\CatDialer\bin\Debug\%AppData%\DateLinks.xml'.

找不到路径“D:\Projects\SubVersionProjects\CatDialer\bin\Debug\%AppData%\DateLinks.xml”的一部分。

I thought the %AppData%should find the relative path. When I go Start|Run|%AppData%windows explorer takes me to that directory.

我认为%AppData%应该找到相对路径。当我去时,Start|Run|%AppData%Windows 资源管理器会将我带到该目录。

I can not put the full path in, as the user is different on each client machine.

我无法输入完整路径,因为每台客户端计算机上的用户都不同。

采纳答案by Noldorin

To get the AppDatadirectory, it's best to use the GetFolderPathmethod:

获取AppData目录,最好使用以下GetFolderPath方法:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

(must add using Systemif not present).

using System如果不存在则必须添加)。

%AppData%is an environment variable, and they are not automatically expanded anywhere in .NET, although you can explicitly use the Environment.ExpandEnvironmentVariablemethod to do so. I would still strongly suggest that you use GetFolderPathhowever, because as Johannes R?ssel points out in the comment, %AppData%may not be set in certain circumstances.

%AppData%是一个环境变量,它们不会在 .NET 中的任何地方自动扩展,尽管您可以显式使用该Environment.ExpandEnvironmentVariable方法来执行此操作。GetFolderPath但是,我仍然强烈建议您使用,因为正如 Johannes R?ssel 在评论中指出的那样,%AppData%在某些情况下可能不会设置。

Finally, to create the path as shown in your example:

最后,要创建示例中所示的路径:

var fileName = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData), "DateLinks.xml");

回答by danswain

I don't think putting %AppData% in a string like that will work.

我不认为将 %AppData% 放在这样的字符串中会起作用。

try

尝试

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString()

回答by parapet

You can also use

你也可以使用

Environment.ExpandEnvironmentVariables("%AppData%\DateLinks.xml");

to expand the %AppData%variable.

来扩展%AppData%变量。

回答by Simon_Weaver

The path is different if you're talking ASP.NET.

如果您说的是 ASP.NET,则路径会有所不同。

I couldn't find any of the 'SpecialFolder' values that pointed to /App_Data for ASP.NET.

我找不到任何指向 ASP.NET 的 /App_Data 的“SpecialFolder”值。

Instead you need to do this:

相反,您需要这样做:

 HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data")  

(Note: You don't need the 'Current' property in an MVC Controller)

(注意:您不需要 MVC 控制器中的“当前”属性)

If theres another more 'abstract' way to get to App_Datawould love to hear how.

如果有另一种更“抽象”的方式到达,App_Data很想听听如何。

回答by Nathan

In .net2.0 you can use the variable Application.UserAppDataPath

在 .net2.0 中,您可以使用变量 Application.UserAppDataPath

回答by Bitterblue

The BESTway to use the AppData directory, ISto use Environment.ExpandEnvironmentVariablemethod.

使用 AppData 目录的最佳方式,就是使用Environment.ExpandEnvironmentVariable方法。

Reasons:

原因:

  • it replaces parts of your string with valid directories or whatever
  • it is case-insensitive
  • it is easy and uncomplicated
  • it is a standard
  • good for dealing with user input
  • 它用有效目录或其他任何内容替换部分字符串
  • 它不区分大小写
  • 简单易行
  • 这是一个标准
  • 适合处理用户输入

Examples:

例子:

string path;
path = @"%AppData%\stuff";
path = @"%aPpdAtA%\HelloWorld";
path = @"%progRAMfiLES%\Adobe;%appdata%\FileZilla"; // collection of paths

path = Environment.ExpandEnvironmentVariables(path);
Console.WriteLine(path);

Remember some users type %AppData%, some %appdata%and some %APpData%You don't want to end up with:

记住一些用户输入%AppData%,一些%appdata%和一些%APpData%你不想结束:

if (path.ToLower().StartsWith("%appdata%"))
    ; // path manipulation
if (path.ToLower().StartsWith("%programfiles%"))
    ; // path manipulation

If the environment variable is not set, it is not your fault (besides when it IS). I usually don't tell people to notre-invent the wheel but after I first went the other way and realized that it was a bad idea.

如果未设置环境变量,这不是您的错(除了当它是时)。我通常不会告诉人们不要重新发明轮子,但在我第一次走另一条路并意识到这是一个坏主意之后。

回答by cpoDesign

Just wanted to share another way of accessing 'App_Data' folder in my mvc application in case that someone needs this.

只是想在我的 mvc 应用程序中分享另一种访问“App_Data”文件夹的方式,以防有人需要它。

 Path.Combine(HttpRuntime.AppDomainAppPath,"App_Data")

回答by Bill

This is working for me in a console application -

这在控制台应用程序中对我有用 -

string appData = System.Environment.GetEnvironmentVariable("APPDATA");

回答by MoonStom

For ASP.NET, the Load User Profilesetting needs to be set on the app pool but that's not enough. There is a hidden setting named setProfileEnvironmentin \Windows\System32\inetsrv\Config\applicationHost.config, which for some reason is turned off by default, instead of on as described in the documentation. You can either change the default or set it on your app pool. All the methods on the Environmentclass will then return proper values.

对于 ASP.NET,Load User Profile需要在应用程序池上进行设置,但这还不够。有一个名为setProfileEnvironmentin的隐藏设置\Windows\System32\inetsrv\Config\applicationHost.config,由于某种原因,它在默认情况下处于关闭状态,而不是如文档中所述。您可以更改默认值或在您的应用程序池中设置它。Environment类上的所有方法都将返回正确的值。