简体   繁体   English

如何仅查找目录AppData \\\\ Local的一部分?

[英]How do i find only part of the directory AppData\\Local?

I have this: 我有这个:

private void SearchForDoc()
{
    string t = Path.GetDirectoryName(Application.LocalUserAppDataPath);
}

Then the variable t contain: 然后变量t包含:

C:\\Users\\bout0_000\\AppData\\Local\\Diagnostic_Tool_Blue_Screen\\Diagnostic Tool Blue Screen

I want that t will contain only the C:\\\\Users\\\\bout0_000\\\\ and if someone else username is testme so the the variable t will be: C:\\\\Users\\\\testme\\\\ 我希望t仅包含C:\\\\Users\\\\bout0_000\\\\ ,如果其他用户名是testme ,则变量t将为: C:\\\\Users\\\\testme\\\\

And not all this long path. 并非所有这条漫长的路。

Environment.GetEnvironmentVariable("UserProfile");

如果您当前的用户是'toto',则此命令将返回'C:\\ Users \\ toto'

我认为这会做

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

As Ulaga said you could use Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); 正如乌拉加所说,您可以使用Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); to get the users directory, or you could just cut off after the first 3 folders. 以获得用户目录,或者您可以在前三个文件夹之后切断。

string t = Path.GetDirectoryName(Application.LocalUserAppDataPath);
t = String.Join(@"\", t.Split('\\').ToList().Take(3).ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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