-1

I load data from a Database on another PC in local network , and it works perfectly fine.

I just enter server name and db path and it works.

Now i am also looking for latest modified file in the folder on the server ,it works fine on local pc but when i use a server name plus path , it generate that path concatenated with the executable folder path.

string tmpPath=string.Empty;
if (serverName != "")
  {
   tmpPath = "\\" + serverName + "\\" + TrackingPath + "\\u00" + ID;
  }

And after this i simply read the folder to get latest modified file.

But the path becomes ....Debug\servername\trackingpath..... which is wrong.

EXAMPLE

Servename=testServer
TrackingPath= TmpFolder\SharedFolder\TrackingFolder

So according to my code it should become \\testServer\\TmpFolder\SharedFolder\TrackingFolder but instead it make it project....\Debug\\\testServer\\TmpFolder\SharedFolder\TrackingFolder So how can i read this folder from testserver

confusedMind
  • 2,573
  • 7
  • 33
  • 74
  • We need more details. Is this when debugging? What's `serverName`? What's `TrackingPath`? Why are you not using Path.Combine? `http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx` – Arran Jun 06 '13 at 14:36
  • As in, where are the values of `serverName` and `TrackingPath` coming from? Are they being passed in? Where are they set? How are they set? As in, show us the *actual* code where they are set. – Arran Jun 06 '13 at 14:40
  • The come from the database they are always serverName = abc and tracking path starting as i stated in example. – confusedMind Jun 06 '13 at 14:40

1 Answers1

0

Use Path.Combine to work with path files and remember that the backslash is a special character in C# and it is used to escape other chars. So you need

string ID = "test";
string TrackingPath = "tracking";
string serverName = "server_name";
string tmpPath=string.Empty;
if (serverName != "")
{
    tmpPath = Path.Combine(@"\\", serverName, TrackingPath ,"u00" + ID);
}
Console.WriteLine(tmpPath);

To insert a backslash in a string you need to double it or prefix with the Verbatim string prefix character @

Using 3.5 Framework and below

    tmpPath = Path.Combine(Path.Combine(Path.Combine(@"\\", serverName), TrackingPath) ,"u00" + ID);

Well not very efficient but it works

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286