In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to create and access a network hard disk in C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
In some scenarios, we need to remotely access the shared hard disk space, so as to achieve convenient and quick access to remote files. For example, there is a computer in the company's local area network that stores a large number of files. if other computers want to access the files of the computer, they can do so through the network hard disk, which is the same as accessing the local hard disk, which is very convenient and fast. Through C#, we can realize the automatic management of network hard disk.
Create a class WebNetHelper and add the following member variables and member functions to the class
The shared disk of the static public WebNetHelper wnh=null;private string remoteHost;// remote host, such as\ 1.1.1.1\ ccprivate string destionDisk;// disk letter to be accessed private string remoteUserName;// login user name of the remote host private string passWord;// login password of the remote host
Access to the network hard disk
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;}}
Disconnect network mapping
Public bool Disconnect () {try {string cmdString=string.Format (@ "net use {0}: / delete > NUL", this.DestionDisk); this.WriteStringToComman (cmdString); return true;} catch (Exception) {throw e;}}
Execute the CMD command
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 ();}}
The test function is then the procedure used for the test. \ 1.1.1.1\ cc is the address of the network hard disk, K is the letter of the drive to be mapped, "Noner" is the login name of the remote host, and "uiosdsau" is the password of the remote host. The Test function is the first line of reading the contents of the ImbaMallLog.txt file under the network hard disk.
/ Test function, using this class / 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);}} this is the end of the article on "how to create and access a network hard disk in C#". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.