C#讀寫(xiě)共享文件夾的方法
本文實(shí)例為大家分享了C#讀寫(xiě)共享文件夾的具體代碼,供大家參考,具體內(nèi)容如下
該試驗(yàn)分以下步驟:
1、在服務(wù)器設(shè)置一個(gè)共享文件夾,在這里我的服務(wù)器ip地址是10.80.88.180,共享文件夾名字是test,test里面有兩個(gè)文件:good.txt和bad.txt,訪問(wèn)權(quán)限,用戶名是admin,密碼是admin。
2、新建一個(gè)webapplication項(xiàng)目,在前臺(tái)頁(yè)面加一個(gè)listbox,ID是ListBox1.
3、添加后臺(tái)代碼如下:其中包含的功能是讀文件,這里以讀good 文件為例;寫(xiě)文件,這里以寫(xiě)bad文件為例;還有是將test文件夾下的文件名列到listbox中。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.Diagnostics; using System.IO; 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 { 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 + " /User:" + userName + " " + passWord + " /PERSISTENT:YES"; proc.StandardInput.WriteLine(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; } //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 = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } //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. sw.Write("This is the "); sw.WriteLine("header for the file."); sw.WriteLine("-------------------"); // Arbitrary objects can also be written to the file. sw.Write("The date is: "); sw.WriteLine(DateTime.Now); } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } } public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { bool status = false; //連接共享文件夾 status = FileShare.connectState(@"\\10.80.88.180\test", "admin", "admin"); if (status) { DirectoryInfo theFolder = new DirectoryInfo(@"\\10.80.88.180\test"); //先測(cè)試讀文件,把目錄路徑與文件名連接 string filename = theFolder.ToString()+"\\good.txt"; FileShare.ReadFiles(filename); //測(cè)試寫(xiě)文件,拼出完整的路徑 filename = theFolder.ToString() + "\\bad.txt"; FileShare.WriteFiles(filename); //遍歷共享文件夾,把共享文件夾下的文件列表列到listbox foreach (FileInfo nextFile in theFolder.GetFiles()) { ListBox1.Items.Add(nextFile.Name); } } else { ListBox1.Items.Add("未能連接!"); } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#基礎(chǔ):Equals()與運(yùn)算符==的區(qū)別分析
本篇文章是對(duì)c#中的Equals()與運(yùn)算符==的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#調(diào)用windows api關(guān)機(jī)(關(guān)機(jī)api)示例代碼分享
本文主要介紹了C#調(diào)用windows api關(guān)機(jī)的示例代碼,大家參考使用吧2014-01-01C# winfrom實(shí)現(xiàn)讀取修改xml
這篇文章主要為大家詳細(xì)介紹了C# winfrom實(shí)現(xiàn)讀取修改xml的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05