如何引用C:\Users\Public目录c#编程吗

时间:2022-06-28 11:20:11

Is it safe to programmatically reference the public folder through:

通过以下方式以编程方式引用公共文件夹安全吗?

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

or is there a better way?

或者有更好的方法吗?

Again, what if someone deletes the environment variable for public, and is this safe to use for different language OSs?

同样,如果有人将环境变量删除了,并且对于不同语言的OSs使用安全吗?

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

下面是:如何从VS 2010部署设置项目安装到Windows 7中的公共目录。

6 个解决方案

#1


10  

It depends on what you want to achieve. There is a enum called SpecialFolder. You can use it to get the Path to some Directories. For Example:

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

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

points to "C:\Users\Public\Desktop".

指向“C:\ \公共\用户桌面”。

IMHO, your way isn't wrong, though i would do some Exception Handling in case the EnvVar is really missing. Also you could use the ENUM with "CommonDesktopDirectory" and get rid of the "\Desktop" part.

IMHO,您的方法并没有错,但是如果EnvVar真的丢失了,我会做一些异常处理。另外,您还可以使用“CommonDesktopDirectory”,并删除“\Desktop”部分。

#2


10  

This seems a tad questionable, but it should work:

这似乎有点可疑,但应该奏效:

// 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;

#3


5  

Notice that the Environment.SpecialFolder.CommonDesktopDirectory is only available in .NET 4.0. For my .NET 3.5 systems (Windows 7 or XP) I used the registry key for the Shell Folders. My code snippet is in VB.NET.

注意到Environment.SpecialFolder。CommonDesktopDirectory仅在。net 4.0中可用。对于我的。net 3.5系统(Windows 7或XP),我使用了Shell文件夹的注册表键。我的代码片段在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


4  

Have you looked at this ?

你看过这个吗?

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

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

Specifies enumerated constants used to retrieve directory paths to system special folders.

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

Ie

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

#5


3  

If you want a place to put application-specific data that can accessed by all users, use as a base:

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

Environment.GetFolderPath(SpecialFolder.CommonApplicationData)

Also, consider using Path.Combine to combine elements to form a new path:

同时,考虑使用路径。结合元素形成新的路径:

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

#6


1  

You can get all these %wildcard% literals by looking into

通过查找,可以获得所有这些%通配符%。

Windows->Start-->regedit-->

窗口- >开始- - >注册表编辑器- - >

如何引用C:\Users\Public目录c#编程吗

Then, you perform

然后,你执行

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

string path2Music = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Music");

... and, so on .... and to test:

…因此在....和测试:

using System.IO;

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

#1


10  

It depends on what you want to achieve. There is a enum called SpecialFolder. You can use it to get the Path to some Directories. For Example:

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

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

points to "C:\Users\Public\Desktop".

指向“C:\ \公共\用户桌面”。

IMHO, your way isn't wrong, though i would do some Exception Handling in case the EnvVar is really missing. Also you could use the ENUM with "CommonDesktopDirectory" and get rid of the "\Desktop" part.

IMHO,您的方法并没有错,但是如果EnvVar真的丢失了,我会做一些异常处理。另外,您还可以使用“CommonDesktopDirectory”,并删除“\Desktop”部分。

#2


10  

This seems a tad questionable, but it should work:

这似乎有点可疑,但应该奏效:

// 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;

#3


5  

Notice that the Environment.SpecialFolder.CommonDesktopDirectory is only available in .NET 4.0. For my .NET 3.5 systems (Windows 7 or XP) I used the registry key for the Shell Folders. My code snippet is in VB.NET.

注意到Environment.SpecialFolder。CommonDesktopDirectory仅在。net 4.0中可用。对于我的。net 3.5系统(Windows 7或XP),我使用了Shell文件夹的注册表键。我的代码片段在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


4  

Have you looked at this ?

你看过这个吗?

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

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

Specifies enumerated constants used to retrieve directory paths to system special folders.

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

Ie

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

#5


3  

If you want a place to put application-specific data that can accessed by all users, use as a base:

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

Environment.GetFolderPath(SpecialFolder.CommonApplicationData)

Also, consider using Path.Combine to combine elements to form a new path:

同时,考虑使用路径。结合元素形成新的路径:

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

#6


1  

You can get all these %wildcard% literals by looking into

通过查找,可以获得所有这些%通配符%。

Windows->Start-->regedit-->

窗口- >开始- - >注册表编辑器- - >

如何引用C:\Users\Public目录c#编程吗

Then, you perform

然后,你执行

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

string path2Music = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Music");

... and, so on .... and to test:

…因此在....和测试:

using System.IO;

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