1

我是 C# 的新手。我有一个问题,我想在运行时创建名为文件的 IP 地址并将数据写入这些文件。我为此目的使用此代码,但它不起作用。它给了我例外:

First chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll

例外是:

System.NotSupportedException: The given path's format is not supported.

这是我的代码:

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);

这是堆栈跟踪:

   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)
4

1 回答 1

2

我想这handler.RemoteEndPoint将返回一个 IP 地址,其端口由:. 但是您不能:在 Windows 上的文件名中包含该字符。您将不得不用其他字符(例如下划线)替换它们。

于 2013-06-21T12:13:11.733 回答