国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#如何訪問共享文件夾或者磁盤

2019-10-29 21:01:48
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C#訪問共享文件夾或者磁盤的具體代碼,供大家參考,具體內容如下

SharedTool:

using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Runtime.InteropServices;    namespace ConsoleApplication5  {    public class SharedTool : IDisposable    {      // obtains user token         [DllImport("advapi32.dll", SetLastError = true)]      static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,        int dwLogonType, int dwLogonProvider, ref IntPtr phToken);        // closes open handes returned by LogonUser         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]      extern static bool CloseHandle(IntPtr handle);        [DllImport("Advapi32.DLL")]      static extern bool ImpersonateLoggedOnUser(IntPtr hToken);        [DllImport("Advapi32.DLL")]      static extern bool RevertToSelf();      const int LOGON32_PROVIDER_DEFAULT = 0;      const int LOGON32_LOGON_NEWCREDENTIALS = 9;//域控中的需要用:Interactive = 2         private bool disposed;        public SharedTool(string username, string password, string ip)      {        // initialize tokens           IntPtr pExistingTokenHandle = new IntPtr(0);        IntPtr pDuplicateTokenHandle = new IntPtr(0);          try        {          // get handle to token             bool bImpersonated = LogonUser(username, ip, password,            LOGON32_LOGON_NEWCREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);            if (bImpersonated)          {            if (!ImpersonateLoggedOnUser(pExistingTokenHandle))            {              int nErrorCode = Marshal.GetLastWin32Error();              throw new Exception("ImpersonateLoggedOnUser error;Code=" + nErrorCode);            }          }          else          {            int nErrorCode = Marshal.GetLastWin32Error();            throw new Exception("LogonUser error;Code=" + nErrorCode);          }        }        finally        {          // close handle(s)             if (pExistingTokenHandle != IntPtr.Zero)            CloseHandle(pExistingTokenHandle);          if (pDuplicateTokenHandle != IntPtr.Zero)            CloseHandle(pDuplicateTokenHandle);        }      }        protected virtual void Dispose(bool disposing)      {        if (!disposed)        {          RevertToSelf();          disposed = true;        }      }        public void Dispose()      {        Dispose(true);      }    }  } 

 案例:

using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.IO;    namespace ConsoleApplication5  {    class Program    {      static void Main(string[] args)      {        using (SharedTool tool = new SharedTool("administrator", "12345678", "192.168.1.101"))        {          string selectPath = @"//192.168.1.101/c$";            var dicInfo = new DirectoryInfo(selectPath);//選擇的目錄信息            DirectoryInfo[] dic = dicInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly);          foreach (DirectoryInfo temp in dic)          {            Console.WriteLine(temp.FullName);          }            Console.WriteLine("---------------------------");          FileInfo[] textFiles = dicInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取所有目錄包含子目錄下的文件          foreach (FileInfo temp in textFiles)          {            Console.WriteLine(temp.Name);          }        }        Console.ReadKey();      }    }  }  

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五原县| 永福县| 香港| 光山县| 崇文区| 临邑县| 石门县| 陇南市| 句容市| 凤山县| 阿拉善盟| 苗栗县| 丰原市| 涟水县| 修武县| 汝南县| 阿拉善左旗| 诸城市| 枣强县| 隆安县| 辽阳市| 垫江县| 大邑县| 抚松县| 仁化县| 裕民县| 柏乡县| 获嘉县| 阳山县| 宁武县| 石家庄市| 平乡县| 广水市| 舞钢市| 武平县| 紫云| 富蕴县| 平谷区| 三原县| 屏边| 涟源市|