A String in C # is immutable as you say. This means that this would create several String objects in memory:
String s = "String of numbers 0"; s += "1"; s += "2";
So, although the s variable will return you the value of String of numbers 012 , internally it required the creation of three lines in memory.
In your particular case, the solution is pretty simple:
String myPath = "C:\\folder1\\folder2\\myFile.aspx"; myPath = Path.Combine(Path.GetDirectoryName(myPath), Path.GetFileNameWithoutExtension(myPath));
Again, it looks like myPath has changed, but it really hasn't. An internal copy and assignment has occurred, and you continue to use the same variable.
Alternatively, if you must save the original variable, you can simply create a new variable:
String myPath = "C:\\folder1\\folder2\\myFile.aspx"; String thePath = Path.Combine(Path.GetDirectoryName(myPath), Path.GetFileNameWithoutExtension(myPath));
In any case, you will get a variable that you can use.
Note that using Path methods ensures that you get the correct paths, not blind String replacements, which can have unintended side effects.
source share