简体   繁体   English

如何访问“文档和设置”文件夹?

[英]How can I access the "Documents and Settings" folder?

I'm using C# .NET 4 with VS 2010.我在 VS 2010 中使用 C# .NET 4。

When Iterating over some paths, I'm running this line:当迭代某些路径时,我正在运行这一行:

files = Directory.GetFiles(path, searchPattern);

I get an exception when the path is the documents and settings folder.当路径是文档和设置文件夹时,我得到一个例外。 How can I access it?我怎样才能访问它? And no, I don't want to skip the folder with a try and catch.不,我不想通过尝试和捕捉来跳过文件夹。 I want to be able to access it somehow.我希望能够以某种方式访问​​它。

Edit: I got a follow up question.编辑:我有一个后续问题。 As I told you, I'm iterating over the paths.正如我告诉你的,我正在遍历路径。 Is there a way to use Environment.GetFolderPath but somehow idetifying the correct speical folder according to the path I'm currently checking?有没有办法使用Environment.GetFolderPath但是根据我当前正在检查的路径以某种方式确定正确的特殊文件夹?

You have to use like this你必须像这样使用

var mydocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

To get access to the MyDocuments folder.访问MyDocuments文件夹。

From Environment.SpecialFolder来自Environment.SpecialFolder

The system special folders are folders such as Program Files, Programs, System, or Startup, which contain common information.系统特殊文件夹是诸如 Program Files、Programs、System 或 Startup 之类的文件夹,其中包含公共信息。 Special folders are set by default by the system, or explicitly by the user, when installing a version of Windows.在安装某个版本的 Windows 时,系统会默认设置特殊文件夹,或由用户明确设置。

The GetFolderPath method returns the locations associated with this enumeration. GetFolderPath 方法返回与此枚举关联的位置。 The locations of these folders can have different values on different operating systems, the user can change some of the locations, and the locations are localized.这些文件夹的位置在不同的操作系统上可以有不同的值,用户可以更改一些位置,并且这些位置是本地化的。

Just use只需使用

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
files = Directory.GetFiles(path, searchPattern);

In my computer, it returns as C:\\Users\\Soner\\Documents在我的电脑中,它返回为C:\\Users\\Soner\\Documents

Is there a way to use Environment.GetFolderPath but somehow idetifying the correct speical folder according to the path I'm currently checking?有没有办法使用 Environment.GetFolderPath 但是根据我当前正在检查的路径以某种方式确定正确的特殊文件夹?

Since SpecialFolder is enum type, you can iterate their values in a loop.由于SpecialFolder是枚举类型,您可以在循环中迭代它们的值。 Here how it looks like;这是它的样子;

public enum SpecialFolder
{
    AdminTools = 0x30,
    ApplicationData = 0x1a,
    CDBurning = 0x3b,
    CommonAdminTools = 0x2f,
    CommonApplicationData = 0x23,
    CommonDesktopDirectory = 0x19,
    CommonDocuments = 0x2e,
    CommonMusic = 0x35,
    CommonOemLinks = 0x3a,
    CommonPictures = 0x36,
    CommonProgramFiles = 0x2b,
    CommonProgramFilesX86 = 0x2c,
    CommonPrograms = 0x17,
    CommonStartMenu = 0x16,
    CommonStartup = 0x18,
    CommonTemplates = 0x2d,
    CommonVideos = 0x37,
    Cookies = 0x21,
    Desktop = 0,
    DesktopDirectory = 0x10,
    Favorites = 6,
    Fonts = 20,
    History = 0x22,
    InternetCache = 0x20,
    LocalApplicationData = 0x1c,
    LocalizedResources = 0x39,
    MyComputer = 0x11,
    MyDocuments = 5,
    MyMusic = 13,
    MyPictures = 0x27,
    MyVideos = 14,
    NetworkShortcuts = 0x13,
    Personal = 5,
    PrinterShortcuts = 0x1b,
    ProgramFiles = 0x26,
    ProgramFilesX86 = 0x2a,
    Programs = 2,
    Recent = 8,
    Resources = 0x38,
    SendTo = 9,
    StartMenu = 11,
    Startup = 7,
    System = 0x25,
    SystemX86 = 0x29,
    Templates = 0x15,
    UserProfile = 40,
    Windows = 0x24
}

You can set the program so you can only run as administrator.您可以将程序设置为只能以管理员身份运行。

In Visual Studio:在 Visual Studio 中:

Right click on the Project -> Properties -> Security -> Enable ClickOnce Security右键单击项目 -> 属性 -> 安全性 -> 启用 ClickOnce 安全性

After you clicked it, a file will be created under the Project's properties folder called app.manifest once this is created, you can uncheck the Enable ClickOnce Security Settings option单击它后,将在项目的属性文件夹下创建一个名为 app.manifest 的文件,一旦创建该文件,您可以取消选中启用 ClickOnce 安全设置选项

Open that file and change this line :打开该文件并更改此行:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to:到:

 <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

This will make the program require administrator privileges, and it will guarantee you have access to that folder.这将使程序需要管理员权限,并保证您可以访问该文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Documents and Settings文件夹出现拒绝访问错误? - Why am I getting an access denied error for the Documents and Settings folder? 如何使用C#访问iOS App的Documents文件夹 - How can I access Documents folder of iOS App with C# 如何访问文档文件夹(UWP) - How do I gain access to the documents folder (UWP) 在AtTask中,如何获取不在文件夹中的文档列表? - In AtTask, how can I get a list of documents not in a folder? 我如何访问未共享的文件夹 - How can i get access to unshared folder 如何访问/读取/写入Android设备上内部存储中的Documents文件夹? - How do I access/read from/write to Documents folder in Internal Storage on Android devices? 如何将设置保存到应用程序设置? - How i can save settings to the application Settings? 如何在不使用每个应用程序将更新保存在其自己的文件夹中的情况下从一个公共程序集中共享一个公共用户配置(settings.settings)? - How can I share a common user config (settings.settings) from a common assembly without each app saving updates in their own folder? 何时清除Documents and Settings \ USER \ Local Settings \ Temp文件夹? - When is the Documents and Settings\USER\Local Settings\Temp folder cleared? C#,是否可以在Vista中访问文档和设置? - C#, getting access to Documents and Settings in Vista?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM