您好,登錄后才能下訂單哦!
這篇“C#讀取寫入文件的方式有哪些”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C#讀取寫入文件的方式有哪些”文章吧。
/// <summary> /// 二進制寫入文件 /// </summary> private void button1_Click(object sender, EventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.FileName = "File"; saveDlg.Title = "保存數據"; saveDlg.Filter = "File(*.txt)|*.txt"; saveDlg.DefaultExt = "txt"; saveDlg.AddExtension = true; //saveDlg.InitialDirectory = "D:AMC Results"; if (saveDlg.ShowDialog() == DialogResult.OK) { Cursor = Cursors.WaitCursor; SaveDatafileWithNewVersion(saveDlg.FileName); Cursor = Cursors.Default; } } /// <summary> /// 二進制保存文件 /// </summary> public static void SaveDatafileWithNewVersion(String sFile) { //文件格式依次為:內部版本號,... Stream stream = File.Open(sFile, FileMode.Create); BinaryWriter bw = new BinaryWriter(stream); bw.Write((Int16)1);//內部版本號,暫時用1,表示版本1,整型 ///寫入Config對象 bw.Write("澤國江山入戰圖 "); bw.Write("生民何計樂樵蘇 "); bw.Write("憑君莫話封侯事 "); bw.Write("一將功成萬骨枯 "); bw.Close(); stream.Close(); } /// <summary> /// 打開文件 /// </summary> private void button2_Click(object sender, EventArgs e) { OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Title = "File"; openDlg.Filter = "File (*.txt)|*.txt"; if (openDlg.ShowDialog() == DialogResult.OK) { BinaryReader br = new BinaryReader(new FileStream(openDlg.FileName, FileMode.Open)); string str = ""; int number = br.ReadInt16(); str += br.ReadString(); str += br.ReadString(); str += br.ReadString(); str += br.ReadString(); richTextBox1.Text = str; br.Close(); } }
/// <summary> /// 泛型寫入 /// </summary> private void button3_Click(object sender, EventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.FileName = "File"; saveDlg.Title = "保存數據"; saveDlg.Filter = "File(*.txt)|*.txt"; saveDlg.DefaultExt = "txt"; saveDlg.AddExtension = true; //saveDlg.InitialDirectory = "D:AMC Results"; if (saveDlg.ShowDialog() == DialogResult.OK) { string str = "澤國江山入戰圖 生民何計樂樵蘇 憑君莫話封侯事 一將功成萬骨枯 "; BinarySerialize<string>(str, saveDlg.FileName); } } /// <summary> /// 寫入文件 /// </summary> /// <typeparam name="T">類型</typeparam> /// <param name="obj">寫入類型</param> /// <param name="path">路徑</param> public static void BinarySerialize<T>(T obj, string path) { FileStream fileStream = new FileStream(path, FileMode.Create); BinaryFormatter b = new BinaryFormatter(); try { b.Serialize(fileStream, obj); fileStream.Close(); } catch (System.Exception ex) { Console.WriteLine(ex.Message); } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } } } /// <summary> /// 泛型讀取 /// </summary> private void button4_Click(object sender, EventArgs e) { OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Title = "File"; openDlg.Filter = "File (*.txt)|*.txt"; if (openDlg.ShowDialog() == DialogResult.OK) { string str = BinaryDeserialize<string>(openDlg.FileName); richTextBox2.Text = str; } } /// <summary> /// 讀取文件 /// </summary> /// <typeparam name="T">類型</typeparam> /// <param name="path">路徑</param> /// <returns></returns> public static T BinaryDeserialize<T>(string path) { FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter b = new BinaryFormatter(); //SoapFormatter T rs; try { rs = (T)b.Deserialize(fileStream); return rs; } catch (System.Exception ex) { Console.WriteLine(ex.Message); } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } } return default(T); }
/// <summary> /// XML寫入 /// </summary> private void button5_Click(object sender, EventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.FileName = "File"; saveDlg.Title = "保存數據"; saveDlg.Filter = "File(*.txt)|*.txt"; saveDlg.DefaultExt = "txt"; saveDlg.AddExtension = true; //saveDlg.InitialDirectory = "D:AMC Results"; if (saveDlg.ShowDialog() == DialogResult.OK) { string str = "澤國江山入戰圖 生民何計樂樵蘇 憑君莫話封侯事 一將功成萬骨枯 "; SerializeToXml<string>(saveDlg.FileName, str); } } /// <summary> /// XML序列化某一類型到指定的文件 /// /// </summary> /// /// <param name="filePath">文件路徑</param> /// /// <param name="obj">寫入對象</param> /// /// <param name="type"></param> public static void SerializeToXml<T>(string filePath, T obj) { try { using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath)) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); xs.Serialize(writer, obj); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// <summary> /// XML讀取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) { OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Title = "File"; openDlg.Filter = "File (*.txt)|*.txt"; if (openDlg.ShowDialog() == DialogResult.OK) { string str = DeserializeFromXml<string>(openDlg.FileName); richTextBox3.Text = str; } } /// <summary> /// 從某一XML文件反序列化到某一類型 /// </summary> /// <param name="filePath">待反序列化的XML文件名稱</param> /// <param name="type">反序列化出的</param> /// <returns></returns> public static T DeserializeFromXml<T>(string filePath) { try { if (!System.IO.File.Exists(filePath)) throw new ArgumentNullException(filePath + " not Exists"); using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath)) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); T ret = (T)xs.Deserialize(reader); return ret; } } catch (Exception ex) { Console.WriteLine(ex.Message); return default(T); } }
以上就是關于“C#讀取寫入文件的方式有哪些”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。