激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩亚洲国产 | 性生活视频一级 | 电影一级毛片 | 黄色av片在线观看 | 日韩视频一区二区三区在线观看 | 欧美一级精品 | 欧美成年视频 | 亚洲成人黄色片 | 99久久超碰中文字幕伊人 | 成年人小视频在线观看 | 女女久久| 欧美另类在线视频 | 久久久久久久黄色片 | 成人在线视频免费观看 | 在线a毛片 | 国产自在自线午夜精品视频在 | 免费一级欧美 | 美国av片在线观看 | 色婷婷久久久亚洲一区二区三区 | 日日摸夜夜添夜夜添牛牛 | 日韩美香港a一级毛片 | 色网站免费观看 | 免费看毛片的网站 | 911精品影院在线观看 | 天天色宗合 | 成人免费一区二区三区 | 91精品国产九九九久久久亚洲 | 日韩黄色片在线观看 | 亚洲精品v天堂中文字幕 | 精品国产一区二区三区在线观看 | 亚洲精品欧美在线 | 亚洲一区在线免费视频 | vidz 98hd| 成人毛片100部免费观看 | 91看片成人 | 一区二区三区日韩视频在线观看 | 中文国产在线视频 | 中文字幕在线永久视频 | 视频一区二区在线播放 | 黄色a级片视频 | 一区二区三区四区在线观看视频 |