C # read/write shared folders,

Source: Internet
Author: User

C # read/write shared folders,
The test involves the following steps:

1. Set a shared folder on the server. Here, my server IP address is 10.200.8.73, the shared folder name is share, and the access permission is. The username is administrator and the password is 11111111.

2. Create a console project.

3. Add the background Code as follows:

Class Program {static void Main (string [] args) {bool status = false; // connect to the shared folder status = connectState (@ "\ 10.200.8.73 \ share", "administrator ", "11111111"); if (status) {// directory of the shared folder DirectoryInfo theFolder = new DirectoryInfo (@ "\ 10.200.8.73 \ share "); // relative path of the shared folder string fielpath = @ "\ 123 \ 456 \"; // obtain the path of the saved file string filename = theFolder. toString () + fielpath; // The execution method Transport (@ "D: \ 1.jpg", filename," 1.j Pg ");} else {// ListBox1.Items. Add (" unable to connect! ");} Console. readKey ();} public static bool connectState (string path) {return connectState (path ,"","");} /// <summary> /// connect to the remote shared folder /// </summary> /// <param name = "path"> path of the remote shared folder </param> /// <param name = "userName"> User name </param> /// <param name = "passWord"> passWord </param> /// <returns> </ returns> public static bool connectState (string path, string userName, string passWord) {bool Flag = false; Proc Ess 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 (); string dosLine = "net use" + path + "" + passWord + "/user:" + userName; proc. standardInput. writeLin E (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 ;}/// <summary> /// Save the local content to the remote folder, or you can download the file from a remote folder to your local device. /// </summary> /// <param name = "src"> path of the file to be saved Path. If you save the file to the shared folder, the path is the local file path, for example, @ "D: \ 1.avi" </param> // <param name = "dst"> Save the file path, excluding the name and extension </param> /// <param name = "fileName"> Save the file name and extension </param> public static void Transport (string src, string dst, string fileName) {FileStream inFileStream = new FileStream (src, FileMode. open); if (! Directory. exists (dst) {Directory. createDirectory (dst);} dst = dst + fileName; FileStream outFileStream = new FileStream (dst, FileMode. openOrCreate); byte [] buf = new byte [inFileStream. length]; int byteCount; while (byteCount = inFileStream. read (buf, 0, buf. length)> 0) {outFileStream. write (buf, 0, byteCount);} inFileStream. flush (); inFileStream. close (); outFileStream. flush (); outFileStream. close ();}}View Code

Follow these steps to read and write shared network files.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.