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

前端 未结 6 953
日久生厌
日久生厌 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 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
    

提交回复
热议问题