C#文件打開、讀取、寫入、創建


打開文件

打開文件夾,供操作者選擇文件:

OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();

獲得所選文件的文件名:

string fileName=dialoa.FileName;

獲取所選文件的文件夾目錄:

FileInfo fi = new FileInfo(t);

string path = fileName.Substring(0, t.Length - fi.Name.Length);

將文件移動到別的目錄:

string movePath="......";

fi.move(movePath);

/******************************************************

FileInfo類操作文件還有許多的情況不一一列舉

******************************************************/

讀取文件

StreamReader sr = new StreamReader(filePath);

sr.ReadLine();

 

往文件中寫值

FileStream fStream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fStream);

sw.WriteLine();

sw.Close();

創建文件

string path="......."

 FileStream fStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

 


免責聲明!

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



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