C#UnauthorizedAccessException错误

C#UnauthorizedAccessException错误,c#,.net,sockets,C#,.net,Sockets,我正在通过套接字传输文件。当我试图将文件保存到自定义目录时,使用BinaryWrite函数会出现此错误 private void downloadFromServer() { try { byte[] buffer = new byte[5000 * 1024]; byte[] incomingFile = new byte[5000 * 1024]; buffer = Encoding.Default.GetBytes(getUserName.T

我正在通过套接字传输文件。当我试图将文件保存到自定义目录时,使用BinaryWrite函数会出现此错误

private void downloadFromServer()
{
   try
   {
      byte[] buffer = new byte[5000 * 1024];
      byte[] incomingFile = new byte[5000 * 1024];
      buffer = Encoding.Default.GetBytes(getUserName.Text + "Download" 
         + getFileName.Text + "end");
      clientSocket.Send(buffer);
      activityLog.AppendText("Preparing to download... \n");
      while (incomingFile != null)
      {
         clientSocket.Receive(incomingFile);

         int receivedBytesLen = incomingFile.Length;
         int fileNameLen = BitConverter.ToInt32(incomingFile, 0);
         File.WriteAllBytes(fileDir, incomingFile);
      } 
      activityLog.AppendText("File saved to " + fileDir + "\n");
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}
File.writealBytes(fileDir,incomingFile)需要文件名。从变量名看,您似乎正在使用文件夹名。也就是说,应该是
@“e:\tempfile.bin”
,而不是
@“e:\”

给定字节数组和文件路径,此方法打开指定的文件,将字节数组的内容写入文件,然后关闭文件


注意:如果
fileDir
表示文件名,那么您应该在整个代码中查找其他不太真实的名称…

您确定可以访问fileDir目录吗?我确定,但我如何检查它?我试图通过C:\\和我的第二张磁盘E:\保存到一个文件,但都失败了,出现了相同的错误。启动程序的用户应该有目标目录上的“写入”权限。我如何检查?我通过vs查看了属性,用户拥有所有读写等访问权限…仍然没有解决方案