[C#] 自动文件,若制定目录下文件不存在,则会自动创建该目录及文件 →→→→→进入此内容的聊天室

来自 , 2019-08-04, 写在 C#, 查看 119 次.
URL http://www.code666.cn/view/5df07ecf
  1.  /*
  2.      自动文件,若制定目录下文件不存在,则会自动创建该目录及文件
  3.      */
  4.     class AutoFile
  5.     {
  6.  
  7.         public AutoFile(string directory, string name,string defaultContent)
  8.         {
  9.             if (Regex.IsMatch(name, "/"))
  10.             {
  11.                 throw new Exception("文件名中不应有/符号出现");
  12.             }
  13.             if (!Directory.Exists(directory))
  14.             {
  15.                 Directory.CreateDirectory(directory);
  16.                 File.Create(name);
  17.             }
  18.             _fileName =directory + "/" + name ;
  19.             if (!File.Exists(_fileName))
  20.             {
  21.               var file =  File.Create(_fileName);
  22.                 file.Close();
  23.                 File.WriteAllText(_fileName,defaultContent);
  24.             }
  25.         }
  26.  
  27.        
  28.         public void Apped(string str)
  29.         {
  30.             File.AppendAllText(_fileName,str);
  31.         }
  32.  
  33.         public void AppedLine(string str)
  34.         {
  35.             File.AppendAllLines(_fileName,new String[]{str});
  36.         }
  37.         public string[] ReadAllLines()
  38.         {
  39.             return File.ReadAllLines(_fileName);
  40.         }
  41.         private string _fileName;
  42.        
  43.         public string FileName
  44.         {
  45.             get { return _fileName; }
  46.             private set{ _fileName = value; }
  47.         }
  48.     }

回复 "自动文件,若制定目录下文件不存在,则会自动创建该目录及文件"

这儿你可以回复上面这条便签

captcha