首页 > 系统相关 >.net访问远程共享的Windows文件

.net访问远程共享的Windows文件

时间:2024-07-29 16:56:45浏览次数:9  
标签:string Windows proc IpAddress StartInfo net password true 共享

可以使用cmd连接创建Windows凭据

public static bool ConnectPwd(string IpAddress, string userName, string password)
{
	bool Flag = faslse;
	Process proc = new Process();
	try
	{
		proc.StartInfo.FileName = "cmd.exe";
		proc.StartInfo.UseShellExecute = false;
		proc.StartInfo.RedirectStandardInput = true;
		proc.StartInfo.RedirectStandardOutput = true;
		proc.StartInfo.RedirectStandardError = true;
		proc.StartInfo.CreateNoWindow = true;
		proc.Start();
		//如果当前IP已有连接,则断开连接,以免冲突
		string dosLine = @"net use \" + IpAddress + " /delete";
		proc.StandardInput.WriteLine(dosLine);
		//需要关闭后等待,不然会报错
		proc.StandardInput.WriteLine("exit");
		while (!proc.HasExited)
		{
			proc.WaitForExit(1000);
		}
		proc.Start();
		///创建凭据
		dosLine = @"net use \\" + IpAddress + " " + password + " /user:" + userName;
		proc.StandardInput.WriteLine(dosLine);
		proc.StandardInput.WriteLine("exit");
		while (!proc.HasExited)
		{
			proc.WaitForExit(1000);
		}
		string errormsg = proc.StandardError.ReadToEnd();
		proc.StandardError.Close();
		if (string.IsNullOrEmpty(errormsg))
		{
			Flag = true;
		}
		else
		{
			throw new Exception(errormsg);
		}
	}
	catch (Exception ex)
	{
		throw ex;
	}
	finally
	{
		proc.Close();
		proc.Dispose();
	}
	return Flag;
	}
}

创建凭据后可直接访问远程文件了

string IpAddress = "127.0.0.1";//IP地址
string userName = "Administrator"; //用户名
string password = "pwd";//密码
if (ConnectPwd(IpAddress, userName, password) == true)
{
	string filePath = @"\\127.0.0.1\address\read.txt";//文件地址
	StreamReader sr = new StreamReader(filePath, this.encoding);
	//根据需求处理文件
}

标签:string,Windows,proc,IpAddress,StartInfo,net,password,true,共享
From: https://www.cnblogs.com/youngleduo/p/18330484

相关文章

  • c# 多线程环境下控制对共享资源访问的办法
    Monitor:定义:Monitor 是C#中最基本的同步机制,通过 Enter 和 Exit 方法来控制对共享资源的访问。它提供了排他锁的功能,确保在任何时刻只有一个线程可以访问共享资源。优点:简单易用,适合对临界区进行粗粒度的同步控制。缺点:只能实现排它锁,不能实现读写锁,性能相对较低。......
  • Pentester Academy -Windows API Exploitation Recipes: Processes, Tokens and Memor
    早年为PentesterAcademy(https://www.pentesteracademy.com/),如今为INE(https://ine.com/)002安装VS社区版https://visualstudio.microsoft.com/zh-hans/003processlistingapi正在运行的是什么:服务,AV,HIDS/IPS等其他attack开始的点:进程注入,内存dump/修改,TokenSt......
  • 基于EasyTcp4Net开发一个功能较为完善的去持久化聊天软件
    之前自己写了一篇介绍TCP的一些常用的功能介绍和特征,并且用代码做了示例,最终开发了一个EasyTcp4Net的TCP工具库,其最大的特色就是使用了微软提供的高性能库中的一些数据结构来处理TCP数据。最近辞职待业在家,也没啥事做,就利用自己写的TCP通讯库基础上开发了一个示例的聊天程序,功能......
  • 使用 Python 中的多处理防止共享内存中的数据损坏?
    我目前正在开发一个多处理Python程序,其中每个进程将其索引作为连续的4字节整数写入共享内存。并且有一个读取器可以在没有任何锁的情况下读取其他进程的索引。因为我没有使用任何同步原语,所以我担心读取器进程可能会由于逐字节写入内存而读取损坏的数据(例如,一个索引的前2个......
  • 【K8s】专题七(4):Kubernetes 服务发现之 Ingress 进阶
    以下内容均来自个人笔记并重新梳理,如有错误欢迎指正!如果对您有帮助,烦请点赞、关注、转发!欢迎扫码关注个人公众号!目录一、官方文档二、Ingress进阶使用(示例)1、Ingress实现重定向2、Ingress实现路由跳转3、Ingress实现自定义配置4、Ingress实现CORS5、Ingress实......
  • windows编译ZLMediaKit流媒体服务webrtc
    环境说明ZLMediaKit编译需要的软件visualstudio 2022cmake 3.29.0-rc2OpenSSL 1.1.1w(不想踩坑的话安装这个版本)libsrtp 2.6.0ZLMediaKit编译后运行需要libsrtp 编译后且配置环境变量ZLMediaKit 编译后文件cmakevisualstuido20222,自带cmakecmake可以到这两个地方......
  • 从流读取时,PyAudio Stream 导致 Windows 堆损坏(-1073740940 (0xC0000374))
    我在尝试读取PyAudio的Stream时遇到了问题。它因退出代码而崩溃-1073740940这是一个Windows堆损坏错误0xC0000374它发生在我从PyAudio流读取的行中,如下所示:stream.read(chunk_size)我也看到它崩溃了-1073741819ACCESS_VIOLATION_......
  • Windows下捕获6GHz报文/无线抓包(WiFi6E网卡mt7921au)
    目录16GHz概述1.16GHz与WiFi6E1.26GHz国际使用情况1.36GHz中国使用情况1.46GHz信道一览表26GHz抓包操作2.1抓包网卡推荐2.2抓包软件使用2.2.1VMware虚拟机安装2.2.2WinSniffer程序使用2.2.3WireShark远程抓包2.3扫描软件推荐2.3.1Windows2.3.2WiFi......
  • 【全过程】windows GPU训练大模型的前期准备教程
    CUDA下载及安装下载显卡驱动点这里进入之后点画圈的地方,然后打开下载的文件,会帮你自动下载和安装适配你显卡的驱动程序(这里不是特别重要,就简单带过)点击桌面左下角小箭头,出现花圈的标志,也就是英伟达的logo,说明驱动安装成功安装CUDA接下来到了重头戏,cuda的安装查看本机......
  • ubuntu22.04通过netplan配置网络
    1.以前的网络配置ubuntu系统里通常在/etc/network/interfaces里配置好IP等信息interfaces文件配置内容大概如下:autoenp0s3ifaceenp0s3inetstaticaddress10.0.2.15netmask255.255.255.0gateway10.0.2.1dns-nameservers218.85.157.99保存关闭后,使用sudosystemctl......