Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.43 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to check if file content is empty?
  2. class LastUsed
  3.     {
  4.         private static string dir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"Folder";
  5.         private static string file = dir + @"Settings.txt";
  6.         private string text;
  7.  
  8.         public void CheckFileStatus()
  9.         {
  10.             if (!Directory.Exists(dir))
  11.             {
  12.                 DirectoryInfo directory = Directory.CreateDirectory(dir);
  13.             }
  14.             if (!File.Exists(file))
  15.             {
  16.                 using (FileStream fileStream = File.Create(file))
  17.                 {
  18.                 }
  19.             }
  20.         }
  21.  
  22.         private void SetFileText(string writeText)
  23.         {
  24.             using (StreamWriter streamWriter = new StreamWriter(file))
  25.             {
  26.                 streamWriter.Write(writeText);
  27.             }
  28.         }
  29.  
  30.         private string GetFileText()
  31.         {
  32.             string readText;
  33.  
  34.             using (StreamReader streamReader = File.OpenText(file))
  35.             {
  36.                 readText = streamReader.ReadLine();
  37.             }
  38.  
  39.             return readText;
  40.         }
  41.  
  42.         public string Text
  43.         {
  44.             set
  45.             {
  46.                 text = value;
  47.                 SetFileText(text);
  48.             }
  49.             get
  50.             {
  51.                 return GetFileText();
  52.             }
  53.         }
  54.        
  55. if (new FileInfo(fileName).Length ==0){
  56.   // file is empty
  57. } else {
  58.   // there is something in it
  59. }