1

My VB6 application is trying to write on a file in C:\ProgramData\ folder. The reason I do this is to let the users on the computer can write on the same file using my application and I know that ProgramData folder can store application data intended for all users to share.

Therefore,

C:\Users\[UserName]\AppData\Local
C:\Users\[UserName]\AppData\LocalLow
C:\Users\[UserName]\AppData\Roaming

These paths with specific user name will not be suitable for my application.

However, if I run as a Standard User in Windows 7, I found that I have no right to write on a file in ProgramData folder. Is there any other way I can do the task?

user2051823
  • 177
  • 1
  • 7
  • 23
  • have a configuration file (like .ini) for your application and designate the path in the configuration file. Let your application read this configuration file and do file reading/writing in the designated path – rags Aug 27 '13 at 10:12
  • 1
    Has the folder been set-up with permission for `Everyone`? – Paul Aug 27 '13 at 14:47
  • "Documents" are normally things a user will directly interact with, i.e. open them via clicking in Explorer and such. Programs shouldn't dictate a location to save such things to, but let the user navigate to a desired folder using a dialog. Then if they want to "share" they can save to a visible shared location like Public, some folder on their D: drive, etc. – Bob77 Aug 30 '13 at 14:27

2 Answers2

3

You really should be using SHGetSpecialFolderPath() API.

Put this into a module (or a form and change it to private):

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

Then you can call this GetSpecialFolder() with any of the defines here:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx

This ensures it's compatible with every operating system. To get the value of each define, just look it up on Google or check Win32 API tool. Here's a bunch:

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

You likely want CSIDL_APPDATA or CSIDL_COMMON_APPDATA.

Mike Weir
  • 3,094
  • 1
  • 30
  • 46
  • Actually I have referred to the following article (http://stackoverflow.com/questions/4273424/where-should-i-store-application-specific-settings) to retrieve the ProgramData path, but the situation I am facing is that: as a standard user, I got no permission to write on a ProgramData folder. – user2051823 Aug 28 '13 at 02:26
  • 2
    Try: http://blogs.msdn.com/b/cjacks/archive/2008/12/04/how-to-set-directory-permissions-at-install-time-using-an-msi-created-using-windows-installer-xml-wix.aspx – Mark Bertenshaw Aug 28 '13 at 07:54
  • @Mark - You have the answer there, but you can do this with other tools besides WiX as well. I also hope he isn't dumping crap right into that folder and instead creates proper subfolders for his application. – Bob77 Aug 30 '13 at 05:43
  • @Bob77 - Agreed. I see way too much incorrect use of the Windows folder structure - even in mainstream applications. – Mark Bertenshaw Aug 30 '13 at 07:39
0

The following path is both available for all users and writable even without any special permissions:

C:\Users\Public
E Mett
  • 2,272
  • 3
  • 18
  • 37