C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
更新時(shí)間:2015年08月22日 10:29:36 作者:我心依舊
這篇文章主要介紹了C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法,涉及C#多線程操作文件讀寫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
namespace WfpApp { public partial class Form2 : Form { object obj = new object(); public Form2() { InitializeComponent(); System.Threading.Thread thread; string[] users = new string[] { "zkk", "admin", "administrator", "soft", "iany", "nec", "necsl" }; for (int i = 0; i < users.Length; i++) { thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteLock)); thread.Start(users[i]); } } public void WriteThread(object user) { string path = System.Windows.Forms.Application.StartupPath + "\\app\\"; if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; StringBuilder sb = new StringBuilder(); sb.AppendLine("----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--------------------------"); sb.AppendLine(user.ToString()); sb.AppendLine("---------------------------------------------------------------------------------"); sb.AppendLine(); //if (!System.IO.File.Exists(path)) // System.IO.File.Create(path).Close(); System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream, Encoding.Default); sw.Write(sb.ToString()); sw.Close(); sw.Dispose(); fileStream.Close(); fileStream.Dispose(); } public void WriteLock(object user) { lock (obj) { string path = System.Windows.Forms.Application.StartupPath + "\\app\\"; if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; StringBuilder sb = new StringBuilder(); sb.AppendLine("----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--------------------------"); sb.AppendLine(user.ToString()); sb.AppendLine("---------------------------------------------------------------------------------"); sb.AppendLine(); if (!System.IO.File.Exists(path)) System.IO.File.Create(path).Close(); System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write); System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream, Encoding.Default); sw.Write(sb.ToString()); sw.Close(); sw.Dispose(); fileStream.Close(); fileStream.Dispose(); } } } }
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
Unity shader實(shí)現(xiàn)高斯模糊效果
這篇文章主要為大家詳細(xì)介紹了Unity shader實(shí)現(xiàn)高斯模糊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#緩存之SqlCacheDependency用法實(shí)例總結(jié)
這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計(jì)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08C#使用AutoResetEvent實(shí)現(xiàn)同步
這篇文章主要為大家詳細(xì)介紹了C#使用AutoResetEvent實(shí)現(xiàn)同步的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Winform中GridView分組排序功能實(shí)現(xiàn)方法
這篇文章主要介紹了Winform中GridView分組排序功能實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)說明了分組排序的實(shí)現(xiàn)方法,并附帶完整的DBHelp類代碼,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11C# 9 新特性——record的相關(guān)總結(jié)
這篇文章主要介紹了C# 9 新特性——record的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c# 9的新特性,感興趣的朋友可以了解下2021-02-02