1)我怎样才能找到用户正在使用的 Windows 安装驱动器。?我需要这个导航到 DocumentsandSettings 中的ApplicationData。
2)另外我怎样才能获得用户名,以便我可以转到ApplicaitionData。?例如:“D:\Documents and Settings\user\Application Data”。
看看结合Environment.GetFolderPath和Environment.SpecialFolder来做到这一点。
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
根据你在做什么,你可能还想看看
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
如果用户在域中,它将仅存储在他们的本地AppData
文件夹中,并且不会与他们的漫游配置文件同步。
Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System
这也应该让你绕过用户名要求。
查看 System.Environment 类及其属性和方法,例如:
string systemDir = System.Environment.SystemDirectory;
string docs = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.MyDocuments));
string systemDrive = System.IO.Path.GetPathRoot(systemDir);
例如,第一个返回“ C:\Windows\system32 ”,第二个返回“ C:\Documents and Settings\USERNAME\My Documents ”。
试试这个:
string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
1)我怎样才能找到用户正在使用的 Windows 安装驱动器。?
var systemDrive = Environment.ExpandEnvironmentVariables("%systemdrive%");
我需要这个导航到 DocumentsandSettings 中的 ApplicationData。
您实际上并不需要获取系统驱动器的值或当前登录的用户名来实现此目的。有预定义的环境变量%localAppData%
,%appData%
它们为您提供这些目录的完全限定路径,如下面的代码所示:
var localApplicationData = Environment.ExpandEnvironmentVariables("%localappdata%");
//this gives C:\Users\<userName>\AppData\Local
var roamingApplicationData = Environment.ExpandEnvironmentVariables("%appdata%");
//this gives C:\Users\<userName>\AppData\Roaming
2)另外我怎样才能获得用户名,以便我可以转到ApplicaitionData。?例如:“D:\Documents and Settings\user\Application Data”。
同样,您不需要用户名来获取我上面讨论的应用程序数据路径。不过,为了知识起见,您可以从%username%
环境变量中获取它,如下所示:
var currentUserName = Environment.ExpandEnvironmentVariables("%username%");