Reputation: 3830
I am newbie in C#. I have a problem, I want to create IP address named file at run time and write data to those files. I am using this code for the purpose, but it's not working. It's giving me exceoption:
First chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
The exception is:
System.NotSupportedException: The given path's format is not supported.
And this is my code:
System.Diagnostics.Debug.Write( content);
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
System.Diagnostics.Debug.WriteLine(
"Read {0} bytes from socket. \n Data : {1}", content);
string dir = @"C:\AppRecord";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(Path.Combine(dir,
"log"+handler.RemoteEndPoint.ToString()+".txt"), content);
This is stack trace:
A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, AccessControlActions control, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllText(String path, String contents, Encoding encoding)
at System.IO.File.WriteAllText(String path, String contents)
at ServerWService.Service1.AsynchronousSocketListener.ReadCallback(IAsyncResult ar)
Upvotes: 1
Views: 1309
Reputation: 1038790
I suppose that handler.RemoteEndPoint
will return an IP address with a port separated by :
. But you cannot have the character :
in a filename on Windows. You will have to replace those with some other character such as an underscore for example.
Upvotes: 2