DataHelp.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace LogoForceTestApp.Modules.MainModule
  8. {
  9. public class DataHelp
  10. {
  11. //写csv
  12. public static bool Save(string fullPath, string fileName, string RowName, string Data)
  13. {
  14. bool result = true;
  15. try
  16. {
  17. if (!Directory.Exists(fullPath))
  18. {
  19. Directory.CreateDirectory(fullPath);
  20. }
  21. if (fileName == null)
  22. {
  23. fileName = DateTime.Now.ToString("yyyyMMdd");
  24. }
  25. string text = "";
  26. string path = fullPath + "\\" + text;
  27. string text2 = ".csv";
  28. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
  29. if (!File.Exists(fullPath + "\\" + fileNameWithoutExtension + fileName + text2))
  30. {
  31. using (File.Create(fullPath + "\\" + fileNameWithoutExtension + fileName + text2))
  32. {
  33. }
  34. FileStream fileStream2 = new FileStream(fullPath + "\\" + fileNameWithoutExtension + fileName + text2, FileMode.Append);
  35. StreamWriter streamWriter = new StreamWriter(fileStream2, Encoding.UTF8);
  36. streamWriter.WriteLine(RowName);
  37. streamWriter.WriteLine(Data);
  38. streamWriter.Flush();
  39. streamWriter.Close();
  40. fileStream2.Close();
  41. }
  42. else
  43. {
  44. FileStream fileStream2 = new FileStream(fullPath + "\\" + fileNameWithoutExtension + fileName + text2, FileMode.Append);
  45. StreamWriter streamWriter = new StreamWriter(fileStream2, Encoding.UTF8);
  46. streamWriter.WriteLine(Data);
  47. streamWriter.Flush();
  48. streamWriter.Close();
  49. fileStream2.Close();
  50. }
  51. }
  52. catch
  53. {
  54. result = false;
  55. }
  56. return result;
  57. }
  58. }
  59. }