How to reference the C:\Users\Public directory programmatically in C#

前端 未结 6 907
日久生厌
日久生厌 2021-01-04 00:33

Is it safe to programmatically reference the public folder through:

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


        
相关标签:
6条回答
  • 2021-01-04 00:44

    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:

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

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

    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.

    0 讨论(0)
  • 2021-01-04 00:53

    Have you looked at this ?

    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)
    
    0 讨论(0)
  • 2021-01-04 00:55

    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;
    
    0 讨论(0)
  • 2021-01-04 01:04

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

    Windows->Start-->regedit-->

    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);
    }
    
    0 讨论(0)
  • 2021-01-04 01:06

    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")
    
    0 讨论(0)
  • 2021-01-04 01:11

    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.

    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
    
    0 讨论(0)
提交回复
热议问题