C#如何创建及访问网络硬盘
发表于:2024-10-28 作者:千家信息网编辑
千家信息网最后更新 2024年10月28日,这篇文章将为大家详细讲解有关C#如何创建及访问网络硬盘,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在某些场景下我们需要远程访问共享硬盘空间,从而实现方便快捷的访问
千家信息网最后更新 2024年10月28日C#如何创建及访问网络硬盘
这篇文章将为大家详细讲解有关C#如何创建及访问网络硬盘,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
在某些场景下我们需要远程访问共享硬盘空间,从而实现方便快捷的访问远程文件。比如公司局域网内有一台电脑存放了大量的文件,其它电脑想要访问该电脑的文件,就可以通过网络硬盘方式实现,跟访问本地硬盘同样的操作,很方便且快速。通过C#我们可以实现网络硬盘的自动化管理。
创建一个类WebNetHelper,在类中加入如下成员变量及成员函数,
static public WebNetHelper wnh=null;private string remoteHost;//远程主机的共享磁盘,形式如\\1.1.1.1\ccprivate string destionDisk;//要访问的磁盘盘符private string remoteUserName;//登录远程主机的用户名private string passWord;//登录远程主机的密码
访问网络硬盘,
public bool Connect(){ try { string cmdString = string.Format(@"net use {1}: {0} {3} /user:{2} >NUL",this.RemoteHost, this.DestionDisk, this.RemoteUserName,this.PassWord); this.WriteStringToComman(cmdString); return true; } catch (Exception e) { throw e; }}
断开网络映射,
public bool Disconnect(){ try { string cmdString=string.Format(@"net use {0}: /delete >NUL",this.DestionDisk); this.WriteStringToComman(cmdString); return true; } catch (Exception e) { throw e; }}
执行CMD命令,
private bool WriteStringToComman(string cmdString){ bool Flag = true; Process proc = new Process(); 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; try { proc.Start(); string command = cmdString; proc.StandardInput.WriteLine(command); command = "exit"; proc.StandardInput.WriteLine(command); while (proc.HasExited == false) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); if (errormsg != "") Flag = false; proc.StandardError.Close(); return Flag; } catch (Exception e) { throw e; } finally { proc.Close(); proc.Dispose(); }}
然后test函数为测试使用的过程。\\1.1.1.1\cc为网络硬盘地址,K为要映射的盘符,"Noner"为远程主机的登录名,"uiosdsau"为远程主机的密码。Test函数为读取网络硬盘下的ImbaMallLog.txt文件内容的第一行。
////// 测试函数,测试使用该类/// private void test(){ try { if (!Directory.Exists(@"K:\")) { WebNetHelper.wnh = new WebNetHelper(@"\\1.1.1.1\cc", "K", "Noner", "uiosdsau"); WebNetHelper.wnh.Connect(); } StreamReader sr = new StreamReader(@"K:\ImbaMallLog.txt"); string tt = sr.ReadLine(); //MessageBox.Show(tt); sr.Close(); sr.Dispose(); if (WebNetHelper.wnh != null) { WebNetHelper.wnh.Disconnect(); } } catch (Exception e) { //MessageBox.Show(e.Message); }}
关于"C#如何创建及访问网络硬盘"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
硬盘
网络
主机
函数
文件
C#
电脑
篇文章
测试
登录
内容
密码
成员
更多
磁盘
不错
实用
方便快捷
一行
公司
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
剑灵 数据库
数据库查看ip地址
网络安全宣传公园
大成老旧数据库账号
java中怎么输出数据库表
闵行区正规软件开发产品介绍
浪潮服务器能装xp系统么
网络安全公司后几年战略规划
网络安全运营证
虹口区软件开发技术价格
场景识别 数据库
节目数据库主要提供
数据库多并发控制
光猫isp服务器连接不到地址
软件开发使用环境
boa服务器搭建
网络安全与执法大专
广州定制化软件开发
绝地求生日韩服务器设置
云服务器有哪几个区域
廊坊网络安全宣传活动
赛鲁班网络技术有限公司湖滨
菲律宾机械硬盘服务器多少钱
空指针服务器报错505
csgo社区服务器mod
服务器倒卖
网络安全open是什么意思
节点机器与服务器对时
如何往数据库添加文章
如何查看数据库日志