Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the document directory in XP

Status
Not open for further replies.

JohnVogel

Programmer
Apr 19, 1999
117
US
Hi, I need to know the location of the users document folder, in order to manipulate the ntuser.dat file (for system backup). Can somebody tell me hot to get the location of the system folder (eq:"C:\Documents and Settings\Username.COMPUTER" or "C:\Documents and Settings\UserName")


TIA

John Vogel
john@thecompuwizard.com
 
Do a search in this forum for SHGetSpecialFolderLocation API.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Everyone keeps wanting to do it the hard way...I keep posting this whenever ShGetSpecialFolderLocation rears it's ugly head:

Here's the easy way in XP:

Code:
' This version not designed to work with W95/W98/NT4
' Requires reference to Microsoft Shell Controls and Automation
Public Function GetSpecialPath(vDir As ShellSpecialFolderConstants) As String
    With New Shell32.Shell
         GetSpecialPath = .NameSpace(vDir).Self.Path
    End With
End Function
And this can be called like:
Code:
MsgBox GetSpecialPath(ssfPERSONAL)
 
Thanks! That did it. Now, one more question... how can someone copy a file that is in use (like the ntuser.dat)? Is there anyway?

John Vogel
john@thecompuwizard.com
 
The way I normally do it is to use setupapi.dll to configure such files to be copied at the next reboot (i.e when the file is not in use), but I don't think this solution will help you make a backup...
 
Hmm, I guess for thiose interested in the technique I should post my code:
Code:
Option Explicit

Private Declare Function SetupInstallFile Lib "setupapi.dll" Alias "SetupInstallFileA" (ByVal InfHandle As Long, ByVal InfContext As Long, ByVal SourceFile As String, ByVal SourcePath As String, ByVal Destination As String, ByVal CopyStyle As Long, ByVal CopyMessageHandler As Long, Context As Long) As Long
Private Const SP_COPY_FORCE_IN_USE = &H200&
Private Const SP_COPY_SOURCE_ABSOLUTE = &H40&
Private Const SP_COPY_NODECOMP = &H10&
Private Const SP_COPY_SOURCEPATH_ABSOLUTE = &H80&

Private Sub Command1_Click()
    Debug.Print CopyAtReboot("C:\test.txt", "C:\Check\test.txt")
End Sub

' Copies file from source to destination. If file exists, copy is delayed until reboot.
' This theoretically allows system files to be replaced.
Private Function CopyAtReboot(strSourceFile As String, strDestinationFile As String) As Boolean
    CopyAtReboot = SetupInstallFile(0&, 0&, strSourceFile, "who_cares", strDestinationFile, SP_COPY_NODECOMP + SP_COPY_SOURCE_ABSOLUTE + SP_COPY_FORCE_IN_USE, 0&, vbNull)
End Function
 
That's some interesting code, but how do you know whether or not it "took". I mean if the source directory/file does not exist, will it return an error.. or if it doesn't copy correctly, or whatever... or do you pretty much have to make sure the file checking is done in your code prior to, and after doing the "CopyAtReboot"? The above code might just work for me, but I'm really not sure how exactly it works...

Thanks again :)

I just wish there was some way of telling Windows XP to ignore the "file in use" lock, and just copy it! I kind of remember doing this once, in Windows NT, but I can't for the life of me remember WHAT I did...

John Vogel
john@thecompuwizard.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top