Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 127.4k

How to add numbers in a text file together with file name

Apr 25 2019 11:45 PM
I want to check the size of text file i.e. if the file size is greater then 500kb then create another file with numbers.
Example:
Currently my file is saved like : NetApiLog_2019_04_25.txt 
 
Here is the code to save file-
  1. public void writeLogFile(string errorln)  
  2. {  
  3. FileStream fs = null;  
  4. try  
  5. {  
  6. var common = (ICommon)new CommonAttribute();  
  7. var netVpbxApiLogFlag = common.GetConfigurationByKeyName(string.Empty, "NetVpbxApiLogFlag");  
  8. if (Convert.ToBoolean(netVpbxApiLogFlag))  
  9. {  
  10. var logFilePath = common.GetConfigurationByKeyName(string.Empty, "NetApiLogFilePath");  
  11. string FileName = string.Format("{0}_{1}.txt", logFilePath,  
  12. DateTime.Today.ToString("yyyy_MM_dd"));  
  13.   
  14. if (!Directory.Exists(logFilePath))  
  15. {  
  16. Directory.CreateDirectory(logFilePath);  
  17. }  
  18.   
  19. fs = new FileStream(FileName, FileMode.Append, FileAccess.Write);  
  20. using (StreamWriter sw = new StreamWriter(fs))  
  21. {  
  22. fs = null;  
  23. sw.WriteLine("Start VPBX Api.");  
  24. sw.WriteLine("-------------------------------------------------------");  
  25. sw.WriteLine(string.Format("{0}:{1}", DateTime.Now.ToString(), errorln));  
  26. }  
  27. }  
  28. }  
  29. catch (Exception ex)  
  30. {  
  31. throw ex;  
  32. }  
  33. finally  
  34. {  
  35. if (fs != null)  
  36. {  
  37. fs.Dispose();  
  38. }  
  39. }  
  40. }  
If the file size is greater then 500kb then create the file like : NetApiLog_1_2019_04_25.txt
if the size of file text file  NetApiLog_1_2019_04_25.txt greater then 500 kb then the name should be NetApiLog_2_2019_04_25.txt.
 
 
Can anyone help me to how to write in c# asp .net.?? 
 

Answers (2)