PrimeBeat
PrimeBeat

Reputation: 484

How to create a folder on desktop?

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

Answers (2)

SilverWarior
SilverWarior

Reputation: 8386

I would recommend against saving your data on user desktop. Why? There are multiple reasons for this:

  1. While from a technical point desktop is just another folder user point of view for desktop is much more complex.
    You see most people have desktops filled with shortcuts to most used programs. And most people also tend to arrange these shortcuts in multiple clusters by keeping similar or related programs in same clusters. This means that there are empty spaces between these shortcuts. So when your program will create a new folder on the desktop its icon will be placed in first empty space the computer could find. For a user who has lots of icons on their desktop this could make finding your newly created folder quite hard.
  2. If your end user has his desktop already filed with various items the newly created folder will be placed outside of the viewing area od users desktop. And the only way for user to reach that folder would be to plug in a larger monitor or open a desktop folder with file explorer in order to get ability to scroll amongst all desktop items.
  3. As you perhaps remember initially when Windows 8 rolled out there was no desktop folder at all. Yes Microsoft tried to remove the desktop folder entirely but put it back on request of many early beta testers of Windows 8. So there is no guarantee that desktop folder might not get removed in future windows versions.
  4. Due the fact that Desktop folder default location is always on system drive it is possible that saving lots of information to such folder could cause stability issues especially on computers that might use smaller SSD for system drive and mechanical drive for storing other data.

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

fpiette
fpiette

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

Related Questions