1

File.Move System.IO.IOException: "コンピューターが受け入れることができる数の接続が既に存在するため、現時点ではこのリモート コンピューターにこれ以上接続できません"。

SYS アカウントでプロセスを実行しています。ローカル HD 上のファイルを処理し、なりすましを使用してドメイン上のリモート ドライブに移動します。

編集、追加されたコード サンプル:

以下のメソッドが繰り返し呼び出されます (Impersonation は、私が偽装に使用するユーティリティ クラスです。これは問題とは関係ありません)。

private void moveFileUsingImpersonation(string srcFilePath, string dstFilePath, string userName, string passWord)
        {
                WindowsImpersonationContext wic = null;
                // move it to destination 
                try
                {
                    wic = Impersonation.Impersonate(userName, passWord);
                    if (wic != null)
                    {
                        File.Move(srcFilePath, dstFilePath);
                    }
                    else
                    {
                        Console.WriteLine("moveFileUsingImpersonation, Failure to impersonate!");
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine("moveFileUsingImpersonation, Exception={0}", ex.ToString());
                }
                finally
                {
                    Impersonation.UndoImpersonate(wic);
                }
            }

編集、コードサンプルを追加。

プロセスが XP マシンで実行されており、リモート ドライブが XP または Win7 マシンのいずれかにある場合、File.Move の呼び出しは問題なく機能し、必要なファイルを移動します。ただし、プロセスが Win7 で実行されており、リモート ドライブが Win7 マシン上にある場合、20 個のファイルが移動された後に前述の例外がスローされます。

また、MOVEFILE_REPLACE_EXISTING & MOVEFILE_COPY_ALLOWED & MOVEFILE_WRITE_THROUGH フラグを指定して win32 API MoveFileEx を呼び出そうとしましたが、同じ結果になりました - ERROR_REQ_NOT_ACCEP 71 (0x47)。

File.Move の呼び出しによって作成された基になる接続が、Win7 で適切に閉じられていないようです。

これを克服する方法はありますか?

ここで何が欠けていますか?

ありがとう、イラン

4

1 に答える 1

3

コードに基づいて、おそらく UNC パスを使用してコピーしています。これを行うには常に問題があり、必要に応じてコードでドライブをマップしてから切断するのが最善であることを学びました。アクセス許可の問題や、あなたが説明しているような問題に対処する必要がなくなります。

これを処理するクラスがあります。コード側とリモート側の両方で Win7 マシンを含め、5 年以上問題なく使用しています。残念なことに、それはあなたにも役立ちます。

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

    }
于 2012-03-27T15:50:49.527 に答える