C#讀寫共享文件夾的方法
本文實例為大家分享了C#讀寫共享文件夾的具體代碼,供大家參考,具體內(nèi)容如下
該試驗分以下步驟:
1、在服務(wù)器設(shè)置一個共享文件夾,在這里我的服務(wù)器ip地址是10.80.88.180,共享文件夾名字是test,test里面有兩個文件:good.txt和bad.txt,訪問權(quán)限,用戶名是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("未能連接!");
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#使用TimeSpan時間計算的簡單實現(xiàn)
本文標(biāo)題:C#讀寫共享文件夾的方法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5175.html
您可能感興趣的文章
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
- 01-10C#讀寫INI文件的方法
- 01-10C#遍歷文件夾后上傳文件夾中所有文件錯誤案例分析
- 01-10c#讀寫App.config,ConfigurationManager.AppSettings 不生效的解決方法
- 01-10C#編程實現(xiàn)獲取文件夾中所有文件的文件名
- 01-10C#警惕匿名方法造成的變量共享實例分析
- 01-10輕松學(xué)習(xí)C#的讀寫操作
- 01-10C#程序中創(chuàng)建、復(fù)制、移動、刪除文件或文件夾的示例
- 01-10舉例說明Java多線程編程中讀寫鎖的使用
- 01-10C#讀寫指定編碼格式的文本文件


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子


