最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年05月21日 14:20:04   作者:wybshyy  
這篇文章主要為大家詳細(xì)介紹了C#讀寫共享文件夾的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

該試驗(yàn)分以下步驟:

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

2、新建一個(gè)webapplication項(xiàng)目,在前臺(tái)頁面加一個(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("未能連接!");
   }
  }
 }
} 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

  • C# 如何生成 DataMatrix 格式的二維碼

    C# 如何生成 DataMatrix 格式的二維碼

    該文主要是利用OnBarcode.dll 生成DataMatrix 格式的二維碼的一些簡單方法和操作技巧,對(duì)C# 如何生成 DataMatrix 格式的二維碼相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-11-11
  • C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法

    C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法

    這篇文章主要介紹了C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法,可直接將string類型結(jié)果保存為附件,實(shí)例中備有相應(yīng)的注釋便于理解,需要的朋友可以參考下
    2014-11-11
  • 如何解決hash沖突

    如何解決hash沖突

    上篇文章 為什么哈希存取比較快?使用它需要付出什么代價(jià) 只是簡單介紹了使用hash所帶來的利與弊。并未涉及hash的技術(shù)細(xì)節(jié),本文則著重學(xué)習(xí)一下如何解決哈希編址的沖突問題。
    2016-06-06
  • C# WPF Image控件的綁定方法

    C# WPF Image控件的綁定方法

    這篇文章主要介紹了C# WPF Image控件的綁定方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C# string轉(zhuǎn)unicode字符的實(shí)現(xiàn)

    C# string轉(zhuǎn)unicode字符的實(shí)現(xiàn)

    本文主要介紹了C# string轉(zhuǎn)unicode字符的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • C#畫筆Pen畫虛線的方法

    C#畫筆Pen畫虛線的方法

    這篇文章主要介紹了C#畫筆Pen畫虛線的方法,涉及C#畫筆Pen屬性的相關(guān)設(shè)置技巧,需要的朋友可以參考下
    2015-06-06
  • 用C#寫的ADSL撥號(hào)程序的代碼示例

    用C#寫的ADSL撥號(hào)程序的代碼示例

    用C#寫的ADSL撥號(hào)程序的代碼示例...
    2007-11-11
  • C#實(shí)現(xiàn)把指定數(shù)據(jù)寫入串口

    C#實(shí)現(xiàn)把指定數(shù)據(jù)寫入串口

    這篇文章主要介紹了C#實(shí)現(xiàn)把指定數(shù)據(jù)寫入串口,直接給出示例代碼,需要的朋友可以參考下
    2015-06-06
  • C#判斷單詞個(gè)數(shù)方法總結(jié)

    C#判斷單詞個(gè)數(shù)方法總結(jié)

    我們給大家總計(jì)了C#中判斷英文單詞個(gè)數(shù)的方法以及排序的技巧,對(duì)此有需要的朋友可以測(cè)試下。
    2018-03-03
  • 最新評(píng)論

    洮南市| 泉州市| 武定县| 志丹县| 滨海县| 东乌珠穆沁旗| 和平区| 伽师县| 托里县| 台南市| 蓬溪县| 阿坝县| 福清市| 周至县| 定边县| 江安县| 天等县| 深州市| 滨海县| 光山县| 茶陵县| 马山县| 深水埗区| 六盘水市| 江城| 长乐市| 犍为县| 黄梅县| 荔波县| 武乡县| 延庆县| 达孜县| 准格尔旗| 平顺县| 沙雅县| 蓝田县| 湛江市| 崇义县| 手游| 大理市| 黑山县|