【问题标题】:How can I access the "Documents and Settings" folder?如何访问“文档和设置”文件夹?
【发布时间】:2013-05-21 02:12:11
【问题描述】:

我在 VS 2010 中使用 C# .NET 4。

当迭代一些路径时,我正在运行这一行:

files = Directory.GetFiles(path, searchPattern);

当路径是文档和设置文件夹时出现异常。我怎样才能访问它?不,我不想尝试跳过该文件夹。我希望能够以某种方式访问​​它。

编辑:我有一个后续问题。正如我告诉你的,我正在迭代路径。有没有办法使用Environment.GetFolderPath 但根据我当前检查的路径以某种方式识别正确的特殊文件夹?

【问题讨论】:

    标签: c# path access-denied getfiles


    【解决方案1】:

    你必须这样使用

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

    访问MyDocuments 文件夹。

    【讨论】:

    • 我编辑了我的问题以添加一个后续问题,如果您也知道答案的话。
    • 您可以很好地将您拥有的路径与此 Environment.GetFolderPath(...) 等同起来,如果匹配,您可以继续,否则您可以在循环结构中调用 Continue
    • 我的意思是自动的。类似于(伪代码):SpeicalFolder 文件夹 = Environment.GetSpeicalFolderByFullPath(path);
    • No.. Environment.GetFolderPath 仅将枚举作为路径。不是字符串。
    • 我想建议你以下。在应用程序启动期间,您可以只缓存所有特殊文件夹的完整路径,然后在运行时检查它们。让我知道这是否符合您的要求。
    【解决方案2】:

    来自Environment.SpecialFolder

    系统专用文件夹为Program Files、 包含公共信息的程序、系统或启动。 特殊文件夹由系统默认设置,或由 用户,在安装 Windows 版本时。

    GetFolderPath 方法返回与此关联的位置 枚举。这些文件夹的位置可以有不同的值 在不同的操作系统上,用户可以更改一些 位置,并且位置是本地化的。

    随便用

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

    在我的电脑中,它返回为C:\Users\Soner\Documents

    有没有办法使用 Environment.GetFolderPath 但不知何故 根据我目前的路径正确的特殊文件夹 检查?

    由于SpecialFolder 是枚举类型,您可以在循环中迭代它们的值。这是它的样子;

    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
    }
    

    【讨论】:

    • 我编辑了我的问题以添加一个后续问题,如果您也知道答案的话。
    • 有没有办法自动完成?类似(伪代码):SpeicalFolder 文件夹 = Environment.GetSpeicalFolderByFullPath(path);另外,还有一点不明白。抛出异常的路径是 C:\\Documents and Settings,环境给我的路径是 C:\\Users\\Yonatan\\Documents。如何在运行时比较路径以知道引发异常的路径实际上应该是另一个特殊路径?我不能只做 if(path == "C:\\Documents and Settings") 因为它可能会在不同的计算机之间发生变化。
    【解决方案3】:

    您可以设置程序,使您只能以管理员身份运行。

    在 Visual Studio 中:

    右击项目->属性->安全->启用ClickOnce安全

    点击后,项目的属性文件夹下会创建一个名为 app.manifest 的文件,一旦创建,您可以取消选中启用 ClickOnce 安全设置选项

    打开该文件并更改这一行:

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

    到:

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

    这将使程序需要管理员权限,并保证您可以访问该文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2011-08-25
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      相关资源
      最近更新 更多