如何将txt文件复制到asp.net中的远程计算机上

时间:2016-02-09 06:17:43

标签: c# asp.net

下面是我的代码,它是将文件复制到任何远程计算机的标准代码。

但它不断出现这个错误:

  

找不到网络路径。

请帮帮我!

IntPtr admin_token = default(IntPtr);
    WindowsIdentity wid_admin = null;
    WindowsImpersonationContext wic = null;
    IntPtr duplicate_token = default(IntPtr);
    if (LogonUser("Administrator", "WIN-ABC", "aaaaaaa", 9, 0, ref admin_token) != 0)
        {
        if (DuplicateToken(admin_token, 2, ref duplicate_token) != 0)
            {
            wid_admin = new WindowsIdentity(duplicate_token);
            wic = wid_admin.Impersonate();
            System.IO.File.Copy("E:/abc.txt", "\\\\WIN-ABC\\Data1\\abc.txt", true);
        }

1 个答案:

答案 0 :(得分:-1)

尝试此代码...这应该允许您访问远程路径

IntPtr admin_token = default(IntPtr);
            IntPtr duplicate_token = default(IntPtr);
            WindowsIdentity wid_admin = null;
            WindowsImpersonationContext wic = null;

           if (LogonUser("Administrator", "WIN-ABC", "ABCDEF", 9, 0, ref admin_token) != 0)
            {
                if (DuplicateToken(admin_token, 2, ref duplicate_token) != 0)
                {
                    wid_admin = new WindowsIdentity(duplicate_token);
                    wic = wid_admin.Impersonate();

                   System.IO.File.Copy("E:/Temp/abc.txt", "\\\\WIN-ABC\\TestFolder\\abc.txt", true)
                }
            }

确保添加以下导入声明

 [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken, 
       int impersonationLevel, ref IntPtr hNewToken);