0
byte[] bFileName = new byte[512];
r2Socket.Receive(bFileName);
String FileName = Encoding.UTF8.GetString(bFileName);
System.Windows.Forms.MessageBox.Show(FileName); // It's Ok. Show FileName = "text.jpg"

FileStream = new FileStream("D:\\" + FileName, FileMode.Create, FileAccess.Write);

FileStream ArgumentException

path is an empty string (""), contains only white space, or contains one or more invalid characters. -or- path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.

Why it happens? thx.

==

//CLIENT
byte[] bFileName = new byte[512];
Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket.Connect(textBoxIP.Text, int.Parse(textBoxPORT.Text));
FileInfo sFileInfo = new FileInfo(textBoxFILE.Text);
string FileName = sFileInfo.Name;
bFileName = Encoding.UTF8.GetBytes(FileName);
Socket.Send(bFileName);
//SERVER
Socket rSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress rIPAddress = Dns.GetHostEntry("localhost").AddressList[0];
IPEndPoint rIPEndPoint = new IPEndPoint(IPAddress.Any, int.Parse(textBoxPORT2.Text));
rSocket.Bind(rIPEndPoint);
rSocket.Listen(1);
byte[] bFileName = new byte[512];
r2Socket.Receive(bFileName);
String FileName = Encoding.UTF8.GetString(bFileName);
Andrey
  • 39
  • 8
  • Are you sure that `FileName` is not empty and has a valid FileName ? Also you're better off using `Path.Combine()` for combining paths instead of using '+' – abhilash Apr 18 '12 at 12:50
  • 1
    Since you're reading from a socket, I'd also ensure you don't have any whitespace characters in the filename which would not be visible in the message box show. Easy way to check is to display the file name and length of the filename. – Josh Apr 18 '12 at 12:55
  • FileStream = new FileStream(Path.Combine(textBoxPATH.Text, "i.jpg"), FileMode.Create, FileAccess.Write); //It's work! – Andrey Apr 18 '12 at 13:06
  • Problem in String FileName = Encoding.UTF8.GetString(bFileName); – Andrey Apr 18 '12 at 13:07

1 Answers1

0

I'm going to guess it's because you are using UTF-8, and NTFS uses something else, thus the invalid characters error. I would try using another encoding.

Check out this SO post: What encoding are filenames in NTFS stored as?

Community
  • 1
  • 1
Joshua
  • 4,099
  • 25
  • 37