一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C#如何訪問共享文件夾或者磁盤

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

2022-02-24 13:59邊界流浪者 C#

這篇文章主要為大家詳細介紹了C#訪問共享文件夾或者磁盤,需要用戶名密碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

SharedTool:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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); 
    
  
}

 案例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_16542775/article/details/52779812

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国内老司机精品视频在线播出 | 久久人妻少妇嫩草AV無碼 | www.99热.com| 亚洲精品国产精品麻豆99 | 欧美亚洲国产精品久久久 | caoporm碰最新免费公开视频 | 亚洲欧洲日产国码无码av | 免费看国产精品久久久久 | www.亚洲视频.com| 香蕉草莓视频 | 高清国语自产拍免费视频国产 | 国产免费看视频 | 四虎2020紧急免费入口 | 日本伊人色| 精品国产成a人在线观看 | 小早川怜子在线播放精品 | 欧美视频一区二区三区在线观看 | 青草福利在线 | 青苹果乐园影院免费观看完整版 | 美女岳肉太深了使劲 | 精品在线看 | 久久99re8热在线播放 | 男男gaygays18中国 | www免费插插视频 | 日本午夜影院 | 色老板在线视频观看 | 夫妻性生活免费在线观看 | 日本无遮挡吸乳视频看看 | 大乳一级一区二区三区 | 美女福利网站 | nxgx欧美 | 99re8在这里只有精品2 | 高清不卡免费一区二区三区 | 日本人做受全过程视频 | 美女流白浆 | 国产精品免费精品自在线观看 | 欧美另类z0zxi | 极品一区 | 欧美日韩国产在线人成 | 亚洲成人精品久久 | 国产亚洲欧美一区二区三区 |