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

问题描述

请你只回答
otherways没有对这个问题作出回应。



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



。我不希望specialroots

 根= Environment.GetFolderPath(Environment.SpecialFolder)

由于我要使用此用于其它目的而不是.NET的。



例如我怎么叫(收藏夹= 6)通过一个按钮的位置单击?

 公共类EnvironmentFolders 
{

公共枚举SpecialFolder
{
AdministrativeTools = 48,
// {用户名} \Start Menu\Programs\Administrative工具
的ApplicationData = 26,
// {用户名} \Application数据
CommonAdministrativeTools = 47 ,
//所有Users\Start Menu\Programs\Administrative工具
CommonApplicationData = 35,
//所有Users\Application数据
CommonDesktopDirectory = 25,
//所有Users\Desktop
CommonDocuments = 46,
//所有Users\Documents
CommonFavorites = 31,
CommonNonLocalizedStartup = 30,
//非本地化的共同启动
CommonPrograms = 23,
//所有Users\Programs
CommonStartMenu = 22,
//所有Users\Start菜单
CommonStartup = 24 ,
//所有Users\Startup
CommonTemplates = 45,
//所有Users\Templates
ControlPanel控制= 3,
//我Computer\Control面板
饼干= 33,
DesktopDirectory = 16,
// {用户名} \Desktop
最爱= 6,
// {用户名} \收藏
字体= 20,
// windows\fonts
记录= 34,
InternetCache = 32,
LocalApplicationData = 28,
// {用户名} \Local Settings\Application数据(非漫游)
我的文档= 5,
//我的文档
MyPictures = 39,
// C:\Program Files\My图片
NetworkShortcuts = 19,
// {用户名} \\\
ethood
NonLocalizedStartup = 29,
//非本地化启动
打印机= 4 ,
//我Computer\Printers
PRINTHOOD = 27,
// {用户名} \PrintHood
PROGRAMFILES = 38,
// C:\\ \\Program文件
ProgramFilesCommon = 43,
// C:\Program Files\Common
程序= 2,
//开始Menu\Programs
最近= 8,
// {用户名} \Recent
RECYCLEBIN = 10,
// {}桌面斌\Recycle
的SendTo = 9,
/ / {用户名} \SendTo
的StartMenu = 11,
// {用户名} \Start菜单
启动= 7,
//开始Menu\Programs\\ \\Startup
系统= 37,
// GetSystemDirectory()
模板= 21,
用户配置= 40,
// USERPROFILE
的Windows = 36
// GetWindowsDirectory()
}

函数[DllImport(Shfolder.dll中,字符集= CharSet.Auto)]
私人静态外部INT的SHGetFolderPath(IntPtr的hwndOwner, INT n文件夹,IntPtr的hToken,诠释的dwFlags,StringBuilder的lpszPath);

///<总结>
///获取Windows环境下的文件夹
///℃的环境中的文件夹路径; /总结>
///<退货和GT;指向特殊的路径474上的字符串; /回报>
///<&言论GT;< /言论>
公共静态字符串的getPath(SpecialFolder文件夹)
{
StringBuilder的lpszPath =新的StringBuilder(260);
的SHGetFolderPath(IntPtr.Zero,(INT)文件夹,IntPtr.Zero,0,lpszPath);
返回lpszPath.ToString();
}
}


解决方案

编辑:如果你继承你已经告诉我们,需要使用它的一些原因,代码(而不是内置,似乎做同样的事情.NET方法),你应该能够使用这样的:

 路径字符串= EnvironmentFolders.GetPath(EnvironmentFolders.SpecialFolders.Fonts); 



话虽如此,在环保类有,做几乎同样的事情的公共方法,:

 路径字符串= Environment.GetFolderPath(Environment.SpecialFolder.Favorites); 



的反映的代码看起来很像在你的类中的代码,但它增加了两件事情:它验证参数值在枚举定义(因为你可以传递给方法的任何整数)它要求调用者有路径发现权限(一个新的的FileIOPermission )。难道你想解决这最后的要求?



这两种方法的静态,这意味着你通过类型访问它们包含它们,不是那种类型的实例:

  //筛选
EnvironmentFolders.GetPath(...);

//不是这个
EnvironmentFolders文件夹=新EnvironmentFolders();
folders.GetPath(...);

请参阅有关的了解详情。


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?

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 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();
}
}
解决方案

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.

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

09-06 01:38