实现访问远程主机的共享目录中的一个文件的解决方法:

一、调用Net use命令

// 使用方法:
        //if (Connect("192.168.1.48", "用户名", "密码"))   
        //{
        //    File.Copy(@"\\192.168.1.48\共享目录\test.txt",   @"e:\\test.txt",   true);   
        //}
        public bool Connect(string remoteHost, string userName, string passWord)
        {
            bool Flag = true;
            Process proc = new Process();
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.CreateNoWindow = true;
            try
            {
                proc.Start();
                string command = @"net  use  \\" + remoteHost + "  " + passWord + "  " + "  /user:" + userName + ">NUL";
                proc.StandardInput.WriteLine(command);
                command = "exit";
                proc.StandardInput.WriteLine(command);
                while (proc.HasExited == false)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                if (errormsg != "")
                    Flag = false;
                proc.StandardError.Close();
            }
            catch (Exception ex)
            {
                Flag = false;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

二、调用WNetAddConnection2、WNetAddConnection3或者NetUseAdd函数,进行磁盘映射。

using System;
using System.Collections.Generic;
using System.Text;       
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public class MyMap
    {
        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
        public static extern uint WNetAddConnection2(
            [In] NETRESOURCE lpNetResource,
            string lpPassword,
            string lpUsername,
            uint dwFlags);
        
        [DllImport("Mpr.dll")]
        public static extern uint WNetCancelConnection2(
            string lpName,
            uint dwFlags,
            bool fForce);
        
        [StructLayout(LayoutKind.Sequential)]
        public class NETRESOURCE
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            public string LocalName;
            public string RemoteName;
            public string Comment;
            public string Provider;
        }

// remoteNetworkPath format:  @"\\192.168.1.48\sharefolder"
        // localDriveName format:     @"E:"
        public static bool CreateMap(string userName, string password, string remoteNetworkPath, string localDriveName)
        {            
            NETRESOURCE myNetResource = new NETRESOURCE();
            myNetResource.dwScope = 2;       //2:RESOURCE_GLOBALNET
            myNetResource.dwType = 1;        //1:RESOURCETYPE_ANY
            myNetResource.dwDisplayType = 3; //3:RESOURCEDISPLAYTYPE_GENERIC
            myNetResource.dwUsage = 1;       //1: RESOURCEUSAGE_CONNECTABLE
            myNetResource.LocalName = localDriveName;
            myNetResource.RemoteName = remoteNetworkPath;
            myNetResource.Provider = null;

uint nret = WNetAddConnection2(myNetResource, password, userName, 0);

if (nret == 0)
                return true;
            else
                return false;
        }

// localDriveName format:     @"E:"
        public static bool DeleteMap(string localDriveName)
        {
            uint nret = WNetCancelConnection2(localDriveName, 1, true);

if (nret == 0)
                return true;
            else
                return false;
        }

public void test()
        {
            // 注意:
            // remote、local、username的格式一定要正确,否则可能出现错误
            string remote = @"\\192.168.1.48\generals";
            string local = @"P:";
            string username = @"Domain\UserName";
            string password = @"Password";
            bool ret = MyMap.CreateMap(username, password, remote, local);
            if (ret)
            {
                //do what you want:
                // ...
                //File.Copy("q:\\test.htm", "c:\\test.htm");

MyMap.DeleteMap(local);
            }
        }
    }
}

三、使用WebClient类

由于WebClient类可以上传下载文件,并且支持以http:、https:和file:开头的URI,所以可以用WebClient类来传输文件。
添加System.Net命名空间后使用如下代码下载文件:

private void Test1()
        {
            try
            {
                WebClient client = new WebClient();
                NetworkCredential cred = new NetworkCredential("username", "password", "172.16.0.222");
                client.Credentials = cred;
                client.DownloadFile("file://172.16.0.222/test/111.txt", "111.txt");
            }
            catch (Exception ex)
            {
                // 如果网络很慢,而文件又很大,这时可能有超时异常(Time out)。
            }
        }

public void Test2()
        {
            try
            {
                WebClient client = new WebClient();
                NetworkCredential cred = new NetworkCredential("username", "password", "domain");
                client.Credentials = cred;
                client.DownloadFile("file://172.16.0.222/test/111.txt", "111.txt");
            }
            catch (Exception ex)
            {
                // 如果网络很慢,而文件又很大,这时可能有超时异常(Time out)。
            }
        }

类似的还可以试试WebRequest、FileWebRequest等:

WebRequest req = WebRequest.Create("file://138.12.12.14/generals/test.htm");
                NetworkCredential cred = new NetworkCredential("username", "password", "IP");
                req.Credentials = cred;
                WebResponse response = req.GetResponse();
                Stream strm = response.GetResponseStream();
                StreamReader r = new StreamReader(strm);
                ... ...

四、角色模拟

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.IO;

namespace Test
{
    public class Test
    {
        // logon types
        const int LOGON32_LOGON_INTERACTIVE = 2;
        const int LOGON32_LOGON_NETWORK = 3;
        const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
        // logon providers
        const int LOGON32_PROVIDER_DEFAULT = 0;
        const int LOGON32_PROVIDER_WINNT50 = 3;
        const int LOGON32_PROVIDER_WINNT40 = 2;
        const int LOGON32_PROVIDER_WINNT35 = 1;

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int LogonUser(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);

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

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

private WindowsImpersonationContext impersonationContext;

public bool impersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

if (RevertToSelf())
            {
                // 这里使用LOGON32_LOGON_NEW_CREDENTIALS来访问远程资源。
                // 如果要(通过模拟用户获得权限)实现服务器程序,访问本地授权数据库可
                // 以用LOGON32_LOGON_INTERACTIVE
                if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                        {
                            System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                            IPrincipal pr = System.Threading.Thread.CurrentPrincipal;
                            IIdentity id = pr.Identity;
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }

if (token != IntPtr.Zero)
                CloseHandle(token);

if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);

return false;
        }

public void undoImpersonation()
        {
            impersonationContext.Undo();
        }

public void TestFunc()
        {
            bool isImpersonated = false;
            try
            {
                if (impersonateValidUser("UserName", "Domain", "Password"))
                {
                    isImpersonated = true;
                    //do what you want now, as the special user
                    // ...
                    File.Copy(@"\\192.168.1.48\generals\now.htm", "c:\\now.htm", true);
                }
            }
            finally
            {
                if (isImpersonated)
                    undoImpersonation();
            }
        }
    }
}

五、比较

方法一通过调用Shell命令Net Use实现,有点笨拙。
方法二和方法一有些相似之处。映射远程资源,然后访问。
方法三由于会有超时异常出现,所以在网络速度快、传输小文件时是可以的。
方法四通过身份模拟实现远程资源访问。一些服务器进程就是通过这种方式运行的。这种方法也是我的最爱。

六、要注意的地方

关于这几种方法,google后都可以找到一些文章。但是等到自己实际测试时,有时会出现各种小错误,
这些错误基本来源于两方面:

1、函数的参数选择有问题,和自己的环境不相符。
比如        
public static extern int LogonUser(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
中的dwLogonType,要访问远程资源就要用LOGON32_LOGON_NEW_CREDENTIALS,
要模拟本机用户就要用LOGON32_LOGON_INTERACTIVE。

2、函数的参数格式有问题。

a、比如
    public static extern int LogonUser(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
    中的lpszUserName、lpszDomain、lpszPassword就要写清楚。

我就在这遇到过问题,第一次测试时,远程服务器就是一台独立的文件服务器,这是我的调用方式:
        LogonUser("myname", "192.168.1.48", "password", LOGON32_LOGON_NEW_CREDENTIALS,
                    LOGON32_PROVIDER_DEFAULT, ref token);

第二次测试时,远程服务器是域MyDomain中的一个成员服务器,提供文件服务。这时代码就应该是:
        LogonUser("myname", "MyDomain", "password", LOGON32_LOGON_NEW_CREDENTIALS,
                    LOGON32_PROVIDER_DEFAULT, ref token);

注意,代码中是MyDomain而不是IP地址。

b、再如:

参考上面代码
            string remote = @"\\192.168.1.48\generals";
            string local = @"P:";
            string username = @"Domain\UserName";
            string password = @"Password";

如果@"\\192.168.1.48\generals"变成@"\\192.168.1.48\generals\”就会出错;
    如果是域中的用户,那么把@"Domain\UserName"变成@"UserName"就会出错。

C#访问远程主机资源的方法的更多相关文章

  1. C#访问远程主机资源的方法,多种方式

    最近要实现访问远程主机的共享目录中的一个文件.遇到了权限问题.google了一下,找到了几种解决方法,记录如下: 一.调用Net use命令 // 使用方法:        //if (Connect ...

  2. spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...

    问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js... web.x ...

  3. 2.struts2访问web资源(在struts2中获取session,request等等)

    什么是web资源:web资源就是指request,response,session,servlet的api 为什么需要访问web资源:因为图片上传,需要获取图片的目录,就需要通过action来访问we ...

  4. 在Action 中访问web资源

    1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...

  5. SecurityError Error 2148 SWF 不能访问本地资源

    错误提示: SWF 文件不能被本地访问 不能访问本地 只有仅限于文件系统的 SWF 文件和可信的本地 SWF 文件可以访问本地资源 错误信息: SecurityError: Error #2148: ...

  6. Struts2(四):在Action中如何访问Web资源

    1.什么WEB资源? HttpServletRequest,HttpServletRespone,HttpApplication,ServletContext,HttpSession等原生Servle ...

  7. SpringMVC访问静态资源的三种方式(转)

    本文转自:http://www.iigrowing.cn/springmvc_fang_wen_jing_tai_zi_yuan_de_san_zhong_fang_shi.html 如何你的Disp ...

  8. SpringMVC访问静态资源

    SpringMVC访问静态资源 在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被Dis ...

  9. [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. HDU 5628 Clarke and math dp+数学

    Clarke and math 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5628 Description Clarke is a patient ...

  2. luogu P1663 山

    题目链接 luogu P1663 山 题解 只需要求出下凸包的最低点就好了 显然是由两个斜率相反的直线相交来的 盼下最低点为直线的情况 代码 #include<cstdio> #inclu ...

  3. BZOJ2655 calc

    拉格朗日插值+dp 直接dp是n立方的,我们考虑优化. dp式子为f[i][j]=f[i-1][j-1]*j*i+f[i-1][j]表示i个元素选j个的答案 然后发现最高次就是2j次,所以我们预处理出 ...

  4. Linux下实现自动设置SSH代理

    SSH的巨大价值体现在能够配置为代理服务器上.不像在Windows下每次还需要手动登录设置,Linux有很好的工具链能够实现自动设置SSH代理,就是expect和ssh的联合使用,再加上proxych ...

  5. Gym 100646 You’ll be Working on the Railroad dfs

    You'll be Working on the Railroad 题目连接: http://codeforces.com/gym/100646/attachments Description Con ...

  6. mysql|表row_format的静态与动态,Compact

    innodb 一般对应 Compact  ,MyISAM 一般对应静态与动态 mysql中若一张表里面存在varchar.text以及其变形.blob以及其变形的字段的话,那么这个表其实也叫动态表,即 ...

  7. 采用ASP.NET IIS 注册工具 (Aspnet_regiis.exe)对web.config实行本地加密

    加密原因:我们通常将一些重要的配置信息写在Web.config里面,其中数据库链接就是这样的信息.将这些数据直接明文显示,显然不太安全. 工具: 采用ASP.NET IIS 注册工具 (Aspnet_ ...

  8. struts2中ognl标签具体解释

    <body> <s:set name="age" value="61" /> <!-- if elseif else 參数test ...

  9. 在CentOS 6 的cron/crontab中使用wine运行exe程序

    几个月前服务器的OS从Ubuntu 10.04转为CentOS 6.3,装好wine后手动运行shell脚本可以正常运行指定的exe程序(脚本和Ubuntu中的一样),于是就直接修改crontab定时 ...

  10. delphi代码实现窗口最小化,最大化,关闭消息发送

      分类: 代码实现窗口最小化,最大化,关闭 var hwnd: hwnd;//句柄 PostMessage(hwnd,WM_SYSCOMMAND, SC_MINIMIZE,0); //最小化Post ...