question

Nedudale-0390 avatar image
0 Votes"
Nedudale-0390 asked Paul-5034 commented

How can I remove a space from a folder name when using string.Format for the folder name ?

 private void CreateDownloadFolders()
         {
             string locationToCreateFolderRadar = textBoxRadarPath.Text;
             string locationToCreateFolderSatellite = textBoxSatellitePath.Text;
             string folderName = "";
             string date = DateTime.Now.ToString("ddd MM.dd.yyyy");
             string time = DateTime.Now.ToString("HH.mm tt");
             string format = "{0} On {1} {2}";
             folderName = string.Format(format, date, time, "");
             Directory.CreateDirectory(locationToCreateFolderRadar + "\\" + folderName);
             Directory.CreateDirectory(locationToCreateFolderSatellite + "\\" + folderName);
    
             radarFolderImagesDownload = locationToCreateFolderRadar + "\\" + folderName;
             satelliteFolderImagesDownload = locationToCreateFolderSatellite + "\\" + folderName;
    
             folders.Add(radarFolderImagesDownload);
             folders.Add(satelliteFolderImagesDownload);
         }

The problem is on the line :

 folderName = string.Format(format, date, time, "");

When I'm using a breakpoint I see that in the folderName string in the end after the pm there is a space.
I'm not sure how to remove that space and why he is added.

The space in the end give me exception that the directory cannot be found. I think the space in the end is the problem.

In the screenshot image on the left is the folder name string and in the end after the pm there is a space.

143904-notf1.jpg


dotnet-csharpwindows-forms
notf1.jpg (64.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
1 Vote"
Paul-5034 answered Paul-5034 commented

You're adding a third substitution to your format - does this work for you?

string format = "{0} On {1}";
folderName = string.Format(format, date, time);
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just a side note in case you're not aware - it's a more robust approach to use Path.Combine to concatenate paths - that way you don't have to worry about trailing slashes:

string combined = Path.Combine(@"c:\folder1", "folder2");
Console.WriteLine(combined); // Outputs: "c:\folder1\folder2"
0 Votes 0 ·