[c#]代码库
/*
自动文件,若制定目录下文件不存在,则会自动创建该目录及文件
*/
class AutoFile
{
public AutoFile(string directory, string name,string defaultContent)
{
if (Regex.IsMatch(name, "/"))
{
throw new Exception("文件名中不应有/符号出现");
}
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
File.Create(name);
}
_fileName =directory + "/" + name ;
if (!File.Exists(_fileName))
{
var file = File.Create(_fileName);
file.Close();
File.WriteAllText(_fileName,defaultContent);
}
}
public void Apped(string str)
{
File.AppendAllText(_fileName,str);
}
public void AppedLine(string str)
{
File.AppendAllLines(_fileName,new String[]{str});
}
public string[] ReadAllLines()
{
return File.ReadAllLines(_fileName);
}
private string _fileName;
public string FileName
{
get { return _fileName; }
private set{ _fileName = value; }
}
}