【问题标题】:How to reference the C:\Users\Public directory programmatically in C#如何在 C# 中以编程方式引用 C:\Users\Public 目录
【发布时间】:2011-06-06 16:40:39
【问题描述】:

通过以下方式以编程方式引用公用文件夹是否安全:

Directory = System.Environment.GetEnvironmentVariable("public")+"MyCompanyName" // etc.

或者有更好的方法吗?

同样,如果有人删除了 public 的环境变量怎么办,这对于不同语言的操作系统是否安全?

如下:How to install to the Public directory in Windows 7 from the VS 2010 deployment Setup Project

【问题讨论】:

  • 作为旁注,我建议使用Path.Combine 作为目录连接的对立面。
  • 您是否担心与 Windows XP 的向后兼容性? %public% 环境变量似乎是随 Vista 引入的; WinXP 有一个 %allusersprofile% 环境变量,它指向 %systemdrive%\Documents and Settings\All Users 但 %public% 未定义。
  • 感谢您的信息;不,这纯粹是针对 Vista/7 的。

标签: c# .net environment-variables


【解决方案1】:

这似乎有点问题,但它应该工作:

// This should give you something like C:\Users\Public\Documents
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);

var directory = new DirectoryInfo(documentsPath);

// Now this should give you something like C:\Users\Public
string commonPath = directory.Parent.FullName;

【讨论】:

  • 感谢您的信息。不幸的是,我的项目目前的目标是 .Net 框架的 3.5,它不排除 CommonDocuments 枚举成员(我们希望将旧软件和与 Windows 7 兼容的新软件之间的更改保持在最低限度)。
【解决方案2】:

这取决于您想要达到的目标。 有一个名为SpecialFolder 的枚举。您可以使用它来获取某些目录的路径。 例如:

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)

指向“C:\Users\Public\Desktop”。

恕我直言,你的方式没有错,但我会做一些异常处理以防 EnvVar 真的丢失。 您也可以将 ENUM 与“CommonDesktopDirectory”一起使用并去掉“\Desktop”部分。

【讨论】:

  • 不知道为什么这被标记为答案,甚至在错误的时候投了赞成票。 CommonDesktopDirectory 不是用于 VS2008 的 .Net 3.5 的有效枚举,这就是问题的标记方式。
  • 显然问题是找到目录“C/Users/Public”
【解决方案3】:

请注意,Environment.SpecialFolder.CommonDesktopDirectory 仅在 .NET 4.0 中可用。对于我的 .NET 3.5 系统(Windows 7 或 XP),我使用了 Shell 文件夹的注册表项。我的代码 sn-p 在 VB.NET 中。

Private mRegShellPath="Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Private mCommonDesktop = Nothing

' dgp rev 3/8/2012
Private ReadOnly Property CommonDesktop As String
    Get
        If mCommonDesktop Is Nothing Then
            Dim RegKey As RegistryKey
            Try
                RegKey = Registry.LocalMachine.OpenSubKey(mRegShellPath, False)
                mCommonDesktop = RegKey.GetValue("Common Desktop")
            Catch ex As Exception
                mCommonDesktop = ""
            End Try
        End If

        Return mCommonDesktop
    End Get

End Property

【讨论】:

    【解决方案4】:

    如果您想要一个地方放置所有用户都可以访问的特定于应用程序的数据,请用作基础:

    Environment.GetFolderPath(SpecialFolder.CommonApplicationData)
    

    另外,考虑使用Path.Combine 组合元素形成新路径:

    Path.Combine(
        Environment.GetFolderPath(SpecialFolder.CommonApplicationData),
        "MyCompanyName")
    

    【讨论】:

    • 感谢您的信息。是的,这是所有用户都可以访问的数据,但在 Windows 资源管理器中也应该对他们可见,即他们可以双击并直接打开这些文件,因此我认为 C:\ProgramData 不适合,因为这是一个隐藏文件夹。
    • 注意另一个潜在问题 ProgramData 文件夹的权限是有限的,例如stackoverflow.com/questions/22107812/…
    【解决方案5】:

    你看过这个吗?

    http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

    指定用于检索系统特殊文件夹的目录路径的枚举常量。

    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
    

    【讨论】:

      【解决方案6】:

      您可以通过查看所有这些 %wildcard% 文字

      Windows->开始-->regedit-->

      然后,你执行

      using System;
      string path2Downloads = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Downloads");
      
      string path2Music = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Music");
      

      ... 等等 .... 并进行测试:

      using System.IO;
      
      string[] files = { "" };
      if (Directory.Exists(path2Music)) {
          files = Directory.GetFiles(path2Music);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多