user285372
user285372

Reputation:

underscore surrounded by backslashes

I am trying to concatenate some file name constituents to make sure if I already have an identically-named file, the new one gets a new name with an underscore and an incremental counter.

I have written the following which I believe should work:

string tempPath = Path.Combine(Application.dataPath, "MyValues");
tempPath = Path.Combine(tempPath, "_");
tempPath = Path.Combine(tempPath, counter.ToString() + ".csv");

After I noticed no file is actually saved, I included Debug.Log(tempPath) to see what it returns, and surprisingly I get the following:

MyUnityProject/Assets\MyValues\_\0.csv

Firstly, why are there \ instead of /?

Also, where are the backslashes surrounding the underscore coming from?

Finally, instead of manually removing them, how could I get the correct syntax back? For example MyValues_0.csv, MyValues_1.csv, etc...

Upvotes: 0

Views: 735

Answers (2)

derHugo
derHugo

Reputation: 90789

Whether \ or / is used to build a valid path is the whole reason why you even use Path.Combine at all:

  • It uses whatever the current Plattform/OS understands to combine two strings to a path and thereby makes it plattforms independent.
  • It also adds some additional checks if the provided strings are valid path snippets and thereby makes it also saver to use than a simple concatenation.

What you do

string tempPath = Path.Combine(Application.dataPath, "MyValues");
tempPath = Path.Combine(tempPath, "_");
tempPath = Path.Combine(tempPath, counter.ToString() + ".csv");

Is assuming that MyValue and _ are folder names.

Use Path.Combine only for combining folders and the final filename not for concatenating the filename itself.


What you want to do instead is building the complete filename by string concatenation e.g. using string.Format

string filename = string.Format("MyValues_{0}.csv", counter.ToString());

or also $ (string interpolation)

string filename = $"MyValues_{counter.ToString()}.csv";

which are just esier ways of writing

string filename = "MyValues_" + counter.ToString() + ".csv";

and than use Path.Combine only to combine the complete filename with the given system folder path like

string tempPath = Path.Combine(Application.dataPath, filename);

Upvotes: 1

Isma
Isma

Reputation: 15209

By looking at the source code for the Path class available here: https://referencesource.microsoft.com/#mscorlib/system/io/path.cs,16ed6da326ce4745

We can see that the method Combine calls the method CombineNoChecks for each pair of string paths and returns the following:

return path1 + DirectorySeparatorCharAsString + path2;

The value of DirectorySeparatorCharAsString is:

internal const string DirectorySeparatorCharAsString = "\\";

There are also other separators defined in the class so you get the right one according your running platform:

// Platform specific alternate directory separator character.  
// This is backslash ('\') on Unix, and slash ('/') on Windows 
// and MacOS.
public static readonly char AltDirectorySeparatorChar = '/';

So I wouldn't worry about what the method is returning, it should just work.

Upvotes: 1

Related Questions