获取特殊文件夹

时间:2009-12-22 15:44:13

标签: c#

请回答问题 其他人不回答这个问题。

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

我不想要特别节目

root = Environment.GetFolderPath(Environment.SpecialFolder)

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

例如,如何通过单击按钮调用(Favorites = 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();
}
}

3 个答案:

答案 0 :(得分:37)

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

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

话虽如此,Environment类的公共方法几乎完全相同,GetFolderPath

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

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

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

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

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

有关详细信息,请参阅有关Static Classes and Static Class Members的.NET文档。

答案 1 :(得分:8)

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

答案 2 :(得分:2)

这样的内容会添加到按钮的点击事件中。

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

您需要更多信息??

ADDED ON EDIT:评论是正确的,方法是静态的,这种方式应该起作用(至少,当我在控制台应用程序上尝试时它起作用)。