简体   繁体   中英

C# getting the path of %AppData%

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

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

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

To get the AppData directory, it's best to use the GetFolderPath method:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

(must add using System if not present).

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

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

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

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

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

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)

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

The BEST way to use the AppData directory, IS to use Environment.ExpandEnvironmentVariables method.

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

More info:

%ALLUSERSPROFILE%   C:\ProgramData
%APPDATA%   C:\Users\Username\AppData\Roaming
%COMMONPROGRAMFILES%    C:\Program Files\Common Files
%COMMONPROGRAMFILES(x86)%   C:\Program Files (x86)\Common Files
%COMSPEC%   C:\Windows\System32\cmd.exe
%HOMEDRIVE% C:
%HOMEPATH%  C:\Users\Username
%LOCALAPPDATA%  C:\Users\Username\AppData\Local
%PROGRAMDATA%   C:\ProgramData
%PROGRAMFILES%  C:\Program Files
%PROGRAMFILES(X86)% C:\Program Files (x86) (only in 64-bit version)
%PUBLIC%    C:\Users\Public
%SystemDrive%   C:
%SystemRoot%    C:\Windows
%TEMP% and %TMP%    C:\Users\Username\AppData\Local\Temp
%USERPROFILE%   C:\Users\Username
%WINDIR%    C:\Windows

You can also use

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

to expand the %AppData% variable.

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

AppData ⇝ Local aka ( C:\\Users\\<user>\\AppData\\Local ):

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

AppData ⇝ Roaming aka ( C:\\Users\\<user>\\AppData\\Roaming ):

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Additionally, it could be handy to know:

  • Environment.SpecialFolder.ProgramFiles - for Program files X64 folder
  • Environment.SpecialFolder.ProgramFilesX86 - for Program files X86 folder

For the full list check here .

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

try

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

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

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

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

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

For ASP.NET, the Load User Profile setting needs to be set on the app pool but that's not enough. There is a hidden setting named setProfileEnvironment in \\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 Environment class will then return proper values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM