This article has shared the specific code of C# read and write shared folder for your reference. The specific content is as follows
This test is divided into the following steps:
1. Set up a shared folder on the server. Here, my server IP address is 10.80.88.180, the name of the shared folder is test, and there are two files in the test: and, access permissions, the user name is admin, and the password is admin.
2. Create a new webapplication project, add a listbox to the front page, and the ID is ListBox1.
3. Add the background code as follows: The function contained in it is to read files, here taking reading good files as an example; writing files, here taking writing bad files as an example; and also listing the file names under the test folder into listbox.
using System; using ; using ; using ; using ; using ; using ; using ; using ; namespace WebApplication2 { public class FileShare { public FileShare() { } public static bool connectState(string path) { return connectState(path,"",""); } public static bool connectState(string path,string userName,string passWord) { bool Flag = false; Process proc = new Process(); try { = ""; = false; = true; =true; =true; =true; (); string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES"; (dosLine); ("exit"); while (!) { (1000); } string errormsg = (); (); if ((errormsg)) { Flag = true; } else { throw new Exception(errormsg); } } catch (Exception ex) { throw ex; } finally { (); (); } return Flag; } //read file public static void ReadFiles(string path) { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader(path)) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = ()) != null) { (line); } } } catch (Exception e) { // Let the user know what went wrong. ("The file could not be read:"); (); } } //write file public static void WriteFiles(string path) { try { // Create an instance of StreamWriter to write text to a file. // The using statement also closes the StreamWriter. using (StreamWriter sw = new StreamWriter(path)) { // Add some text to the file. ("This is the "); ("header for the file."); ("-------------------"); // Arbitrary objects can also be written to the file. ("The date is: "); (); } } catch (Exception e) { // Let the user know what went wrong. ("The file could not be read:"); (); } } } public partial class _Default : { protected void Page_Load(object sender, EventArgs e) { bool status = false; //Connect the shared folder status = (@"\\10.80.88.180\test", "admin", "admin"); if (status) { DirectoryInfo theFolder = new DirectoryInfo(@"\\10.80.88.180\test"); //First test the read file and connect the directory path to the file name string filename = ()+"\\"; (filename); //Test the file and spell out the complete path filename = () + "\\"; (filename); //Travel through the shared folder and list the file list under the shared folder to listbox foreach (FileInfo nextFile in ()) { (); } } else { ("Failed to connect!"); } } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.