a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#讀寫共享文件夾的方法

2019-10-29 21:01:48
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C#讀寫共享文件夾的具體代碼,供大家參考,具體內容如下

該試驗分以下步驟:

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

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

3、添加后臺代碼如下:其中包含的功能是讀文件,這里以讀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");    //先測試讀文件,把目錄路徑與文件名連接    string filename = theFolder.ToString()+"//good.txt";    FileShare.ReadFiles(filename);    //測試寫文件,拼出完整的路徑    filename = theFolder.ToString() + "//bad.txt";    FileShare.WriteFiles(filename);        //遍歷共享文件夾,把共享文件夾下的文件列表列到listbox    foreach (FileInfo nextFile in theFolder.GetFiles())    {     ListBox1.Items.Add(nextFile.Name);    }   }   else   {    ListBox1.Items.Add("未能連接!");   }  } }} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧洲精品一区二区 | 亚洲国产精品视频 | 亚洲国产成人久久一区二区三区 | 91久久精品国产免费一区 | 欧美视频网址 | 91电影院 | 久久一本 | 国产真实乱全部视频 | 国模一区二区三区 | 91精品国产91久久久久久最新 | 成人av自拍 | 成人水多啪啪片 | 青娱乐在线播放 | 日韩久久一区 | 亚洲精品专区 | 国产精品兄妹在线观看麻豆 | 久久精品久久久久电影 | 久久黄视频 | 羞羞在线观看视频免费观看hd | av资源首页 | 国产精品视频黄色 | 久久艹国产视频 | 亚洲免费视频网站 | 欧美日一区二区 | 久久激情综合 | 婷婷丁香五| 激情网站免费观看 | 91欧美在线 | 天天操操 | 欧美国产日韩在线观看 | 国产精品视频二区不卡 | 天堂在线视频 | 精品视频久久久 | 在线播放91 | 欧美精品久久久 | 五月婷婷色 | 中文字幕第15页 | 中文字幕日韩在线 | 欧美日本国产 | 欧美激情一区二区三区 | gogo熟少妇大胆尺寸 |