📄 filehelper.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using Library;
namespace DAL
{
public static class FileHelper
{
/// <summary>
/// 将表中数据保存到文件夹folderName下的xml文件xmlName;
/// </summary>
/// <param name="xmlName">要保存的xml文件名称</param>
/// <param name="folderName">文件所在路径,但是并不包括文件名称(Application.StartupPath + Path.DirectorySeparatorChar + "FolderName" + Path.DirectorySeparatorChar;)</param>
/// <param name="dataTable">要保存的数据表</param>
/// <returns>保存成功返回true,否则是false</returns>
public static SuceedFailMessage WriteToXml(string xmlName, string serverFilePath, DataTable dataTable)
{
serverFilePath = serverFilePath + xmlName + ".xml";
SuceedFailMessage isSuceed = new SuceedFailMessage();
try
{
if (!Directory.Exists(Path.GetDirectoryName(serverFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));
}
dataTable.WriteXml(serverFilePath, XmlWriteMode.WriteSchema);
isSuceed.succeedFail = SucceedFail.Succeed;
isSuceed.message = "Suceed";
return isSuceed;
}
catch (IOException ex)
{
isSuceed.succeedFail = SucceedFail.Fail;
isSuceed.message = ex.Message;
return isSuceed;
}
catch (Exception ex)
{
isSuceed.succeedFail = SucceedFail.Fail;
isSuceed.message = ex.Message;
return isSuceed;
}
}
/// <summary>
/// 从xml文件中读数据,返回一个DataTable
/// </summary>
/// <param name="xmlName">xml文件名称</param>
/// <param name="serverFilePath">xml文件所在路径</param>
/// <returns>DataTable</returns>
public static DataTable ReadFromXml(string xmlName, string serverFilePath)
{
serverFilePath = serverFilePath + xmlName + ".xml";
bool isExistsPath = File.Exists(serverFilePath);
DataTable dataTable = new DataTable(xmlName);
try
{
if (isExistsPath)
{
dataTable.ReadXml(serverFilePath);
}
else
{
dataTable = null;
}
return dataTable;
}
catch (IOException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -