0

This is my code.

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/C net view";
// p.StartInfo.RedirectStandardOutput = true;
p.Start();
char[] delimiterChars = { '\\' };
// List<string> serverNames = new List<string>();
string input = "\\ABC-PC,\\ADMIN,\\ANUSHREE-PC,\\MANISHA-PC";
List<string> serverNames = input.Split(',').ToList();
// System.Console.WriteLine("Original input: '{0}'", input);

all \\ character in output is deleted.& only server names are extracted.Hence dynamic array is created.& this array is split &parsed. how can i do? please give the solution & changes of code.

3 Answers 3

2

Your problem here is the "\" characters. In C#, the backslash character has a special meaning in a string, it means "the next character is going to be an escaped code".

In short, your solution is either to double up every slash to get "\\\\ABC-PC", or prefix the string with an @ symbol which means "use this string exactly as typed":

string input = @"\\ABC-PC,\\ADMIN,\\ANUSHREE-PC,\\MANISHA-PC";

The reason the "\" is deleted (on fact I think you'll find that your double "\" are becoming a single "\") is because it interprets "\" to mean "the first slash indicates an escaped character next. The second slash is that escaped character, a backslash, therefore I should just print the second backslash"). The reason it has a special meaning is that it allows you to supply a string like "\n" which means "a newline character".

Sign up to request clarification or add additional context in comments.

Comments

0

I don't get it completely, but do you mean:

string input = @"\\ABC-PC,\\ADMIN,\\ANUSHREE-PC,\\MANISHA-PC";
List<string> serverNames = input.Replace(@"\\", "").Split(',').ToList();

The changes are in the @"\" replaced, by nothing, and the @ added before the " in string input.

Comments

0

Is this what you mean? :

List<string> serverNames = input.Split(',')
                                .Select( s => s.Trim('\\'))
                                .ToList();

Also as @Rob Levine pointed out your input string does not contain valid network path's right now, they should start with \\ double slashes so either prefix with @ to take the literal string (preferred, much more readable) like @"\\ABC-PC" or escape them i.e. "\\\\ABC-PC".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.