Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

c# - Get Special Folder

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

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

IDon't want specialroots

root = Environment.GetFolderPath(Environment.SpecialFolder)

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

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

public class EnvironmentFolders
{

public enum SpecialFolder
{
    AdministrativeTools = 48,
    //{user name}Start MenuProgramsAdministrative Tools 
    ApplicationData = 26,
    //{user name}Application Data 
    CommonAdministrativeTools = 47,
    //All UsersStart MenuProgramsAdministrative Tools 
    CommonApplicationData = 35,
    //All UsersApplication Data 
    CommonDesktopDirectory = 25,
    //All UsersDesktop 
    CommonDocuments = 46,
    //All UsersDocuments 
    CommonFavorites = 31,
    CommonNonLocalizedStartup = 30,
    //non localized common startup 
    CommonPrograms = 23,
    //All UsersPrograms 
    CommonStartMenu = 22,
    //All UsersStart Menu 
    CommonStartup = 24,
    //All UsersStartup 
    CommonTemplates = 45,
    //All UsersTemplates 
    ControlPanel = 3,
    //My ComputerControl Panel 
    Cookies = 33,
    DesktopDirectory = 16,
    //{user name}Desktop 
    Favorites = 6,
    //{user name}Favorites 
    Fonts = 20,
    //windowsfonts 
    History = 34,
    InternetCache = 32,
    LocalApplicationData = 28,
    //{user name}Local SettingsApplication Data (non roaming) 
    MyDocuments = 5,
    //My Documents 
    MyPictures = 39,
    //C:Program FilesMy Pictures 
    NetworkShortcuts = 19,
    //{user name}
ethood 
    NonLocalizedStartup = 29,
    //non localized startup 
    Printers = 4,
    //My ComputerPrinters 
    PrintHood = 27,
    //{user name}PrintHood 
    ProgramFiles = 38,
    //C:Program Files 
    ProgramFilesCommon = 43,
    //C:Program FilesCommon 
    Programs = 2,
    //Start MenuPrograms 
    Recent = 8,
    //{user name}Recent 
    RecycleBin = 10,
    //{desktop}Recycle Bin 
    SendTo = 9,
    //{user name}SendTo 
    StartMenu = 11,
    //{user name}Start Menu 
    Startup = 7,
    //Start MenuProgramsStartup 
    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();
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

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

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

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?

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...