C#讀寫共享文件夾

該試驗分如下步驟:html

一、在服務器設置一個共享文件夾,在這裏個人服務器ip地址是10.80.88.180,共享文件夾名字是test,test裏面有兩個文件:good.txt和bad.txt,訪問權限,用戶名是admin,密碼是admin。web

二、新建一個webapplication項目,在前臺頁面加一個listbox,ID是ListBox1.服務器

三、添加後臺代碼以下:其中包含的功能是讀文件,這裏以讀good 文件爲例;寫文件,這裏以寫bad文件爲例;還有是將test文件夾下的文件名列到listbox中。 app

  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\test", "admin", "admin");
132             if (status)
133             {
134                 DirectoryInfo theFolder = new DirectoryInfo(@"\\10.80.88.180\test");
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     }}

轉自:http://www.cnblogs.com/ManMonth/archive/2011/10/11/2206998.htmlwebapp

相關文章
相關標籤/搜索