11

Am I using the Path.Combine Method Incorrectly?
I Get This Result With Path.Combine(string[]):

C:Users\\Admin\\AppData\\Roaming\\TestProject\\Connections.xml

And This is the desired Result of the less desirable code:the one that is the result generated by the commented out code):

C:\\Users\\Admin\\AppData\\Roaming\\TestProject\\Connections.xml

Note the missing \\ after the drive letter in the first path.

The old method (manually adding backslashes, commented out) works well, but I'm pretty sure that it wouldn't work under Linux or something else if compiled with mono.

string[] TempSave = Application.UserAppDataPath.Split(Path.DirectorySeparatorChar);
string[] DesiredPath = new string[TempSave.Length - 2];
for (int i = 0; i < TempSave.Length - 2; i++)
{
    //SaveLocation += TempSave[i] + "\\";//This Works
    DesiredPath[i] = TempSave[i];
}
SaveLocation = Path.Combine(DesiredPath);//This Doesn't Work.    
ConnectionsFs = new FileStream(Path.Combine(SaveLocation,FileName)/*SaveLocation+FileName*/, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

Basically, I want the end Result to be the TestProject Directory, not the project itself or its version.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Samuel
  • 710
  • 5
  • 8
  • 1
    It looks like you're trying to find the grandparent folder of the `UserAppDataPath` folder. Why not just combine with `"..\.."` instead? That is: `Path.Combine(Application.UserAppDataPath, @"..\..")` – Dan Puzey Nov 11 '13 at 14:52

0 Answers0