C#访问远程服务器共享目录下载文件到本地

今天需要解决个从远程服务器共享文件夹中下载文件到本地的小技术,在网上百度学习了下,因此下面的内容是在别人的基础上修改而来,同时还添加了从本地上传到服务器共享文件夹中的代码。我需要连接的服务器IP为:10.0.xx.xxx,共享文件夹名称为:ctec2,用户名:waveaccess, 密码:waveaccessxxx。

方法一:
建立一个控制台程序,下面直接上代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace test3
{
    class Program
    {
        static void Main(string[] args)
        {
            bool status = false;

            //连接
            status = connectState(@"\\10.0.xx.xxx\ctec2", "waveaccess", "waveaccessxxx");
            if (status)
            {
                //共享文件夹的目录
                DirectoryInfo theFolder = new DirectoryInfo(@"\\10.0.xx.xxx\ctec2\"); 
                string filename = theFolder.ToString();
                //执行方法
                TransportLocalToRemote(@"D:\readme1.txt", filename, "readme1.txt");  //实现将本地文件写入到远程服务器
                TransportRemoteToLocal(@"D:\readme.txt", filename, "readme.txt");    //实现将远程服务器文件写入到本地
            }
            else
            {
                Console.WriteLine("未能连接!");
            }
            Console.WriteLine("成功");
            Console.ReadKey();
        }

        public static bool connectState(string path)
        {
            return connectState(path, "", "");
        }
        /// <summary>
        /// 连接远程共享文件夹
        /// </summary>
        /// <param name="path">远程共享文件夹的路径</param>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns></returns>
        public static bool connectState(string path, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                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;
                proc.Start();
                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    throw new Exception(errormsg);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

        /// <summary>
        /// 从远程服务器下载文件到本地
        /// </summary>
        /// <param name="src">下载到本地后的文件路径,包含文件的扩展名</param>
        /// <param name="dst">远程服务器路径(共享文件夹路径)</param>
        /// <param name="fileName">远程服务器(共享文件夹)中的文件名称,包含扩展名</param>
        public static void TransportRemoteToLocal(string src, string dst, string fileName)  //src:下载到本地后的文件路径     dst:远程服务器路径    fileName:远程服务器dst路径下的文件名
        {
            if (!Directory.Exists(dst))
            {
                Directory.CreateDirectory(dst);
            }
            dst = dst + fileName;
            FileStream inFileStream = new FileStream(dst, FileMode.Open);    //远程服务器文件  此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错

            FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate);   //从远程服务器下载到本地的文件

            byte[] buf = new byte[inFileStream.Length];

            int byteCount;

            while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
            {

                outFileStream.Write(buf, 0, byteCount);

            }

            inFileStream.Flush();

            inFileStream.Close();

            outFileStream.Flush();

            outFileStream.Close();
            
        }

        /// <summary>
        /// 将本地文件上传到远程服务器共享目录
        /// </summary>
        /// <param name="src">本地文件的绝对路径,包含扩展名</param>
        /// <param name="dst">远程服务器共享文件路径,不包含文件扩展名</param>
        /// <param name="fileName">上传到远程服务器后的文件扩展名</param>
        public static void TransportLocalToRemote(string src, string dst, string fileName)    //src
        {
            FileStream inFileStream = new FileStream(src, FileMode.Open);    //此处假定本地文件存在,不然程序会报错   

            if (!Directory.Exists(dst))        //判断上传到的远程服务器路径是否存在
            {
                Directory.CreateDirectory(dst);
            }
            dst = dst + fileName;             //上传到远程服务器共享文件夹后文件的绝对路径

            FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);

            byte[] buf = new byte[inFileStream.Length];

            int byteCount;

            while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
            {

                outFileStream.Write(buf, 0, byteCount);

            }

            inFileStream.Flush();

            inFileStream.Close();

            outFileStream.Flush();

            outFileStream.Close();
        }
    }
}

方法二:
同样直接建立一个控制台程序测试。

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

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            if (test.TestFunctionLogin())
            {
                DateTime time1 = System.DateTime.Now;
                test.TestFunctionCopyFile(@"\\10.0.xx.xxx\ctec2\readme5.txt", "D:\\readme5.txt", true);
                DateTime time2 = System.DateTime.Now;
                test.TestFunctionCopyFile(@"\\10.0.xx.xxx\ctec2\readme6.txt", "D:\\readme6.txt", true);
                DateTime time3 = System.DateTime.Now;
                test.TestFunctionCopyFile(@"\\10.0.xx.xxx\ctec2\readme7.txt", "D:\\readme7.txt", true);
                DateTime time4 = System.DateTime.Now;

                TimeSpan ts1 = time2.Subtract(time1);
                TimeSpan ts2 = time3.Subtract(time2);
                TimeSpan ts3 = time4.Subtract(time3);

                Console.WriteLine("成功");
                Console.WriteLine("第一步耗时{0}毫秒:", ts1.TotalMilliseconds);
                Console.WriteLine("第二步耗时{0}毫秒:", ts2.TotalMilliseconds);
                Console.WriteLine("第三步耗时{0}毫秒:", ts3.TotalMilliseconds);

                test.TestFunctionQuit();
            }
            Console.ReadLine();
        }
    }

    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 static WindowsImpersonationContext impersonationContext;

        public static 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 static void undoImpersonation()
        {
            impersonationContext.Undo();
        }

        bool isImpersonated = false;
        
        public bool TestFunctionLogin()  //登陆
        {
            isImpersonated = impersonateValidUser("waveaccess", @"\\10.0.xx.xxx", "waveaccessxxx");
            return isImpersonated;
        }

        public void TestFunctionCopyFile(string path, string spath, bool istrue)
        {
            File.Copy(path, spath, istrue);
        }

        public void TestFunctionQuit()
        {
            if (isImpersonated)
            {
                undoImpersonation();
            }
        }
    }
}

方法二中加入了测试耗时的代码,可以忽略,同时,为了测试是否每次读写文件都要建立连接,将连接、读写操作和关闭连接分开了。



版权声明:本文为liuqinghui1990原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。