1.打开文件
//打开XML文件,并识别一些其中的内容
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "配置文件|*.xml";
ofd.DefaultExt = "*.xml";
ofd.InitialDirectory = Application.StartupPath + "\\Cfg";
if (ofd.ShowDialog() == DialogResult.OK) //如果点击的是打开文件
{
string fileName = ofd.FileName; //获取全路径文件名
//MessageBox.Show("OK");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNode xml_CaseInfo = xmlDoc.SelectSingleNode("CaseInfo");
if (null == xml_CaseInfo)
return;
XmlNode xml_MaxPower = xml_CaseInfo.SelectSingleNode("NormalConfiguration");
XmlNodeList oList = xml_MaxPower.ChildNodes;
//XmlNode oCurrentNode;
stroutPutLevel = oList[0].Attributes.GetNamedItem("Value").Value;
strInputLevel = oList[1].Attributes.GetNamedItem("Value").Value;
strPMax = oList[2].Attributes.GetNamedItem("Value").Value;
}
}
2.文件另存为
方式一:移动文件
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "配置文件(*.xml)|*.xml";
sfd.DefaultExt = "*.xml";
sfd.InitialDirectory = Application.StartupPath + "\\Cfg\\Cellular";
if (sfd.ShowDialog() == DialogResult.OK)
{
string localFilePath = Application.StartupPath + "\\Cfg\\Cellular\\Case.xml";
string fileName = sfd.FileName;
if (System.IO.File.Exists(fileName))
{
if (MessageBox.Show("文件已经存在,确认覆盖吗?", "参数保存") == DialogResult.OK)
{
System.IO.File.Copy(localFilePath, fileName, true);
}
else
{
return;
}
}
else
{
//System.IO.Directory.Move(localFilePath, fileName);//剪切文件或文件夹
System.IO.File.Copy(localFilePath, fileName, true);//只能移动文件
MessageBox.Show("另存成功", "另存");
}
return;
}
方式二:递归移动文件夹
public void CopyUpdateFile(string srcPath, string aimPath)
{
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
if (!Directory.Exists(srcPath))
return;
string[] fileList = Directory.GetFileSystemEntries(srcPath);
foreach (string file in fileList)
{
int k = file.LastIndexOf("\\");
string filename = file.Substring(k + 1);
if (filename.ToLower() != Path.GetFileName(Application.ExecutablePath).ToLower())
{
if (Directory.Exists(file))
CopyUpdateFile(file, aimPath + Path.GetFileName(file));
else
{
try
{
File.Copy(file, aimPath + Path.GetFileName(file), true);
//File.Delete(file);
}
catch (Exception e)
{
Console.WriteLine("复制文件失败:" + file + e.Message);
continue;
}
}
}
}
}
方法三:批量移动文件到指定的文件夹
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件备份路径";
string foldPath = "";
if (dialog.ShowDialog() == DialogResult.OK)
{
foldPath = dialog.SelectedPath + @"\";//获取选择的路径
}
//通过某种方法获取要转移的文件的集合
List<string> TestList = GetSelectFile();
foreach (string File in FileList)
{
//使用方法二进行转移
}
3.删除文件
//System.IO.Directory.Delete(path);//删除文件
//System.IO.Directory.Delete(path, true); 删除文件夹
//递归删除文件夹及文件夹内所有文件
private void DeleteDir(string path)
{
if (path.Trim() == "" || !Directory.Exists(path))
return;
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] fileInfos = dirInfo.GetFiles();
if (fileInfos != null && fileInfos.Length > 0)
{
foreach (FileInfo fileInfo in fileInfos)
{
//DateTime.Compare( fileInfo.LastWriteTime,DateTime.Now);
File.Delete(fileInfo.FullName); //删除文件
}
}
DirectoryInfo[] dirInfos = dirInfo.GetDirectories();
if (dirInfos != null && dirInfos.Length > 0)
{
foreach (DirectoryInfo childDirInfo in dirInfos)
{
this.DeleteDir(childDirInfo.FullName); //递归
}
}
Directory.Delete(dirInfo.FullName, true); //删除目录
}