C#如何訪問共享文件夾或者磁盤
更新時(shí)間:2018年05月21日 14:14:46 作者:邊界流浪者
這篇文章主要為大家詳細(xì)介紹了C#訪問共享文件夾或者磁盤,需要用戶名密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C#訪問共享文件夾或者磁盤的具體代碼,供大家參考,具體內(nèi)容如下
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(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中實(shí)現(xiàn)輸入漢字獲取其拼音(漢字轉(zhuǎn)拼音)的2種方法
這篇文章主要介紹了C#中實(shí)現(xiàn)輸入漢字獲取其拼音(漢字轉(zhuǎn)拼音)的2種方法,本文分別給出了使用微軟語言包、手動編碼實(shí)現(xiàn)兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2015-01-01實(shí)例詳解C#實(shí)現(xiàn)http不同方法的請求
本篇文章給大家分享了C#實(shí)現(xiàn)http不同方法的請求的相關(guān)知識點(diǎn)以及實(shí)例代碼,有需要的朋友參考下。2018-07-07C#調(diào)用halcon實(shí)現(xiàn)使用鼠標(biāo)滾輪對圖片進(jìn)行縮放顯示
這篇文章主要為大家詳細(xì)介紹了C#如何調(diào)用halcon實(shí)現(xiàn)使用鼠標(biāo)滾輪對圖片進(jìn)行縮放顯示,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03C#隱藏手機(jī)號、郵箱等敏感信息的實(shí)現(xiàn)方法
這篇文章主要介紹了C#隱藏手機(jī)號、郵箱等敏感信息的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-09-09