You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
5.2 KiB
200 lines
5.2 KiB
using System;
|
|
using System.IO;
|
|
using System.Xml;
|
|
|
|
namespace SiaSun.LMS.Common
|
|
{
|
|
/// <summary>
|
|
/// 文件读写类
|
|
/// </summary>
|
|
public class File
|
|
{
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public File()
|
|
{
|
|
}
|
|
|
|
#region ------读写.txt文件
|
|
/// <summary>
|
|
/// 向txt文件中写内容
|
|
/// </summary>
|
|
/// <param name="contents">写入内容</param>
|
|
/// <param name="path">文件路径</param>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从txt文件中读数据
|
|
/// </summary>
|
|
/// <param name="path">文件路径</param>
|
|
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文件
|
|
/// <summary>
|
|
/// 读xml文件中指定节点的InnerText值
|
|
/// </summary>
|
|
/// <param name="path">xml文件路径名</param>
|
|
/// <param name="nodeName">节点路径</param>
|
|
/// <returns>返回节点值</returns>
|
|
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);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 向xml文件中指定节点赋值
|
|
/// </summary>
|
|
/// <param name="path">xml文件路径名</param>
|
|
/// <param name="nodeName">节点路径</param>
|
|
/// <param name="nodeValue">节点值</param>
|
|
/// <returns>返回节点值</returns>
|
|
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 ------判断指定文件夹是否存在,不存在则创建
|
|
|
|
/// <summary>
|
|
/// 创建并返回保存目录,在执行程序的磁盘目录下创建,指定文件夹
|
|
/// </summary>
|
|
/// <param name="SavedDicName">保存的文件夹路径</param>
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// //判断文件是否存在,不存在,创建
|
|
/// </summary>
|
|
/// <param name="strFile"></param>
|
|
/// <returns></returns>
|
|
public static string GetFile(string strFile)
|
|
{
|
|
if (!System.IO.File.Exists(strFile))
|
|
{
|
|
System.IO.File.Create(strFile);
|
|
}
|
|
|
|
return strFile;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ------向指定文件夹路径,以日期为文件名,创建文件并写入信息
|
|
|
|
/// <summary>
|
|
/// 向指定文件中写入信息
|
|
/// </summary>
|
|
/// <param name="RootDicPath">上级目录路径</param>
|
|
/// <param name="Content">写入内容</param>
|
|
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
|
|
}
|
|
}
|