Re: copying files w/ GetWindowsDirectoryPosted by webmaster Guido on November 06, 2001 at 20:31:51: In Reply to: copying files w/ GetWindowsDirectory posted by p12174 Doug on November 03, 2001 at 09:31:59: : i have some code that i've been working on, utilizing the "GetWindowsDirectory" parameters/functions, etc.. : the code that i've been working with, is for like opening a file in the \system32 directory of the end user(s) system, without actually knowing the user(s) drive/directory that they installed nt4/2000/xp to.. : here's my sample code: : procedure TForm1.Button1Click(Sender: TObject); : end. : what i'm trying to figure out is this.. how to use the GetWindowsDirectory, but instead of opening a file, "copying" a predetermined/specific file to the "\system32" directory of the users WindowsDir, etc.. : any suggestions?
: i'd love to find a reference somewhere or another on the "GetWindowsDirectory" functions, etc.. and others or similar one's in it's class as well.. -------------
For opening a file from a certain location, copying a file to a certain location or deleting a file at a certain location, essentially the technique is the same: 2. Append a backslash if the last character is not a backslash. That's because functions like GetWindowsDirectory give the directory without a backslash, EXCEPT if it is the root of a disk. Thus, in the classical situation you get C:\Windows -- but if Windows is installed in the root of drive C, you get C:\ 3. If necessary, append some more stuff to the destination directory, like "System32\" in your example.
4. Append the name of the file to handle. An example:
procedure TForm1.Button1Click(Sender: TObject); var Source, FileName, DestDir, Destination : string; Buff: array[0..Max_Path] of Char; OK: Boolean; begin // Set up destination directory, for example: GetWindowsDirectory(Buff, Max_Path); DestDir := StrPas(Buff); // Set up full path of destination if DestDir[Length(DestDir)] <> '\' then DestDir := DestDir + '\'; DestDir := DestDir + 'System32\'; Source := 'C:\Test\TestFile01.dat'; // ExtractFileName(Location) returns the leftmost characters of Location, // starting with the first character after the colon or backslash that // separates the path info from the name and extension FileName := ExtractFileName(Source); Destination := DestDir + FileName; // CopyFile(lpSource, lpDest, FailIfExists) will NOT overwrite the testination // if it already exists, if the last parameter FailIfExists is TRUE. It will also // return FALSE in that case. If you always want to overwrite the destination, // specify FALSE as the last parameter. OK := CopyFile(PChar(Source), PChar(Destination), TRUE); // Let's check if everything went all right if OK then ShowMessage('File ' + Source + ' was copied to ' + Destination) else ShowMessage('Error: copy of ' + Source + ' to ' + Destination + ' failed.'); end; Tip: GetWindowsDirectory, GetSystemDirectory, FileCopy and many more are documented in the "Win32 Programmer's Reference", usually accessible as the last topic on the "Contents" that you get when selecting Help | Contents in Delphi. The "Win32 Api" helpfiles, such as win32.hlp, usually are located in the directory "Program Files\Common Files\Borland Shared\MSHelp". |