C#如何訪問(wèn)共享文件夾或者磁盤(pán)
本文實(shí)例為大家分享了C#訪問(wèn)共享文件夾或者磁盤(pán)的具體代碼,供大家參考,具體內(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();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 線(xiàn)程相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了C# 線(xiàn)程相關(guān)知識(shí),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
分享一個(gè)C#編寫(xiě)簡(jiǎn)單的聊天程序(詳細(xì)介紹)
這是一篇基于Socket進(jìn)行網(wǎng)絡(luò)編程的入門(mén)文章,我對(duì)于網(wǎng)絡(luò)編程的學(xué)習(xí)并不夠深入,這篇文章是對(duì)于自己知識(shí)的一個(gè)鞏固,同時(shí)希望能為初學(xué)的朋友提供一點(diǎn)參考。文章大體分為四個(gè)部分:程序的分析與設(shè)計(jì)、C#網(wǎng)絡(luò)編程基礎(chǔ)(篇外篇)、聊天程序的實(shí)現(xiàn)模式、程序?qū)崿F(xiàn)2015-12-12
DevExpress之ChartControl的SeriesTemplate實(shí)例
這篇文章主要介紹了DevExpress之ChartControl的SeriesTemplate用法實(shí)例,實(shí)現(xiàn)了餅狀Series百分比顯示的效果,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10
C# 鼠標(biāo)穿透窗體功能的實(shí)現(xiàn)方法
通過(guò)以下代碼,在窗體啟動(dòng)后調(diào)用方法SetPenetrate() 即可實(shí)現(xiàn)窗體的穿透功能,有需要的朋友可以參考一下2013-10-10
opencvsharp瑕疵檢測(cè)的實(shí)現(xiàn)示例
本文主要介紹了opencvsharp瑕疵檢測(cè)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C# 將學(xué)生列表轉(zhuǎn)換為字典的實(shí)現(xiàn)
在開(kāi)發(fā)應(yīng)用程序時(shí),管理和處理數(shù)據(jù)結(jié)構(gòu)是非常重要的一環(huán),本文就來(lái)介紹一下C# 將學(xué)生列表轉(zhuǎn)換為字典的實(shí)現(xiàn),感興趣的可以了解一下2025-01-01
親自教你實(shí)現(xiàn)棧及C#中Stack源碼分析
大家都知道棧的實(shí)現(xiàn)方式有兩種,一種是基于數(shù)組實(shí)現(xiàn)的順序棧,另一種是基于鏈表實(shí)現(xiàn)的鏈?zhǔn)綏?。這篇文章主要介紹了手把手教你實(shí)現(xiàn)棧以及C#中Stack源碼分析,需要的朋友可以參考下2021-09-09

