我的VB6应用程序正在尝试在C:\ ProgramData \文件夹中写入文件。我这样做的原因是让计算机上的用户可以使用我的应用程序在同一个文件上写入,我知道ProgramData文件夹可以存储供所有用户共享的应用程序数据。
因此,
C:\Users\[UserName]\AppData\Local
C:\Users\[UserName]\AppData\LocalLow
C:\Users\[UserName]\AppData\Roaming
具有特定用户名的这些路径将不适合我的应用程序。
但是,如果我在Windows 7中作为标准用户运行,我发现我无权在ProgramData文件夹中写入文件。 还有其他方法可以完成任务吗?
答案 0 :(得分:3)
您确实应该使用SHGetSpecialFolderPath()
API。
将其放入模块(或表单并将其更改为私有):
Private Declare Function SHGetSpecialFolderPath Lib "SHELL32.DLL" Alias "SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, ByVal pszPath As String, ByVal nFolder As Long, ByVal fCreate As Long) As Long
Public Function GetSpecialFolder(ByVal csidl As Long) As String
Dim X As Long
GetSpecialFolder = Space$(261&)
If SHGetSpecialFolderPath(0&, GetSpecialFolder, csidl, 1&) Then
GetSpecialFolder = Left$(GetSpecialFolder, InStr(1&, GetSpecialFolder, vbNullChar, vbBinaryCompare) - 1&)
Else
Err.Raise -1&, , "Could not find special folder " & csidl & "."
End If
End Function
然后,您可以使用以下任何定义调用此GetSpecialFolder()
:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx
这确保了它与每个操作系统兼容。要获取每个定义的值,只需在Google上查找或查看Win32 API工具即可。这是一堆:
Public Const CSIDL_DESKTOP As Long = &H0
Public Const CSIDL_INTERNET As Long = &H1
Public Const CSIDL_PROGRAMS As Long = &H2
Public Const CSIDL_CONTROLS As Long = &H3
Public Const CSIDL_PRINTERS As Long = &H4
Public Const CSIDL_PERSONAL As Long = &H5
Public Const CSIDL_FAVORITES As Long = &H6
Public Const CSIDL_STARTUP As Long = &H7
Public Const CSIDL_RECENT As Long = &H8
Public Const CSIDL_SENDTO As Long = &H9
Public Const CSIDL_BITBUCKET As Long = &HA
Public Const CSIDL_STARTMENU As Long = &HB
Public Const CSIDL_MYDOCUMENTS As Long = &HC
Public Const CSIDL_MYMUSIC As Long = &HD
Public Const CSIDL_MYVIDEO As Long = &HE
Public Const CSIDL_UNUSED1 As Long = &HF '&HF not currently implemented
Public Const CSIDL_DESKTOPDIRECTORY As Long = &H10
Public Const CSIDL_DRIVES As Long = &H11
Public Const CSIDL_NETWORK As Long = &H12
Public Const CSIDL_NETHOOD As Long = &H13
Public Const CSIDL_FONTS As Long = &H14
Public Const CSIDL_TEMPLATES As Long = &H15
Public Const CSIDL_COMMON_STARTMENU As Long = &H16
Public Const CSIDL_COMMON_PROGRAMS As Long = &H17
Public Const CSIDL_COMMON_STARTUP As Long = &H18
Public Const CSIDL_COMMON_DESKTOPDIRECTORY As Long = &H19
Public Const CSIDL_APPDATA As Long = &H1A
Public Const CSIDL_PRINTHOOD As Long = &H1B
Public Const CSIDL_LOCAL_APPDATA As Long = &H1C
Public Const CSIDL_ALTSTARTUP As Long = &H1D
Public Const CSIDL_COMMON_ALTSTARTUP As Long = &H1E
Public Const CSIDL_COMMON_FAVORITES As Long = &H1F
Public Const CSIDL_INTERNET_CACHE As Long = &H20
Public Const CSIDL_COOKIES As Long = &H21
Public Const CSIDL_HISTORY As Long = &H22
Public Const CSIDL_COMMON_APPDATA As Long = &H23
Public Const CSIDL_WINDOWS As Long = &H24
Public Const CSIDL_SYSTEM As Long = &H25
Public Const CSIDL_PROGRAM_FILES As Long = &H26
Public Const CSIDL_MYPICTURES As Long = &H27
Public Const CSIDL_PROFILE As Long = &H28
Public Const CSIDL_SYSTEMX86 As Long = &H29 'RISC only
Public Const CSIDL_PROGRAM_FILESX86 As Long = &H2A 'RISC only
Public Const CSIDL_PROGRAM_FILES_COMMON As Long = &H2B
Public Const CSIDL_PROGRAM_FILES_COMMONX86 As Long = &H2C 'RISC only
Public Const CSIDL_COMMON_TEMPLATES As Long = &H2D
Public Const CSIDL_COMMON_DOCUMENTS As Long = &H2E
Public Const CSIDL_COMMON_ADMINTOOLS As Long = &H2F
Public Const CSIDL_ADMINTOOLS As Long = &H30
Public Const CSIDL_CONNECTIONS As Long = &H31
Public Const CSIDL_COMMON_MUSIC As Long = &H35
Public Const CSIDL_COMMON_PICTURES As Long = &H36
Public Const CSIDL_COMMON_VIDEO As Long = &H37
Public Const CSIDL_RESOURCES As Long = &H38
Public Const CSIDL_RESOURCES_LOCALIZED As Long = &H39
Public Const CSIDL_COMMON_OEM_LINKS As Long = &H3A
Public Const CSIDL_CDBURN_AREA As Long = &H3B
Public Const CSIDL_UNUSED2 As Long = &H3C '&H3C not currently implemented
Public Const CSIDL_COMPUTERSNEARME As Long = &H3D
您可能需要CSIDL_APPDATA
或CSIDL_COMMON_APPDATA
。
答案 1 :(得分:0)
以下路径既可供所有用户使用,也可写,即使没有任何特殊权限:
C:\Users\Public