[C#]访问共享文件夹或者磁盘(需要用户名密码)
有项目要求使用对方本地管理员访问访问对方D盘,网上找到一段API,刚开始可以使用一段时间,升级到1903就失效了,一脸懵逼啊
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 = ;
const int LOGON32_LOGON_NEWCREDENTIALS = ;//域控中的需要用:Interactive = 2
private bool disposed; public SharedTool(string username, string password, string ip)
{
// initialize tokens
IntPtr pExistingTokenHandle = new IntPtr();
IntPtr pDuplicateTokenHandle = new IntPtr(); 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", "", "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();
}
}
}
[C#]访问共享文件夹或者磁盘(需要用户名密码)的更多相关文章
- (转)WIN7更改用户名访问共享文件夹
原文地址: http://ryy8013.blog.163.com/blog/static/71729589201210610533778/ 一直以来,windows7客户端访问windows ser ...
- IIS/ASP.NET访问共享文件夹的可用方式
[截止2014-10-14] 网上搜索了很多篇文章,所提及的总共有两种方式: 1.Asp.Net模拟登陆: 例如: 实战ASP.NET访问共享文件夹(含详细操作步骤) 实现一个2008serve的II ...
- VirtualBox中的Ubuntu没有权限访问共享文件夹/media/sf_bak
之前已经搞定可以自动共享文件夹了,但是现在发现无法去访问,非root用户下,使用“ls /media/sf_bak”提示没有权限,当然如果切换到root,是可以的. [解决过程]1.把普通用户名加入到 ...
- 清除在Windows下访问共享文件夹时的登录信息
清除在Windows下访问共享文件夹时的登录信息 在实际工作中,经常需要访问局域网内其他机子上的共享文件夹,例如\\192.168.1.100\d$ , 首次访问时,需要输入用户名和密码才可以进入,即 ...
- PC问题-可以PING通IP,PING名字不通,可以远程,但不能访问共享文件夹?
问题现象:可以PING通IP,PING名字不通,可以远程,但不能访问共享文件夹? 问题原因:目标主机中NetLogon服务未启动. 问题处理:远程打开目标主机,“我的电脑-管理-服务-启动(改为自动) ...
- [转]Win10企业版无法访问共享文件夹
Win10系统电脑在更新后,当我们访问共享文件夹时可能会出现如下图所示窗口,导致我们无法访问.那么这个问题如何解决呢?具体如下:1. 首先我们按“Windows+R键”打开运行窗口.2. 在该窗口文本 ...
- 实战ASP.NET访问共享文件夹(含详细操作步骤)
博客园找找看(http://zzk.cnblogs.com)的索引文件占用空间太大,需要移至另外一台服务器,所以要解决"在ASP.NET中通过共享文件夹访问索引文件"的问题. 假设 ...
- 第一篇-ubuntu18.04访问共享文件夹
1. 在访问Windows共享资料之前,请确保Windows共享是可用的.Linux访问Windows共享或者LInux共享资料给Windows时,都使用Samba软件 rpm -qa | grep ...
- Windows无法访问共享文件夹
问题描述 今天打开vss连接代码,提示如下信息 解决办法 可行:重置登录用户信息 原博文 https://zhidao.baidu.com/question/1174230805440255699.h ...
随机推荐
- Ubuntu桌面版与服务器版的区别(转)
Ubuntu桌面版vs服务器版 提到安装Linux,Ubuntu可谓是最受欢迎的.为了满足每个人的需求,出现了不少版本或风格的Ubuntu:其中两项便是桌面版与服务器版.只要发布版本号一致,这两者从核 ...
- 检查并解决CentOS 7 安装Tomcat是否成功
参考网址 https://blog.csdn.net/Blue_Sky_rain/article/details/91348791
- shiro中接入单点登录功能
最近新建的系统中使用了shiro,而shiro框架中包含登录认证和鉴权的功能,因为我们系统要统一接入公司内部的单点登录(isso)系统,所以通过isso的登录用户,需要在shiro中置为已认证,一下提 ...
- Ajax案例-基于HTML,以GET或POST方式,检查注册用户名是否在数据库中已存在
08_register.jsp <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTY ...
- 日志文件---log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.a ...
- springmvc集成swagger
1.保证项目为maven项目 2.导入jar包依赖 <dependency> <groupId>io.springfox</groupId> <artifac ...
- Oracle面试题
0.绑定变量的优缺点及使用场合分别是什么? 优点:能够避免SQL的硬解析以及与之相关的额外开销(SQL语法.语义的分析.逻辑分析.生成较佳的执行计划等开销),提高执行效率. 缺点:如果在表存在数据倾斜 ...
- mysql大量locked的一个案例
转自 http://cyxu2004.blog.163.com/blog/static/45768718201222133440986/
- 如何查看linux系统安装时间
第一种方法: 先查看系统盘挂到哪个分区上,然后用 dumpe2fs 查看这个磁盘分区 创建的时间,即可查出此 服务器 系统安装的时间.() # dumpe2fs /dev/sda3|grep -i ...
- MariaDB知识点总结02--日志+备份
一.日志 1.查询日志 记录每一条sql语句,建议不开启,因为如果访问量较大,会占用相当大的资源,影响性能; vim /etc/my.cnf.d/server.cnf general_log = ON ...