C# 访问共享文件夹或者磁盘(需要用户名密码)
来源:互联网 发布:holi天气数据来源 编辑:程序博客网 时间:2024/05/16 11:47
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(); } } }
0 0
- C# 访问共享文件夹或者磁盘(需要用户名密码)
- C# 访问共享文件夹或者磁盘(需要用户名密码)
- C# 访问共享文件夹或者磁盘(需要用户名密码)
- 访问共享文件夹需要密码?
- win7访问网络共享文件提示用户名或者密码错误
- 如何设置共享文件夹访问不需要输入用户名和密码
- 如何更改访问共享文件夹的用户名和密码
- 如何更改访问共享文件夹的用户名和密码
- 使用需要用户名和密码访问的samba共享
- IIS 访问需要用户名和密码 (需要进行身份验证)
- IIS 访问需要用户名和密码 (需要进行身份验证)
- 解决win7访问xp共享的文件时,未知用户名或者密码错误的问题
- 带用户名密码---远程访问另一台主机中共享文件夹中文件
- Windows7在访问网络共享文件夹提示“登陆失败:未知的用户名或密码错误”
- .net使用用户名和密码访问远程共享文件夹的两种方法
- MAC通过SMB访问windows共享文件夹:为何用户名密码都正确却无法通过
- windows系统共享文件夹访问无需验证(输入用户名和密码)即可进入
- 当访问共享文件夹时需输入用户名和密码的解决办法
- CSS (一)CSS的基本认识与Css选择器
- 单例的简单实现
- 高考语文的满分文言文神作
- NSArray 倒序 输出
- iOS导航栏返回手势
- C# 访问共享文件夹或者磁盘(需要用户名密码)
- QObject::sender()和QCustomPlot::setInteractions
- Caffe自己修改训练方法
- Hibernate从入门到精通(十一)多对多双向关联映射
- thinkphp学习笔记
- 可拖拽移位,可删除的listview功能 recyclerView
- 浏览器如何访问最新js文件
- //选择完下拉框后加载数据
- 配置vim+NERDTree+ Source Explorer+ Tag List