C# 下載文件 刪除文件 寫(xiě)入文本的實(shí)例
由于經(jīng)常用到文件處理,便自己封裝了下 分享給大家。 包含寫(xiě)入文本 批量刪除文件 下載文件 。--可直接使用
/// <summary> /// 寫(xiě)入到txt /// </summary> /// <param name="savePath"></param> /// <param name="content"></param> public static void WriteInTxt(string savePath, string content) { string tempPath = System.IO.Path.GetDirectoryName(savePath); System.IO.Directory.CreateDirectory(tempPath); //創(chuàng)建臨時(shí)文件目錄 if (!System.IO.File.Exists(savePath)) { FileStream fs1 = new FileStream(savePath, FileMode.Create, FileAccess.Write);//創(chuàng)建寫(xiě)入文件 StreamWriter sw = new StreamWriter(fs1); sw.WriteLine(content);//開(kāi)始寫(xiě)入值 sw.Close(); fs1.Close(); } else { FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Write); StreamWriter sr = new StreamWriter(fs); sr.WriteLine(content);//開(kāi)始寫(xiě)入值 sr.Close(); fs.Close(); } } /// <summary> /// 遞歸刪除文件夾下所有文件 /// </summary> /// <param name="file"></param> public static void DeleteFile(string dirPath) { try { //去除文件夾和子文件的只讀屬性 //去除文件夾的只讀屬性 System.IO.DirectoryInfo fileInfo = new DirectoryInfo(dirPath); fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory; //去除文件的只讀屬性 System.IO.File.SetAttributes(dirPath, System.IO.FileAttributes.Normal); //判斷文件夾是否還存在 if (Directory.Exists(dirPath)) { foreach (string f in Directory.GetFileSystemEntries(dirPath)) { if (File.Exists(f)) { //如果有子文件刪除文件 File.Delete(f); } else { //循環(huán)遞歸刪除子文件夾 DeleteFile(f); } } //刪除空文件夾 Directory.Delete(dirPath); } } catch (Exception e) { } } /// <summary> /// Http下載文件 /// </summary> /// <param name="url">下載文件路徑</param> /// <param name="savePath">保存路徑</param> /// <returns></returns> public static bool HttpDownloadFile(string url, string savePath) { string tempPath = System.IO.Path.GetDirectoryName(savePath); System.IO.Directory.CreateDirectory(tempPath); //創(chuàng)建臨時(shí)文件目錄 string tempFile = tempPath + @"\" + System.IO.Path.GetFileName(savePath); //臨時(shí)文件 if (System.IO.File.Exists(tempFile)) { //存在則跳出 return true; //System.IO.File.Delete(tempFile); } try { FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); // 設(shè)置參數(shù) HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù) HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求 Stream responseStream = response.GetResponseStream(); //創(chuàng)建本地文件寫(xiě)入流 //Stream stream = new FileStream(tempFile, FileMode.Create); byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr, 0, (int)bArr.Length); while (size > 0) { //stream.Write(bArr, 0, size); fs.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, (int)bArr.Length); } //stream.Close(); fs.Close(); responseStream.Close(); System.IO.File.Move(tempFile, savePath); return true; } catch (Exception ex) { return false; } }
以上這篇C# 下載文件 刪除文件 寫(xiě)入文本的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。
上一篇:C#實(shí)現(xiàn)的二維數(shù)組排序算法示例
欄 目:C#教程
本文標(biāo)題:C# 下載文件 刪除文件 寫(xiě)入文本的實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5321.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)多線程寫(xiě)入同一個(gè)文件的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無(wú)法刪除文件)
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#路徑,文件,目錄及IO常見(jiàn)操作匯總
- 01-10winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法
- 01-10C#讀寫(xiě)INI文件的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改