私の経験では、.NET は常に 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;
}
}