一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C# - C#讀寫共享文件夾的方法

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

2022-02-24 14:00wybshyy C#

這篇文章主要為大家詳細(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)頁(yè)面加一個(gè)listbox,ID是ListBox1.

3、添加后臺(tái)代碼如下:其中包含的功能是讀文件,這里以讀good 文件為例;寫文件,這里以寫bad文件為例;還有是將test文件夾下的文件名列到listbox中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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("未能連接!");
   }
  }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/wybshyy/article/details/52064234

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品自在欧美一区 | 亚洲精品乱码久久久久久蜜桃 | 亚洲社区在线观看 | 国产乱妇无码大片在线观看 | 国语自产拍在线观看7m | 91免费视频国产 | 久久精品麻豆国产天美传媒果冻 | 无人区在线观看免费观看 | 男男双性生子产乳高辣h | 猫影视tv接口| 97se亚洲国产综合自在线观看 | 80日本xxxxxxxxx96| 国内久久 | 火影忍者小南裸羞羞漫画 | 99免费在线视频 | 3d美女触手怪爆羞羞漫画 | 亚洲伦理天堂 | 北条麻妃黑人正在播放 | 波多野结衣52部合集在线观看 | 午夜影视免费 | 色爱导航| 好看华人华人经典play | 日本www视频在线观看 | 日本一区二区三区精品 | 午夜DY888国产精品影院 | 99久久精品国产一区二区 | 久久精品午夜一区二区福利 | 香蕉免费高清完整 | 手机看片国产免费久久网 | 日本sss在线高清观看 | 国产爱啪啪 | 日本www视频在线观看 | 国产男女乱淫真视频全程播放 | 超爽人人做人人爽 | 日本捏胸吃奶视频免费 | 天天快乐在线观看 | 国产精品中文 | 成年视频在线播放 | 色琪琪久久草在线视频 | 999国产高清在线精品 | 楚乔传第二部免费播放电视连续剧 |