怎么在C#中讀寫共享文件夾?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比富裕網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式富裕網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋富裕地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
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 文件為例;寫文件,這里以寫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è)試寫文件,拼出完整的路徑 filename = theFolder.ToString() + "\\bad.txt"; FileShare.WriteFiles(filename); //遍歷共享文件夾,把共享文件夾下的文件列表列到listbox foreach (FileInfo nextFile in theFolder.GetFiles()) { ListBox1.Items.Add(nextFile.Name); } } else { ListBox1.Items.Add("未能連接!"); } } } }
C#是一個(gè)簡(jiǎn)單、通用、面向?qū)ο蟮木幊陶Z(yǔ)言,它由微軟Microsoft開發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡(jiǎn)單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語(yǔ)法風(fēng)格、創(chuàng)新的語(yǔ)言特性和便捷的面向組件編程從而成為.NET開發(fā)的首選語(yǔ)言,但它不適用于編寫時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。
關(guān)于怎么在C#中讀寫共享文件夾問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)頁(yè)名稱:怎么在C#中讀寫共享文件夾
本文鏈接:http://jinyejixie.com/article32/peoosc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、ChatGPT、手機(jī)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、搜索引擎優(yōu)化、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)