Advertisement
Guest User

FileHelper

a guest
May 16th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. class FileHelper
  2.     {
  3.         public void WriteStringToFile(string str, string filename)
  4.         {
  5.             string path = PathForDocumentsFile(filename);
  6.             FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write);
  7.  
  8.             StreamWriter sw = new StreamWriter(file);
  9.             sw.WriteLine(str);
  10.  
  11.             sw.Close();
  12.             file.Close();
  13.         }
  14.  
  15.         public string PathForDocumentsFile(string filename)
  16.         {
  17.                 string path = Application.persistentDataPath;
  18.                 path = path.Substring(0, path.LastIndexOf('/'));
  19.                 return Path.Combine(path, filename);
  20.         }
  21.  
  22.         public string ReadStringFromFile(string filename)
  23.         {
  24.             string path = PathForDocumentsFile(filename);
  25.  
  26.             if (File.Exists(path))
  27.             {
  28.                 FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
  29.                 StreamReader sr = new StreamReader(file);
  30.  
  31.                 string str = null;
  32.                 str = sr.ReadLine();
  33.  
  34.                 sr.Close();
  35.                 file.Close();
  36.  
  37.                 return str;
  38.             }
  39.  
  40.             else
  41.             {
  42.                 return null;
  43.             }
  44.         }
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement