C# 客戶端讀取共享目錄文件


  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Diagnostics;
  7  
  8 namespace test3
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             bool status = false;
 15  
 16             //連接共享目錄
 17             status = connectState(@"\\10.0.xx.xxx\ctec2", "waveaccess", "waveaccessxxx");
 18             if (status)
 19             {
 20                 //共享文件夾的目錄
 21                 DirectoryInfo theFolder = new DirectoryInfo(@"\\10.0.xx.xxx\ctec2\"); 
 22                 string filename = theFolder.ToString();
 23                 //執行方法
 24                 TransportLocalToRemote(@"D:\readme1.txt", filename, "readme1.txt");  //實現將本地文件寫入到遠程服務器
 25                 TransportRemoteToLocal(@"D:\readme.txt", filename, "readme.txt");    //實現將遠程服務器文件寫入到本地
 26             }
 27             else
 28             {
 29                 Console.WriteLine("未能連接!");
 30             }
 31             Console.WriteLine("成功");
 32             Console.ReadKey();
 33         }
 34  
 35         public static bool connectState(string path)
 36         {
 37             return connectState(path, "", "");
 38         }
 39         /// <summary>
 40         /// 連接遠程共享文件夾
 41         /// </summary>
 42         /// <param name="path">遠程共享文件夾的路徑</param>
 43         /// <param name="userName">用戶名</param>
 44         /// <param name="passWord">密碼</param>
 45         /// <returns></returns>
 46         public static bool connectState(string path, string userName, string passWord)
 47         {
 48             bool Flag = false;
 49             Process proc = new Process();
 50             try
 51             {
 52                 proc.StartInfo.FileName = "cmd.exe";
 53                 proc.StartInfo.UseShellExecute = false;
 54                 proc.StartInfo.RedirectStandardInput = true;
 55                 proc.StartInfo.RedirectStandardOutput = true;
 56                 proc.StartInfo.RedirectStandardError = true;
 57                 proc.StartInfo.CreateNoWindow = true;
 58                 proc.Start();
 59                 string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
 60                 proc.StandardInput.WriteLine(dosLine);
 61                 proc.StandardInput.WriteLine("exit");
 62                 while (!proc.HasExited)
 63                 {
 64                     proc.WaitForExit(1000);
 65                 }
 66                 string errormsg = proc.StandardError.ReadToEnd();
 67                 proc.StandardError.Close();
 68                 if (string.IsNullOrEmpty(errormsg))
 69                 {
 70                     Flag = true;
 71                 }
 72                 else
 73                 {
 74                     throw new Exception(errormsg);
 75                 }
 76             }
 77             catch (Exception ex)
 78             {
 79                 throw ex;
 80             }
 81             finally
 82             {
 83                 proc.Close();
 84                 proc.Dispose();
 85             }
 86             return Flag;
 87         }
 88  
 89         /// <summary>
 90         /// 從遠程服務器下載文件到本地
 91         /// </summary>
 92         /// <param name="src">下載到本地后的文件路徑,包含文件的擴展名</param>
 93         /// <param name="dst">遠程服務器路徑(共享文件夾路徑)</param>
 94         /// <param name="fileName">遠程服務器(共享文件夾)中的文件名稱,包含擴展名</param>
 95         public static void TransportRemoteToLocal(string src, string dst, string fileName)  //src:下載到本地后的文件路徑     dst:遠程服務器路徑    fileName:遠程服務器dst路徑下的文件名
 96         {
 97             if (!Directory.Exists(dst))
 98             {
 99                 Directory.CreateDirectory(dst);
100             }
101             dst = dst + fileName;
102             FileStream inFileStream = new FileStream(dst, FileMode.Open);    //遠程服務器文件  此處假定遠程服務器共享文件夾下確實包含本文件,否則程序報錯
103  
104             FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate);   //從遠程服務器下載到本地的文件
105  
106             byte[] buf = new byte[inFileStream.Length];
107  
108             int byteCount;
109  
110             while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
111             {
112  
113                 outFileStream.Write(buf, 0, byteCount);
114  
115             }
116  
117             inFileStream.Flush();
118  
119             inFileStream.Close();
120  
121             outFileStream.Flush();
122  
123             outFileStream.Close();
124             
125         }
126  
127         /// <summary>
128         /// 將本地文件上傳到遠程服務器共享目錄
129         /// </summary>
130         /// <param name="src">本地文件的絕對路徑,包含擴展名</param>
131         /// <param name="dst">遠程服務器共享文件路徑,不包含文件擴展名</param>
132         /// <param name="fileName">上傳到遠程服務器后的文件擴展名</param>
133         public static void TransportLocalToRemote(string src, string dst, string fileName)    //src
134         {
135             FileStream inFileStream = new FileStream(src, FileMode.Open);    //此處假定本地文件存在,不然程序會報錯   
136  
137             if (!Directory.Exists(dst))        //判斷上傳到的遠程服務器路徑是否存在
138             {
139                 Directory.CreateDirectory(dst);
140             }
141             dst = dst + fileName;             //上傳到遠程服務器共享文件夾后文件的絕對路徑
142  
143             FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
144  
145             byte[] buf = new byte[inFileStream.Length];
146  
147             int byteCount;
148  
149             while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
150             {
151  
152                 outFileStream.Write(buf, 0, byteCount);
153  
154             }
155  
156             inFileStream.Flush();
157  
158             inFileStream.Close();
159  
160             outFileStream.Flush();
161  
162             outFileStream.Close();
163         }
164     }
165 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM