C# File.Move System.IO.IOException:“文件”;无法再连接到此远程计算机…”;

C# File.Move System.IO.IOException:“文件”;无法再连接到此远程计算机…”;,c#,windows-7,connection,ioexception,file-move,C#,Windows 7,Connection,Ioexception,File Move,File.Move System.IO.IOException:“此时无法再连接到此远程计算机,因为已存在计算机可以接受的连接数。” 我有一个进程在SYS帐户下运行。它正在本地硬盘上处理文件,并使用模拟将其移动到域上的远程驱动器 编辑,添加代码示例: 反复调用下面的方法(模拟是我用于模拟的实用程序类,这与问题无关) 编辑,添加代码示例。 当进程在XP机器上运行且远程驱动器在XP或Win7机器上时,对File.Move的调用可以正常工作并移动所需的文件。但是,当进程在Win7上运行且远程驱动器在W

File.Move System.IO.IOException:“此时无法再连接到此远程计算机,因为已存在计算机可以接受的连接数。”

我有一个进程在SYS帐户下运行。它正在本地硬盘上处理文件,并使用模拟将其移动到域上的远程驱动器

编辑,添加代码示例:

反复调用下面的方法(模拟是我用于模拟的实用程序类,这与问题无关)

编辑,添加代码示例。

当进程在XP机器上运行且远程驱动器在XP或Win7机器上时,对File.Move的调用可以正常工作并移动所需的文件。但是,当进程在Win7上运行且远程驱动器在Win7机器上时,在移动了20个文件后会引发上述异常

我还尝试使用MOVEFILE\u REPLACE\u EXISTING&MOVEFILE\u COPY\u ALLOWED&MOVEFILE\u WRITE\u THROUGH标志调用win32 API MoveFileEx,结果相同-ERROR\u REQ\u NOT\u ACCEP 71(0x47)

在Win7上,调用File.Move所建立的底层连接似乎未正确关闭

有没有办法克服这个问题

我错过了什么


谢谢,Ilan根据您的代码,您可能正在使用UNC路径进行复制。我一直都有这样做的问题,我学到了最好只是映射,然后根据需要在代码中断开驱动器。它使我不必处理权限问题,也不用处理您所描述的问题

我们有一个类为我们处理这个问题。我们已经使用它5年多了,没有任何问题,包括在Win7机器上的代码和远程端。当然,它也会对你有用

public static class NetworkDrives
    {
        public static bool  MapDrive(string DriveLetter, string Path, string Username, string Password)
        {

            bool ReturnValue = false;

            if(System.IO.Directory.Exists(DriveLetter + ":\\"))
            {
                DisconnectDrive(DriveLetter);
            }
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": " + '"' + Path + '"' + " " + Password + " /user:" + Username;
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }
        public static bool DisconnectDrive(string DriveLetter)
        {
            bool ReturnValue = false;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": /DELETE";
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }

    }

如果没有一个代码示例,就很难判断您缺少了什么。你试过什么@David Stratton,感谢您的回复,已经添加了一个示例代码。感谢您的代码示例,非常感谢,确实使用了UNC路径。我今天要试一试。映射远程驱动器时,是否仍需要模拟(用于移动文件)?否。它使用在函数中传递的任何用户名和密码。它的工作原理与将驱动器映射到您无权访问的文件夹类似。系统会提示您输入用户名和密码,如果您为具有访问权限的帐户提供用户名和密码,则可以访问驱动器。你不需要登录或注销,它就在那里。
public static class NetworkDrives
    {
        public static bool  MapDrive(string DriveLetter, string Path, string Username, string Password)
        {

            bool ReturnValue = false;

            if(System.IO.Directory.Exists(DriveLetter + ":\\"))
            {
                DisconnectDrive(DriveLetter);
            }
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": " + '"' + Path + '"' + " " + Password + " /user:" + Username;
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }
        public static bool DisconnectDrive(string DriveLetter)
        {
            bool ReturnValue = false;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": /DELETE";
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }

    }