获取特殊文件夹 [英] Get Special Folder

查看:59
本文介绍了获取特殊文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请回答这个问题
否则不回答此问题。

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

让我重新开始。我该如何使用此类扩展内部Environment.GetSpecialFolder?

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

ID不需要特殊根

root = Environment.GetFolderPath(Environment.SpecialFolder)

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

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

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

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

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


推荐答案

编辑:如果您继承了向我们展示的代码,并且由于某种原因需要使用它(而不是看起来像在做同样事情的内置.NET方法),则应该可以像这样使用它:

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:

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

话虽如此, Environment 类具有一个执行几乎相同操作的公共方法, GetFolderPath

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

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

。NET 中反映的代码与类中的代码完全相同,不同之处在于它添加了两点:验证参数值是否在枚举中定义(因为您可以传递任何整数并要求调用者具有路径发现权限(新的 FileIOPermission )。

The reflected code in .NET looks 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?

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

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

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

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

请参阅有关.NET文档静态类和静态类成员以获取更多信息。

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

这篇关于获取特殊文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆