为什么使用静态方法映射的共享会失败?

时间:2019-05-22 12:42:35

标签: asp.net

我遇到了一个奇怪的现象。我设法解决了这个问题,但我仍然想知道其原因对我来说是违反直觉的。

简而言之:在页面加载方法中,我使用了另一个类的静态方法来映射网络共享。然后在页面加载中,我尝试将文件复制到该共享上,但失败了。就目前而言,映射似乎是在不同的用户句柄(或类似的东西)下运行的。当我在页面加载方法(而不是其他静态方法)中执行完全相同的映射时,它工作顺利。

现在的问题是:这是什么原因?

页面加载:

...
MemoryStream streamReader;
...
myClass.mapdrive();
byte[] buffer = new byte[4096];

using (FileStream streamWriter = File.Create(@"\\myserver\theshare\myfile.txt"))
{
    StreamUtils.Copy(streamReader, streamWriter, buffer); //<-- Point of Exception (Access Denied)
}

静态:

public class myclass
{
    public static void mapdrive()
    {
    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 = String.Format(
                    "net use %1 %2 /USER:%3",
                    @"\\myserver\theshare",
                    "passme",
                    @"\\myserver\myuser"
        );
        p.Start();
        p.WaitForExit();
    }
}

0 个答案:

没有答案