1、需求分析web
1.將信息記錄到本地記事本中。服務器
2.將記錄的信息讀取出來。網絡
3.計算出某個文件夾下全部後綴名爲txt的數量和dll的數量。url
4.從網絡上下載文件。spa
2、二話不說上代碼3d
1 using System; 2 using System.IO; 3 using System.Text; 4 5 namespace FileOperation 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 SaveLoginInfo(); 12 ReadLoginInfo(); 13 Ergodic(); 14 } 15 16 17 /// <summary> 18 /// 本地文件中保存登陸信息 19 /// </summary> 20 static void SaveLoginInfo() 21 { 22 #region 在本地保存登陸信息 23 //判斷文件是否存在 24 string loginDicPath = "E:\\Login"; 25 if (!Directory.Exists(loginDicPath)) 26 { 27 Directory.CreateDirectory(loginDicPath); 28 } 29 string loginFilePath = loginDicPath + "\\login.txt"; 30 //能夠指定盤符,也能夠指定任意文件名,還能夠爲word等文件 31 FileStream fs = new FileStream(loginFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); 32 //建立寫入流 33 StreamWriter sw = new StreamWriter(fs); 34 string testContent = "用戶已登陸"; 35 sw.WriteLine(testContent); 36 sw.Close(); 37 #endregion 38 } 39 40 /// <summary> 41 /// 讀取登陸信息 42 /// </summary> 43 static void ReadLoginInfo() 44 { 45 string loginFilePath = "E:\\Login\\login.txt"; 46 if (File.Exists(loginFilePath)) 47 { 48 StreamReader st = new StreamReader(loginFilePath, Encoding.UTF8); 49 string content = st.ReadToEnd(); 50 st.Close(); 51 Console.WriteLine(content); 52 } 53 } 54 55 /// <summary> 56 /// 遍歷文件夾,計算文件夾中txt的數量和dll的數量 57 /// </summary> 58 static void Ergodic() 59 { 60 string loginDicPath = "E:\\testDic"; 61 DirectoryInfo root = new DirectoryInfo(loginDicPath); 62 DirectoryInfo[] dics = root.GetDirectories(); 63 int txtNum = 0; 64 int dllNum = 0; 65 foreach (DirectoryInfo info in dics) 66 { 67 string path = info.FullName; 68 //獲取該文件夾下的全部文件 69 DirectoryInfo root_Children = new DirectoryInfo(path); 70 FileInfo[] filesChildren = root_Children.GetFiles(); 71 //獲取完整路徑名 72 foreach (FileInfo fileInfo in filesChildren) 73 { 74 if (fileInfo.Extension == ".txt") 75 { 76 txtNum += 1; 77 } 78 else if (fileInfo.Extension == ".dll") 79 { 80 dllNum += 1; 81 } 82 else 83 { 84 } 85 } 86 } 87 Console.WriteLine("txt的數量爲{0},dll的數量爲{1}", txtNum, dllNum); 88 } 89 } 90 }
注:對於需求3的目錄結構以下code
需求4的代碼(下載網絡文件到本地)對象
1 using System; 2 using System.Linq; 3 using System.Net; 4 5 namespace FileOperation 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 DownloadWebFile(); 12 } 13 14 15 /// <summary> 16 /// 下載網絡文件 17 /// </summary> 18 static void DownloadWebFile() 19 { 20 //網絡文件地址 21 string webFileUrl = "http://127.0.0.1:8088//error-file.txt"; 22 //下載地址 23 string myDictionary = AppDomain.CurrentDomain.BaseDirectory; 24 //獲取文件名稱 25 string fileName = GetFileName(webFileUrl); 26 if (!HttpFileExist(webFileUrl)) 27 { 28 Console.WriteLine("服務器上沒有該文件"); 29 return; 30 } 31 else 32 { 33 WebClient client = new WebClient(); 34 client.DownloadFile(webFileUrl, myDictionary + "/" + fileName); 35 } 36 37 } 38 39 /// <summary> 40 /// 判斷網絡文件是否存在 41 /// </summary> 42 /// <param name="fileUrl"></param> 43 /// <returns></returns> 44 static bool HttpFileExist(string fileUrl) 45 { 46 try 47 { 48 //建立根據網絡地址的請求對象 49 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(fileUrl)); 50 httpWebRequest.Method = "HEAD"; 51 httpWebRequest.Timeout = 1000; 52 //返回響應狀態是不是成功比較的布爾值 53 using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse()) 54 { 55 return response.StatusCode == HttpStatusCode.OK; 56 } 57 } 58 catch (Exception ex) 59 { 60 return false; 61 } 62 } 63 64 /// <summary> 65 /// 獲取文件名稱 66 /// </summary> 67 /// <param name="url"></param> 68 /// <returns></returns> 69 private static string GetFileName(string url) 70 { 71 string fileName = ""; 72 string[] str = url.Split('/'); 73 fileName = str.Last(); 74 return fileName; 75 } 76 } 77 }
3、寫到後面的話blog
其實這篇都算不上是文件操做彙總,我只是把新學到的知識放到了一個小小的項目中,以便本身後期用到能隨時查詢出來。C#對文件的操做不單單是這些,還有不少操做,就不一一列舉驗證。string
繼續加油~