Creating a file path in C #

So, I am trying to create a path in C #. I use Environment.Machinename and save it with serverName variable. Then I create another string variable and have a different path extension. Here is my code:

string serverName = Environment.MachineName;
string folderName = "\\AlarmLogger";

No matter what I do, I cannot get just one backslash before AlarmLogger. Any ideas how I can point the path in C #?

Edit: I am wondering if my code wants to insert correctly. In any case, when I insert it, I only see the backslash, but I have two codes. Due to the escape character sequence. But something like

string test = @"\\" + serverName + folderName 

doesn't seem to want to work for me.

+5
source share
3 answers

Path.Combine(serverName, folderName). Path.Combine , .

+20

Path.Combine , . , , , . Environment.MachineName( . MSDN). Path.Combine(_, ), "\ AlarmLogger". , .

- ( ):

string serverName = Environment.MachineName;
string folderName = "\\\\AlarmLogger";  //this gives alarmlogger two leading slashes
string test = @"\\" + serverName + folderName.Substring(1,folderName.Length-1); //this removes one of the two leading slashes

, .

+2

, .

, :

string twoBackslashes = "\\\\";

:

string twoBackslashes = @"\\";

, System.IO.Path. , Path.Combine.

+1
source

All Articles