首頁 後端開發 C#.Net教程 C#存取遠端主機資源的多種方法分享

C#存取遠端主機資源的多種方法分享

Apr 25, 2017 am 09:55 AM

最近要实现访问远程主机的共享目录中的一个文件。遇到了权限问题。google了一下,找到了几种解决方法,记录如下:
一、调用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);
                ... ...
登入後複製

四、角色模拟

/// <summary>
    /// 身份模拟实现远程资源访问
    /// </summary>
    public class FileImpersonation
    {
        // 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;
        /// <summary>
        /// 身份模拟
        /// </summary>
        /// <param name="userName">独立服务器用IP,域环境就用域名</param>
        /// <param name="domain"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        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)
                        {
                            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;
        }
        /// <summary>
        /// 取消模拟身份
        /// </summary>
        public void undoImpersonation()
        {
            impersonationContext.Undo();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="domain">独立服务器用IP,域环境就用域名</param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="act">需要执行的操作</param>
        public void ImpersonateFunc(string username, string domain, string password, Action act)
        {
            bool isImpersonated = false;
            try
            {
                if (impersonateValidUser(username, domain, password))
                {
                    isImpersonated = true;
                    //File.Copy(@"\\192.168.1.48\generals\now.htm", "c:\\now.htm", true);
                    act();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (isImpersonated)
                    undoImpersonation();
            }
        }
        //
    }
 FileImpersonation imp = new FileImpersonation();
            imp.ImpersonateFunc("Administrator", "192.168.0.125", "xxxx", () =>
            {
                var source = new DirectoryInfo(@"D:\HR");
                string targetFolder = @"\\192.168.0.125\d$\HR";
                if (!Directory.Exists(targetFolder))
                {
                    Directory.CreateDirectory(targetFolder);
                }
                foreach (FileInfo item in source.GetFiles())
                {
                    string targertFile = Path.Combine(targetFolder, item.Name);
                    File.Copy(item.FullName, targertFile, true);
                }
            });
登入後複製

五、比较

方法一通过调用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#存取遠端主機資源的多種方法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用 C# 的活動目錄 使用 C# 的活動目錄 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

C# 序列化 C# 序列化 Sep 03, 2024 pm 03:30 PM

C# 序列化指南。這裡我們分別討論C#序列化物件的介紹、步驟、工作原理和範例。

C# 中的隨機數產生器 C# 中的隨機數產生器 Sep 03, 2024 pm 03:34 PM

C# 隨機數產生器指南。在這裡,我們討論隨機數產生器的工作原理、偽隨機數和安全數的概念。

C# 資料網格視圖 C# 資料網格視圖 Sep 03, 2024 pm 03:32 PM

C# 資料網格視圖指南。在這裡,我們討論如何從 SQL 資料庫或 Excel 檔案載入和匯出資料網格視圖的範例。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範例和程式碼實作。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範例和程式碼實作。

C# 中的質數 C# 中的質數 Sep 03, 2024 pm 03:35 PM

C# 質數指南。這裡我們討論c#中素數的介紹和範例以及程式碼實作。

c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

See all articles