using System;
using System.IO;
using System.Xml;
namespace SiaSun.LMS.Common
{
///
/// 文件读写类
///
public class File
{
///
/// 构造函数
///
public File()
{
}
#region ------读写.txt文件
///
/// 向txt文件中写内容
///
/// 写入内容
/// 文件路径
static public void WriteToText(string contents,string path)
{
StreamWriter sr = null;
try
{
if (System.IO.File.Exists(path))
sr = new StreamWriter(path);
else
sr = System.IO.File.CreateText(path);
sr.WriteLine(contents);
}
catch(IOException iox)
{
throw new Exception("Faile to Write to File",iox);
}
finally
{
sr.Close();
}
}
///
/// 从txt文件中读数据
///
/// 文件路径
static public string ReadFromText(string path)
{
StreamReader sr = null;
try
{
if (!System.IO.File.Exists(path))
return string.Empty;
sr = System.IO.File.OpenText(path);
System.Text.StringBuilder input = new System.Text.StringBuilder();
input.Append(sr.ReadToEnd());
return input.ToString();
}
catch(IOException iox)
{
throw new Exception("Faile to Read File",iox);
}
finally
{
sr.Close();
}
}
#endregion
#region ------读写XML文件
///
/// 读xml文件中指定节点的InnerText值
///
/// xml文件路径名
/// 节点路径
/// 返回节点值
public static string ReadXml(string path,string nodeName)
{
XmlDocument docXML = new XmlDocument();
try
{
docXML.Load(path);
XmlNode node = docXML.SelectSingleNode(nodeName);
docXML.Save(path);
return node.InnerText;
}
catch(XmlException ex)
{
throw new Exception("Faile to Read XML File",ex);
}
}
///
/// 向xml文件中指定节点赋值
///
/// xml文件路径名
/// 节点路径
/// 节点值
/// 返回节点值
public static void WriteXml(string path,string nodeName,string nodeValue)
{
try
{
XmlDocument docXML = new XmlDocument();
docXML.Load(path);
XmlNode node = docXML.SelectSingleNode(nodeName);
node.InnerText = nodeValue;
docXML.Save(path);
}
catch(XmlException ex)
{
throw new Exception("Faile to Write XML File",ex);
}
}
#endregion
#region ------判断指定文件夹是否存在,不存在则创建
///
/// 创建并返回保存目录,在执行程序的磁盘目录下创建,指定文件夹
///
/// 保存的文件夹路径
public static string GetDicPath(string SavedDicName)
{
//获得根目录
string rootDic = Directory.GetDirectoryRoot(AppDomain.CurrentDomain.BaseDirectory);
string path = Path.Combine(rootDic, SavedDicName);
//判断根目录指定文件夹是否存在
if (!Directory.Exists(SavedDicName))
{
Directory.CreateDirectory(path);
}
return path;
}
///
/// //判断文件是否存在,不存在,创建
///
///
///
public static string GetFile(string strFile)
{
if (!System.IO.File.Exists(strFile))
{
System.IO.File.Create(strFile);
}
return strFile;
}
#endregion
#region ------向指定文件夹路径,以日期为文件名,创建文件并写入信息
///
/// 向指定文件中写入信息
///
/// 上级目录路径
/// 写入内容
public static void WriteToEveryDayFile(string DicName, string Content)
{
//获得或创建指定文件夹路径
string dicPath = GetDicPath(DicName);
//获得写入的文件名
string fileName = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
//文件路径
string filePath = Path.Combine(dicPath, fileName + ".txt");
//创建文件流,写入文件
FileStream flStream = null;
StreamWriter writer = null;
try
{
flStream = new FileStream(filePath, FileMode.Append, FileAccess.Write);
writer = new StreamWriter(flStream);
writer.WriteLine(DateTime.Now.ToString());
writer.Write(Content);
writer.WriteLine();
writer.WriteLine();
writer.Flush();
}
catch (IOException ex)
{
throw new Exception(ex.Message);
}
finally
{
writer.Close();
flStream.Close();
}
}
#endregion
}
}