create dynamic array in c#
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.
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".
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.
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"
.
精彩评论