C# 文件操作筆記

文件夾

1.存在:

?if(Directory.Exists(dirPath)

{

}

2.獲取文件夾內文件信息:

? ? ? ? ? ? ? ? DirectoryInfo di = new DirectoryInfo(dirPath);
? ? ? ? ? ? ? ? foreach (FileInfo fi in di.GetFiles())
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? len1 = fi.Length;
fi.FullName;

......
? ? ? ? ? ? ? ? }


3.獲取文件夾內,文件夾信息

?DirectoryInfo[] dis = di.GetDirectories();


4.創建文件夾

?Directory.CreateDirectory(desdir);


文件

1.存在:

?if(File.Exists(filePath)

{

}


====================================================================================================================
?1. DirectoryInfo.GetFiles():獲取目錄中(不包含子目錄)的文件,返回類型為FileInfo[],支持通配符查找;
??? 2. DirectoryInfo.GetDirectories():獲取目錄(不包含子目錄)的子目錄,返回類型為DirectoryInfo[],支持通配符查找;
??? 3. DirectoryInfo. GetFileSystemInfos():獲取指定目錄下(不包含子目錄)的文件和子目錄,返回類型為FileSystemInfo[],支持通配符查找;
如何獲取指定文件的基本信息;
??? FileInfo.Exists:獲取指定文件是否存在;
??? FileInfo.Name,FileInfo.Extensioin:獲取文件的名稱和擴展名;
??? FileInfo.FullName:獲取文件的全限定名稱(完整路徑);
??? FileInfo.Directory:獲取文件所在目錄,返回類型為DirectoryInfo;
??? FileInfo.DirectoryName:獲取文件所在目錄的路徑(完整路徑);
??? FileInfo.Length:獲取文件的大小(字節數);
??? FileInfo.IsReadOnly:獲取文件是否只讀;
??? FileInfo.Attributes:獲取或設置指定文件的屬性,返回類型為FileAttributes枚舉,可以是多個值的組合
??? FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分別用于獲取文件的創建時間、訪問時間、修改時間;


文件名字符串操作

1.獲取文件名

nameList[i].Substring(nameList[i].LastIndexOf("\\")+1);


2.獲取文件路徑

nameList[i].Substring(0,nameList[i].LastIndexOf("\\"));



保存到txt

? ? ??private void SaveTextBox(string filename)
? ? ? ? {
? ? ? ? ? ? if(!Directory.Exists(@".\save"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Directory.CreateDirectory(@".\save");
? ? ? ? ? ? }? ? ? ? ??
? ? ? ? ? ? string strfile = @".\save\" + filename+".txt";

? ? ? ? ? ?// FileStream fileStream = new FileStream(strfile, FileMode.OpenOrCreate);? ? //新建文件流

? ? FileStream fileStream = new FileStream(strfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); ?//讀寫打開,不會阻塞;
? ? ? ? ? ? StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.Default); ? //寫流

? ? ? ? ? ? string textfile = textBox1.Text;
? ? ? ? ? ? string[] textline = textfile.Split( '\n'); ? //分行
? ? ? ? ? ? for (int i = 0; i < textline.Length; i++) ? ?//分行寫入
? ? ? ? ? ? {
? ? ? ? ? ? ? ? streamWriter.Write(textline[i] + "\r\n");
? ? ? ? ? ? }
? ? ? ? ? ? streamWriter.Flush();
? ? ? ? ? ? streamWriter.Close();
? ? ? ? ? ? fileStream.Close();
? ? ? ? ? ?
? ? ? ? }




復制文件到某個目錄
? ? ? ? private bool CopyFile(string filepath, string desdir)
? ? ? ? {
? ? ? ? ? ? string srcfileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);
? ? ? ? ? ? srcfileName = desdir + "\\" + srcfileName; ? ?//目標文件
? ? ? ? ? ? if (!Directory.Exists(desdir)) ? ? ? ? ? //判定目標目錄存在
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Directory.CreateDirectory(desdir);
? ? ? ? ? ? }


? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? File.Copy(filepath, srcfileName, true); //over write
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //AddTextBox("copy file error:" + ex.Message);
? ? ? ? ? ? ? ? return false;


? ? ? ? ? ? }
? ? ? ? }




文件名字符串操作

1.獲取文件名

nameList[i].Substring(nameList[i].LastIndexOf("\\")+1);


2.獲取文件路徑

nameList[i].Substring(0,nameList[i].LastIndexOf("\\"));



參考鏈接:

http://blog.csdn.net/donthan/article/details/17310975


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281063.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281063.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281063.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

.NET跨平臺框架選擇之一 - Avalonia UI

本文閱讀目錄1. Avalonia UI簡介Avalonia UI文檔教程&#xff1a;https://docs.avaloniaui.net/docs/getting-started隨著跨平臺越來越流行&#xff0c;.NET支持跨平臺至今也有十幾年的光景了(Mono[1]開始)。但是目前基于.NET[2]的跨平臺&#xff0c;大多數還是在使用B/S架構的…

網絡串流_串流NBA籃球的最便宜方式(無需電纜)

網絡串流I love NBA basketball. Every year, I get really excited around the beginning of September because I know tip-off is approaching. This year, I also had to figure out how I’m going to watch the Bulls (lose almost every game) with a combination of st…

tornado 第一篇

一&#xff1a;異步和非阻塞IO 實時的web特性通常需要每個用戶一個大部分時間&#xff0c;在傳統的同步web服務器中&#xff0c;這意味著需要給每個用戶分配一個專用的線程&#xff0c;這樣的開銷是十分巨大 tornado使用啦一種單線程事件循環的方式&#xff0c;這意味著所有的應…

最近找工作面的面試題目匯總(一)

網址&#xff1a;http://www.cnblogs.com/renyiqiu/p/6504839.html 目錄 1.抽象類的介紹&#xff0c;抽象類里的虛函數和抽象函數 參考文檔抽象類特征抽象方法特征2.虛函數和抽象方法 參考文檔虛方法的特點虛方法(virtual)和抽象方法(abstract)的區別3.靜態類和靜態類成員 參考…

你認識的C# foreach語法糖,真的是全部嗎?

本文的知識點其實由golang知名的for循環陷阱發散而來&#xff0c; 對應到我的主力語言C#&#xff0c; 其實牽涉到閉包、foreach。為了便于理解&#xff0c;我重新組織了語言&#xff0c;以倒敘結構行文。先給大家提煉出一個C#題&#xff1a;觀察for、foreach閉包的差異左邊輸出…

C#對window 硬件類操作,ManagementObjectSearcher

原文轉載&#xff1a;http://blog.csdn.net/da_keng/article/details/50589145 純屬轉載&#xff0c;復制過來方便編程時尋找。感謝作者&#xff1a;I-Awakening復制前補充&#xff1a; 在剛學C#&#xff0c;用ManagementObjectSearcher 竟然不能解析到頭文件&#xff0c;需要手…

2018第51周日

從人們開始用電腦開始就面臨著文件版本控制的問題&#xff0c;從最原始的同一個文檔多個不同命名表示版本到使用本地的文件版本管理&#xff0c;到后面集中式版本管理如2000年的SVN&#xff0c;到再后來的分布式的版本控制系統&#xff0c;如2005年的Git。到現在用的最多的版本…

twitter批量取消關注_如何在Twitter上取消阻止“潛在敏感內容”

twitter批量取消關注Twitter推特Twitter blocks some tweets with a “potentially sensitive content” warning. You can disable this warning—even on an iPhone or iPad, where the option isn’t normally available. You can also disable sensitive content warnings …

mysql數值類型總結及常用函數

最近在學習下&#xff0c;總結一下mysql數值類型&#xff1b; mysql字符類型分&#xff1a; 1、整數類型&#xff1a; 字節 值范圍 INTERGER 1 -127-128 SMALLINT 2 MEDIUMINT…

Semantic-UI的React實現(二):CSS類構造模塊

更簡單的類名標簽 Semantic-UI使用了更簡單的類名聲明。用過Bootstrap的同學都會被其復雜的類名標簽折磨過&#xff0c;例如一個簡單的按鍵樣式&#xff0c;不論顏色或是大小&#xff0c;都需要btn-前綴聲明&#xff1a; <button type"button" class"btn btn…

skype自動回復_如何在Windows 10上阻止Skype自動啟動

skype自動回復Microsoft微軟The Skype app included with Windows 10 now has a notification area icon. That’s great, but what if you never use Skype and don’t want it starting every time you sign in? Here’s how to get rid of it. Windows 10隨附的Skype應用程…

Vue 組件實例屬性的使用

前言 因為最近面試了二、三十個人&#xff0c;發現大部分都還是只是停留在 Vue 文檔的教程。有部分連教程這部分的文檔也沒看全。所以稍微寫一點&#xff0c;讓新上手的 Vuer 多了解 Vue 文檔的其他更需要關注的點。 因為 Vue 文檔已經是個很成熟的文檔&#xff0c;并且實現的 …

C# 讀取硬盤信息類

在編寫工具檢查硬盤信息時&#xff0c;總結常用到的類&#xff1a; Win32_DiskDrive 這個用了檢查整個硬盤的信息&#xff0c;如果電腦只有一個硬盤&#xff0c;那只顯示一條信息。參考如下代碼&#xff0c;AddTextBox為自定義顯示函數。&#xff08;MSDN class 查詢&#xff1…

95后滬漂女孩深陷“狠”且“卷”職場,向上思維,永不過時!

hi&#xff0c;這里是桑小榆。最近和一個伙伴oncall了很久&#xff0c;對我的文章以及思想轉變產生了很大的共鳴&#xff0c;她向我分享了一些職場經歷還有成長經歷等&#xff0c;她的這些經歷也讓我引發了一定的思考。光光&#xff0c;最近剛升任了部門主管&#xff0c;對于當…

PHP:6種GET和POST請求發送方法

在i94web博客中&#xff0c;我試過了暢言和多說兩種社會化評論框&#xff0c;后來還是拋棄了暢言&#xff0c;不安全。 無論是暢言還是多說&#xff0c;我都需要從遠程抓取文章的評論數&#xff0c;然后存入本地數據庫。對于多說&#xff0c;請求的格式如下&#xff1a; // 獲取…

解決Ubuntu 16.04下提示boot分區空間不足的辦法

原文地址: http://www.jb51.net/article/106976.htm   https://www.linuxidc.com/Linux/2015-09/123227.htm 因為linux內核一直在更新&#xff0c;更新后&#xff0c;舊的內核就不在使用&#xff0c;但舊的內核文件還在boot里面&#xff0c;占據著空間&#xff0c;更新幾次過…

3d鏡頭 適配_您是否應該將鏡頭適配器與無反光鏡相機一起使用?

3d鏡頭 適配Canon佳能Mirrorless cameras aren’t the future, they’re the present. If you’re switching from an older DSLR, though, the obvious thing to do is just buy an adapter so you can keep using your old gear. 無反光鏡相機不是未來&#xff0c;而是現在。…

C#彈窗提示并自動關閉方法

剛學C#不久&#xff0c;就寫個工具&#xff0c;總結寫一個簡便自定義提示窗口方法&#xff0c;并自動關閉。 1.在項目添加windows form&#xff08;非user control&#xff09;&#xff0c;命名為Form_wait。 2.在Form_wait,加入需要控件與一個定時器timer1。 數字10為計時顯…

dotNET 7:最小 API 使用

最小 API 并不是在 .NET 7 中才加入的&#xff0c;記得應該是在 .NET 6 中就已經提供&#xff0c;只是對我來說&#xff0c;到現在才開始使用。創建一個最小 API在 VS 2022 中創建 WebAPI 項目&#xff0c;不勾選使用控制器&#xff0c;創建出來的就是最小 API &#xff1a;不勾…

Taro小程序采坑記

Taro&#xff0c;京東凹凸實驗室出品的適配多端的一個框架&#xff0c; Taro 是一套遵循 React 語法規范的 多端開發 解決方案。現如今市面上端的形態多種多樣&#xff0c;Web、React-Native、微信小程序等各種端大行其道&#xff0c;當業務要求同時在不同的端都要求有所表現的…