简体   繁体   中英

C# remove invisible chars from NetworkStream

I'm making a simple command program, and while trying to create a directory, I'm getting the error that there is invalid characters in the path, I'm guessing the NetworkStream added invisible characters?

Code:

Console.WriteLine(">> Recieved: " + Encoding.ASCII.GetString(data, 0, data.Length));
byte[] back = null;
if (Encoding.ASCII.GetString(data, 0, data.Length).Contains("cd "))
{
    try
    {
        back = Encoding.UTF8.GetBytes(">> Created Directory");
        stream.Write(back, 0, back.Length);
        string dir = Encoding.ASCII.GetString(data, 0, data.Length).Replace("cd ", "");
        Directory.CreateDirectory(dir);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

}

EXACT ERROR: System.Argument Exception, Illegal Characters at Path

Put dir.ToCharArray() in the debugger watch expression. You'll now see the individual character codes, including the non-printable ones like 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM