使用C#WebClient類訪問(上傳/下載/刪除/列出文件目錄)由IIS搭建的http文件服務器

前言web

 爲何要寫這邊博文呢?其實,就是使用C#WebClient類訪問由IIS搭建的http文件服務器的問題花了我足足兩天的時間,所以,有必要寫下本身所學到的,同時,也能讓廣大的博友學習學習一下。編程

本文足若有不足之處,請在下方留言提出,我會進行改正的,謝謝!瀏覽器

 

搭建IIS文件服務器服務器

本博文使用的操做系統爲Windows 10 企業版,其餘Windows系統相似,請借鑑:異步

1、固然,開始確定沒有IIS,那該怎麼辦?須要一個軟件環境進行搭建,具體方法以下:異步編程

1)打開「控制面板」,找到「程序與功能」,以下圖所示:工具

2)點進去以後,找到「啓用或關閉Windows功能」,以下圖所示:學習

3)點進去以後,將「Internet Information Services」下全部節點都打勾(這樣就搭建了一個功能徹底的HTTP/FTP服務器),注意「WebDAV發佈」必需要安裝,這個跟文件服務器中文件訪問權限有着很大的關係,若是想對服務器中某個具備讀寫權限的文件夾進行讀寫,就必須開啓該選項,以下圖所示:網站

4)等待安裝完畢,請耐心等待, 以下圖所示:ui

5)完成以後,點擊「關閉」按鈕便可,而後,打開「控制面板」,找到「管理工具」,以下圖所示:

 

6)點擊「管理工具」後,找到「Internet Information Services (IIS)管理器」,打開它,以下圖所示:

 

7)進去以後,就已經進入了IIS的管理界面,咱們只用到的功能爲紅色框內的IIS功能,以下圖所示:

8)第一搭建IIS,會出現一個默認的Web網站,咱們將鼠標移到「Default Web Site」上方,右鍵彈出菜單,在菜單中點擊「刪除」將該網站刪除,以下圖所示:

 

9)添加本身的一個網站,鼠標移到「網站」上方,右鍵點擊鼠標,彈出菜單,在菜單中點擊「添加網站」,以下圖所示:

10)根據以下圖所說的步驟,填寫網站名稱及選擇物理路徑,其餘默認便可,而後點擊「肯定」按鈕:

 

11)本網站僅做爲文件服務器,所以,將服務器的文件瀏覽功能打開,以便瀏覽,具體操做爲鼠標雙擊「目錄瀏覽」後,將「操做」一欄裏的「啓用」打開,以下圖所示:

12)鼠標雙擊「WebDAV創做規則」,以下圖所示:

13)點擊「WebDAV設置」,以下圖所示:

 

 

 14)將①②所示紅色框內的屬性設置爲圖中所示的屬性,並點擊「應用」,以下圖所示:

15)返回到「WebDAV創做規則」,點擊「添加創做規則」,以下圖所示:

16)在彈出的「添加創做規則」,將「容許訪問此內容」選中,權限「讀取、源、寫入」都打勾,點擊「肯定」按鈕關閉,以下圖所示:

 

 17)返回到「WebDAV創做規則」,點擊「啓用WebDAV」,以下圖所示:

 

 18)雙擊「身份驗證」,將「匿名身份驗證」(客戶端讀取文件)及「Windows身份驗證」(客戶端寫入、刪除)啓用,以下所示:

 

19)爲了能讓文件服務器具備寫入、刪除功能,能夠在現有Windows系統帳戶上新建一個隸屬於「Power Users」的帳戶「test」(密碼:123),以下圖所示:

以上關於如何建立帳戶的內容,請自行百度

20)爲了能讓test帳戶順利訪問存放於E盤下的「TestWebSite」文件夾,須要爲該文件夾設置Power Users組的訪問權限,以下圖所示:

關於如何將特定組或用戶設置權限的問題,請自行百度

21)查看本機IIS的IP地址,並在瀏覽器輸入該IP,將會顯示如下內容,以下圖所示:

22)自此,IIS文件服務器的搭建已經完畢。

 

使用C#WebClient訪問IIS文件服務器

 

本博文使用的的IDE爲VS2015,在使用WebClient類以前,必須先引用System.Net命名空間,文件下載、上傳與刪除的都是使用異步編程,也可使用同步編程,

這裏以異步編程爲例:

1)文件下載:

 1         static void Main(string[] args)
 2         {
 3             //定義_webClient對象
 4             WebClient _webClient = new WebClient();
 5             //使用默認的憑據——讀取的時候,只需默認憑據就能夠
 6             _webClient.Credentials = CredentialCache.DefaultCredentials;
 7             //下載的連接地址(文件服務器)
 8             Uri _uri = new Uri(@"http://192.168.1.103/test.doc");
 9             //註冊下載進度事件通知
10             _webClient.DownloadProgressChanged += _webClient_DownloadProgressChanged;
11             //註冊下載完成事件通知
12             _webClient.DownloadFileCompleted += _webClient_DownloadFileCompleted;
13             //異步下載到D盤
14             _webClient.DownloadFileAsync(_uri, @"D:\test.doc");
15             Console.ReadKey();
16         }
17 
18         //下載完成事件處理程序
19         private static void _webClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
20         {
21             Console.WriteLine("Download Completed...");
22         }
23 
24         //下載進度事件處理程序
25         private static void _webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
26         {
27             Console.WriteLine($"{e.ProgressPercentage}:{e.BytesReceived}/{e.TotalBytesToReceive}");   
28         }

運行結果以下:

2)文件上傳:

        static void Main(string[] args)
        {
            //定義_webClient對象
            WebClient _webClient = new WebClient();
            //使用Windows登陸方式
            _webClient.Credentials = new NetworkCredential("test", "123");
            //上傳的連接地址(文件服務器)
            Uri _uri = new Uri(@"http://192.168.1.103/test.doc");
            //註冊上傳進度事件通知
            _webClient.UploadProgressChanged += _webClient_UploadProgressChanged;
            //註冊上傳完成事件通知
            _webClient.UploadFileCompleted += _webClient_UploadFileCompleted;
            //異步從D盤上傳文件到服務器
            _webClient.UploadFileAsync(_uri,"PUT", @"D:\test.doc");
            Console.ReadKey();
        }
        //下載完成事件處理程序
        private static void _webClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            Console.WriteLine("Upload Completed...");
        }

        //下載進度事件處理程序
        private static void _webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            Console.WriteLine($"{e.ProgressPercentage}:{e.BytesSent}/{e.TotalBytesToSend}");
        }

運行結果以下:

3)文件刪除:

        static void Main(string[] args)
        {
            //定義_webClient對象
            WebClient _webClient = new WebClient();
            //使用Windows登陸方式
            _webClient.Credentials = new NetworkCredential("test", "123");
            //待刪除的文件連接地址(文件服務器)
            Uri _uri = new Uri(@"http://192.168.1.103/test.doc");
            //註冊刪除完成時的事件(模擬刪除)
            _webClient.UploadDataCompleted += _webClient_UploadDataCompleted;
            //異步從文件(模擬)刪除文件
            _webClient.UploadDataAsync(_uri, "DELETE", new byte[0]);
            Console.ReadKey();
        }
        //刪除完成事件處理程序
        private static void _webClient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            Console.WriteLine("Deleted...");
        }

運行結果以下:

4)列出文件(或目錄):

 需引入命名空間:System.IO、System.Xml及System.Globalization

        static void Main(string[] args)
        {

            SortedList<string, ServerFileAttributes> _results =GetContents(@"http://192.168.1.103", true);
            //在控制檯輸出文件(或目錄)信息:
            foreach(var _r in _results)
            {
                Console.WriteLine($"{_r.Key}:\r\nName:{_r.Value.Name}\r\nIsFolder:{_r.Value.IsFolder}");
                Console.WriteLine($"Value:{_r.Value.Url}\r\nLastModified:{_r.Value.LastModified}");
                Console.WriteLine();
            }

            Console.ReadKey();
        }

        //定義每一個文件或目錄的屬性
        struct ServerFileAttributes
        {
            public string Name;
            public bool IsFolder;
            public string Url;
            public DateTime LastModified;
        }

        //將文件或目錄列出來
        static SortedList<string, ServerFileAttributes> GetContents(string serverUrl, bool deep)
        {
            HttpWebRequest _httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
            _httpWebRequest.Headers.Add("Translate: f");
            _httpWebRequest.Credentials = CredentialCache.DefaultCredentials;

            string _requestString = @"<?xml version=""1.0"" encoding=""utf-8""?>" +
                  @"<a:propfind xmlns:a=""DAV:"">" +
                  "<a:prop>" +
                  "<a:displayname/>" +
                  "<a:iscollection/>" +
                  "<a:getlastmodified/>" +
                  "</a:prop>" +
                  "</a:propfind>";

            _httpWebRequest.Method = "PROPFIND";
            if (deep == true)
                _httpWebRequest.Headers.Add("Depth: infinity");
            else
                _httpWebRequest.Headers.Add("Depth: 1");
            _httpWebRequest.ContentLength = _requestString.Length;
            _httpWebRequest.ContentType = "text/xml";

            Stream _requestStream = _httpWebRequest.GetRequestStream();
            _requestStream.Write(Encoding.ASCII.GetBytes(_requestString), 0, Encoding.ASCII.GetBytes(_requestString).Length);
            _requestStream.Close();

            HttpWebResponse _httpWebResponse;
            StreamReader _streamReader;
            try
            {
                _httpWebResponse = (HttpWebResponse)_httpWebRequest.GetResponse();
                _streamReader = new StreamReader(_httpWebResponse.GetResponseStream());
            }
            catch (WebException ex)
            {
                throw ex;
            }

            StringBuilder _stringBuilder = new StringBuilder();

            char[] _chars = new char[1024];
            int _bytesRead = 0;

            _bytesRead = _streamReader.Read(_chars, 0, 1024);

            while (_bytesRead > 0)
            {
                _stringBuilder.Append(_chars, 0, _bytesRead);
                _bytesRead = _streamReader.Read(_chars, 0, 1024);
            }
            _streamReader.Close();

            XmlDocument _xmlDocument = new XmlDocument();
            _xmlDocument.LoadXml(_stringBuilder.ToString());

            XmlNamespaceManager _xmlNamespaceManager = new XmlNamespaceManager(_xmlDocument.NameTable);
            _xmlNamespaceManager.AddNamespace("a", "DAV:");

            XmlNodeList _nameList = _xmlDocument.SelectNodes("//a:prop/a:displayname", _xmlNamespaceManager);
            XmlNodeList _isFolderList = _xmlDocument.SelectNodes("//a:prop/a:iscollection", _xmlNamespaceManager);
            XmlNodeList _lastModifyList = _xmlDocument.SelectNodes("//a:prop/a:getlastmodified", _xmlNamespaceManager);
            XmlNodeList _hrefList = _xmlDocument.SelectNodes("//a:href", _xmlNamespaceManager);

            SortedList<string, ServerFileAttributes> _sortedListResult = new SortedList<string, ServerFileAttributes>();
            ServerFileAttributes _serverFileAttributes;

            for (int i = 0; i < _nameList.Count; i++)
            {
                if (_hrefList[i].InnerText.ToLower(new CultureInfo("en-US")).TrimEnd(new char[] { '/' }) != serverUrl.ToLower(new CultureInfo("en-US")).TrimEnd(new char[] { '/' }))
                {
                    _serverFileAttributes = new ServerFileAttributes();
                    _serverFileAttributes.Name = _nameList[i].InnerText;
                    _serverFileAttributes.IsFolder = Convert.ToBoolean(Convert.ToInt32(_isFolderList[i].InnerText));
                    _serverFileAttributes.Url = _hrefList[i].InnerText;
                    _serverFileAttributes.LastModified = Convert.ToDateTime(_lastModifyList[i].InnerText);
                    _sortedListResult.Add(_serverFileAttributes.Url, _serverFileAttributes);
                }
            }
            return _sortedListResult;
        }

 

運行結果以下:

相關文章
相關標籤/搜索