Reputation: 484
I'm currently overhauling my dirt oval racing pointkeeping app a bit and I've decided to start with the folder saving aspect of my app.
I want to change the file path from just C:\
to a path that saves directly to the user's desktop to make saving and finding the saved folder from my application a lot easier (the user then writes selected CSV files to that folder).
Current code I'm using:
procedure TfrmExDialog.FormShow(Sender: TObject);
var
sInput:string;
begin
sInput:=InputBox('Folder creation','Please enter the name of event without spaces (instead of spaces you can use _ )','C:\');
folderForToday:=sInput;
createdir(folderForToday);
end;
Thanks in advance for the help!
Kind Regards
PrimeBeat
Upvotes: 2
Views: 851
Reputation: 8386
I would recommend against saving your data on user desktop. Why? There are multiple reasons for this:
So instead I would recommend you store the data in other folders like MyDocuments or AppData.
You could always add ability to open such folder in file explorer from your application by calling ShellExecute(Handle, 'open', MyFolder, '', '', SW_SHOWNORMAL);
where MyFolder
is just path location to the folder you want to open.
But probably it would be best for you to allow your end users to go and chose by themselves where they want this data to be saved.
And if you are really concerned that your end users might forget where they chose for the data to be saved you can also register new Windows Library that will point to such folder by using approach mentioned in How to Read/Write Windows 7 Library Locations?
Upvotes: 1
Reputation: 12322
The desktop is just a folder like any other. You can find his path like this:
var
Path : array [0..MAX_PATH] of Char;
sInput : String;
begin
sInput := InputBox('Folder creation','Please enter the name of event without spaces (instead of spaces you can use _ )','C:\');
sInput := sInput.Replace(' ', '_'); // Prevent spaces
SHGetFolderPath(0, CSIDL_DESKTOP, 0, SHGFP_TYPE_CURRENT, @Path[0]);
folderForToday := IncludeTrailingPathDelimiter(Path) + sInput;
CreateDir(folderForToday);
end;
You can also use CSIDL_COMMON_DESKTOPDIRECTORY
to get the desktop directory for all users. Look at Microsoft Documentation for all possible values.
Add WinApi.ShlObj
in the uses clause.
Once you have the desktop folder, you can create your file there or create a sub folder for your files using the standard Delphi functions for the purpose.
Upvotes: 2