C#读写共享文件夹

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.Text;
  8 using System.Diagnostics;
  9 using System.IO;
 10 
 11 
 12 namespace WebApplication2
 13 {
 14 
 15     public class FileShare
 16     {
 17         public FileShare() { }
 18 
 19         public static bool connectState(string path)
 20         {
 21             return connectState(path,"","");
 22         }
 23 
 24         public static bool connectState(string path,string userName,string passWord)
 25          {
 26             bool Flag = false;
 27             Process proc = new Process();
 28             try
 29             {
 30                 proc.StartInfo.FileName = "cmd.exe";
 31                 proc.StartInfo.UseShellExecute = false;
 32                 proc.StartInfo.RedirectStandardInput = true;
 33                 proc.StartInfo.RedirectStandardOutput=true;
 34                 proc.StartInfo.RedirectStandardError=true;
 35                 proc.StartInfo.CreateNoWindow=true;
 36                 proc.Start();
 37                 string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
 38                 proc.StandardInput.WriteLine(dosLine);
 39                 proc.StandardInput.WriteLine("exit");
 40                 while (!proc.HasExited)
 41                 {
 42                     proc.WaitForExit(1000);
 43                 }
 44                 string errormsg = proc.StandardError.ReadToEnd();
 45                 proc.StandardError.Close();
 46                 if (string.IsNullOrEmpty(errormsg))
 47                 {
 48                     Flag = true;
 49                 }
 50                 else
 51                 {
 52                     throw new Exception(errormsg);
 53                 }
 54             }
 55             catch (Exception ex)
 56             {
 57                 throw ex;
 58             }
 59             finally
 60             {
 61                 proc.Close();
 62                 proc.Dispose();
 63             }
 64             return Flag;
 65         }
 66 
 67 
 68         //read file
 69         public static void ReadFiles(string path)
 70         {
 71             try
 72             {
 73                 // Create an instance of StreamReader to read from a file.
 74                 // The using statement also closes the StreamReader.
 75                 using (StreamReader sr = new StreamReader(path))
 76                 {
 77                     String line;
 78                     // Read and display lines from the file until the end of 
 79                     // the file is reached.
 80                     while ((line = sr.ReadLine()) != null)
 81                     {
 82                         Console.WriteLine(line);
 83                         
 84                     }
 85                 }
 86             }
 87             catch (Exception e)
 88             {
 89                 // Let the user know what went wrong.
 90                 Console.WriteLine("The file could not be read:");
 91                 Console.WriteLine(e.Message);
 92             } 
 93 
 94         }
 95 
 96         //write file
 97         public static void WriteFiles(string path)
 98         {
 99             try
100             {
101                 // Create an instance of StreamWriter to write text to a file.
102                 // The using statement also closes the StreamWriter.
103                 using (StreamWriter sw = new StreamWriter(path))
104                 {
105                     // Add some text to the file.
106                     sw.Write("This is the ");
107                     sw.WriteLine("header for the file.");
108                     sw.WriteLine("-------------------");
109                     // Arbitrary objects can also be written to the file.
110                     sw.Write("The date is: ");
111                     sw.WriteLine(DateTime.Now);
112                 }
113             }
114             catch (Exception e)
115             {
116                 // Let the user know what went wrong.
117                 Console.WriteLine("The file could not be read:");
118                 Console.WriteLine(e.Message);
119             }
120         }
121     }
122 
123     public partial class _Default : System.Web.UI.Page
124     {
125         protected void Page_Load(object sender, EventArgs e)
126         {
127             
128             bool status = false;
129 
130             //连接共享文件夹
131             status = FileShare.connectState(@"\10.80.88.180	est", "admin", "admin");
132             if (status)
133             {
134                 DirectoryInfo theFolder = new DirectoryInfo(@"\10.80.88.180	est");
135 
136                 //先测试读文件,把目录路径与文件名连接
137                 string filename = theFolder.ToString()+"\good.txt";
138                 FileShare.ReadFiles(filename);
139 
140                 //测试写文件,拼出完整的路径
141                 filename = theFolder.ToString() + "\bad.txt";
142                 FileShare.WriteFiles(filename);
143                
144                 //遍历共享文件夹,把共享文件夹下的文件列表列到listbox
145                 foreach (FileInfo nextFile in theFolder.GetFiles())
146                 {
147                     ListBox1.Items.Add(nextFile.Name);
148                 }
149             }
150             else
151             {
152                 ListBox1.Items.Add("未能连接!");
153             }
154         }
155     }}

 程序员的基础教程:菜鸟程序员