国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現多線程寫入同一個文件的方法

2019-10-29 21:39:03
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#實現多線程寫入同一個文件的方法,涉及C#多線程操作文件讀寫的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#實現多線程寫入同一個文件的方法。分享給大家供大家參考。具體實現方法如下:

 

  1. namespace WfpApp 
  2. public partial class Form2 : Form 
  3. object obj = new object(); 
  4. public Form2() 
  5. InitializeComponent(); 
  6. System.Threading.Thread thread; 
  7. string[] users = new string[] { "zkk""admin""administrator""soft""iany""nec""necsl" }; 
  8. for (int i = 0; i < users.Length; i++) 
  9. thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteLock)); 
  10. thread.Start(users[i]); 
  11. public void WriteThread(object user) 
  12. string path = System.Windows.Forms.Application.StartupPath + "//app//"
  13. if (!System.IO.Directory.Exists(path)) 
  14. System.IO.Directory.CreateDirectory(path); 
  15. path = path + "//" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; 
  16. StringBuilder sb = new StringBuilder(); 
  17. sb.AppendLine("----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--------------------------"); 
  18. sb.AppendLine(user.ToString()); 
  19. sb.AppendLine("---------------------------------------------------------------------------------"); 
  20. sb.AppendLine(); 
  21. //if (!System.IO.File.Exists(path)) 
  22. // System.IO.File.Create(path).Close(); 
  23. System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); 
  24. System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream, Encoding.Default); 
  25. sw.Write(sb.ToString()); 
  26. sw.Close(); 
  27. sw.Dispose(); 
  28. fileStream.Close(); 
  29. fileStream.Dispose(); 
  30. public void WriteLock(object user) 
  31. lock (obj) 
  32. string path = System.Windows.Forms.Application.StartupPath + "//app//"
  33. if (!System.IO.Directory.Exists(path)) 
  34. System.IO.Directory.CreateDirectory(path); 
  35. path = path + "//" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; 
  36. StringBuilder sb = new StringBuilder(); 
  37. sb.AppendLine("----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--------------------------"); 
  38. sb.AppendLine(user.ToString()); 
  39. sb.AppendLine("---------------------------------------------------------------------------------"); 
  40. sb.AppendLine(); 
  41. if (!System.IO.File.Exists(path)) 
  42. System.IO.File.Create(path).Close(); 
  43. System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write); 
  44. System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream, Encoding.Default); 
  45. sw.Write(sb.ToString()); 
  46. sw.Close(); 
  47. sw.Dispose(); 
  48. fileStream.Close(); 
  49. fileStream.Dispose(); 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌县| 平利县| 新兴县| 贞丰县| 巴彦淖尔市| 新干县| 额敏县| 灵川县| 白玉县| 大港区| 温宿县| 宿迁市| 安陆市| 榆中县| 普宁市| 荣成市| 栾川县| 高邮市| 香港 | 浏阳市| 昌宁县| 中牟县| 宁波市| 长丰县| 察隅县| 郎溪县| 尼勒克县| 兰州市| 三明市| 泸定县| 青海省| 富平县| 浮山县| 盐亭县| 工布江达县| 东乡族自治县| 什邡市| 瑞金市| 正定县| 长沙市| 西安市|