C# 获取特殊文件夹

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

Get Special Folder

c#

提问by hlatif

please just answer the questionotherways do not respond to this question.

请回答这个问题,否则不要回答这个问题。

let me start again. How do I use this class, which extends the internal Environment.GetSpecialFolder?

让我重新开始。我如何使用这个扩展了内部 Environment.GetSpecialFolder 的类?

IDon't want specialroots

我不想要特殊的根

root = Environment.GetFolderPath(Environment.SpecialFolder)

because i want to use this for other purposes Instead of .net.

因为我想将它用于其他目的而不是 .net。

for example how do I call (Favorites = 6) location by a button click?

例如,我如何通过单击按钮调用(收藏夹 = 6)位置?

public class EnvironmentFolders
{

public enum SpecialFolder
{
    AdministrativeTools = 48,
    //{user name}\Start Menu\Programs\Administrative Tools 
    ApplicationData = 26,
    //{user name}\Application Data 
    CommonAdministrativeTools = 47,
    //All Users\Start Menu\Programs\Administrative Tools 
    CommonApplicationData = 35,
    //All Users\Application Data 
    CommonDesktopDirectory = 25,
    //All Users\Desktop 
    CommonDocuments = 46,
    //All Users\Documents 
    CommonFavorites = 31,
    CommonNonLocalizedStartup = 30,
    //non localized common startup 
    CommonPrograms = 23,
    //All Users\Programs 
    CommonStartMenu = 22,
    //All Users\Start Menu 
    CommonStartup = 24,
    //All Users\Startup 
    CommonTemplates = 45,
    //All Users\Templates 
    ControlPanel = 3,
    //My Computer\Control Panel 
    Cookies = 33,
    DesktopDirectory = 16,
    //{user name}\Desktop 
    Favorites = 6,
    //{user name}\Favorites 
    Fonts = 20,
    //windows\fonts 
    History = 34,
    InternetCache = 32,
    LocalApplicationData = 28,
    //{user name}\Local Settings\Application Data (non roaming) 
    MyDocuments = 5,
    //My Documents 
    MyPictures = 39,
    //C:\Program Files\My Pictures 
    NetworkShortcuts = 19,
    //{user name}\nethood 
    NonLocalizedStartup = 29,
    //non localized startup 
    Printers = 4,
    //My Computer\Printers 
    PrintHood = 27,
    //{user name}\PrintHood 
    ProgramFiles = 38,
    //C:\Program Files 
    ProgramFilesCommon = 43,
    //C:\Program Files\Common 
    Programs = 2,
    //Start Menu\Programs 
    Recent = 8,
    //{user name}\Recent 
    RecycleBin = 10,
    //{desktop}\Recycle Bin 
    SendTo = 9,
    //{user name}\SendTo 
    StartMenu = 11,
    //{user name}\Start Menu 
    Startup = 7,
    //Start Menu\Programs\Startup 
    System = 37,
    //GetSystemDirectory() 
    Templates = 21,
    UserProfile = 40,
    //USERPROFILE 
    Windows = 36
    //GetWindowsDirectory() 
}

[DllImport("shfolder.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);

/// <summary> 
/// Get an environment folder path for Windows environment folders 
/// </summary> 
/// <returns>A string pointing to the special path</returns> 
/// <remarks></remarks> 
public static string GetPath(SpecialFolder folder)
{
    StringBuilder lpszPath = new StringBuilder(260);
    SHGetFolderPath(IntPtr.Zero, (int)folder, IntPtr.Zero, 0, lpszPath);
    return lpszPath.ToString();
}
}

采纳答案by Jeff Sternal

Edit: if you've inherited the code you've shown us and need to use it for some reason (instead of the built-in .NET method that appears to do the same thing), you should be able to use it like this:

编辑:如果您继承了向我们展示的代码并且出于某种原因需要使用它(而不是似乎执行相同操作的内置 .NET 方法),您应该能够像这样使用它:

string path = EnvironmentFolders.GetPath(EnvironmentFolders.SpecialFolders.Fonts);

Having said that, the Environmentclass has a public method that does nearly the same thing, GetFolderPath:

话虽如此,Environment该类有一个公共方法,几乎​​可以做同样的事情GetFolderPath

string path = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

The reflected code in .NETlooks exactly like the code in your class, except it adds two things: it verifies that the parameter value is defined in the enumeration (since you can pass any integer to the method) and it demands that the caller has path discovery permission (a new FileIOPermission). Is it that last requirement you're trying to work around?

.NET 中反射代码看起来与您的类中的代码完全一样,只是它增加了两件事:它验证参数值是否在枚举中定义(因为您可以将任何整数传递给方法),并且它要求调用者具有路径发现权限(一个新的FileIOPermission)。这是您试图解决的最后一个要求吗?

Both methods are static, meaning you access them through the type that contains them, not an instance of that type:

这两种方法都是static,这意味着您可以通过包含它们的类型访问它们,而不是该类型的实例:

 // Like this
 EnvironmentFolders.GetPath(...);

 // Not this
 EnvironmentFolders folders = new EnvironmentFolders();
 folders.GetPath(...);

See the .NET documentation about Static Classes and Static Class Membersfor more information.

有关详细信息,请参阅有关静态类和静态类成员的 .NET 文档。

回答by Priyank Bolia

string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

回答by gbianchi

Something like this added to the click event of your button.

像这样的东西添加到按钮的点击事件中。

String path = EnvironmentFolders.GetPath(EnvironmentFolders.SpecialFolder.Favorites)
//do something with the path.

You need more info??

你需要更多信息??

ADDED ON EDIT: Comments are right the method is static this way should work (at least, it worked when I try it on a console app).

添加编辑:评论是正确的方法是静态的,这种方式应该可以工作(至少,当我在控制台应用程序上尝试时它可以工作)。