我制作了自动加载用户设置的xml文件的程序,但问题是当我在代码中添加新值并且程序尝试读取没有“Data5”值的xml文件时,程序返回错误:
当我关闭程序时,我收到另一个关于另一个进程使用的文件的错误
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'C:\Users\xxxxx\AppData\Local\APK Easy Tool\cfg.xml' because it is being used by another process.
我不明白为什么文件仍然被锁定。它应该在read.Close();
之后关闭。我尝试让程序在读取xml之前在xml中创建一个新值,但这不是一个好主意,因为它将替换修改后的值。这是我的代码
docs = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\APK";
if (File.Exists(docs + @"\cfg.xml"))
{
try
{
XmlSerializer xs = new XmlSerializer(typeof(Data));
FileStream read = new FileStream(docs + @"\cfg.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Data info = (Data)xs.Deserialize(read);
signApkCheckBox.Checked = bool.Parse(info.Data1);
pathOfApk.Text = info.Data2;
pathOfDec.Text = info.Data3;
pathOfCom.Text = info.Data4;
disableDebugChkBox.Checked = bool.Parse(info.Data5);
read.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ApkForm_FormClosed(object sender, FormClosedEventArgs e)
{
/*
Properties.Settings.Default.textBoxDec = pathOfDec.Text;
Properties.Settings.Default.textBoxCom = pathOfCom.Text;
Properties.Settings.Default.textBoxSelApk = pathOfApk.Text;
Properties.Settings.Default.checkSignApk = signApkCheckBox.Checked;
Properties.Settings.Default.Save();
*/
Data info = new Data();
info.Data1 = signApkCheckBox.Checked.ToString();
info.Data2 = pathOfApk.Text;
info.Data3 = pathOfDec.Text;
info.Data4 = pathOfCom.Text;
info.Data5 = disableDebugChkBox.Checked.ToString();
SaveXML.SaveData(info, docs + @"\cfg.xml");
}
SaveXML.cs
using System.Xml.Serialization;
using System.IO;
namespace APK
{
public class SaveXML
{
public static void SaveData(object obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
}
}
Data.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace APK
{
public class Data
{
private string data1, data2, data3, data4, data5;
public string Data1
{
get { return data1; }
set { data1 = value; }
}
public string Data2
{
get { return data2; }
set { data2 = value; }
}
public string Data3
{
get { return data3; }
set { data3 = value; }
}
public string Data4
{
get { return data4; }
set { data4 = value; }
}
public string Data5
{
get { return data5; }
set { data5 = value; }
}
}
}
答案 0 :(得分:1)
也许这不能解决问题,但在read.close();
放read.EndRead();
之前或者只是尝试在catch
语句上关闭它。
但正如已经说过的那样,最好的选择是这样做:
if (File.Exists(docs + @"\cfg.xml"))
{
try
{
using (FileStream read = new FileStream(docs + @"\cfg.xml", FileMode.Open, FileAccess.Read, FileShare.Read)){
XmlSerializer xs = new XmlSerializer(typeof(Data));
Data info = (Data)xs.Deserialize(read);
signApkCheckBox.Checked = bool.Parse(info.Data1);
pathOfApk.Text = info.Data2;
pathOfDec.Text = info.Data3;
pathOfCom.Text = info.Data4;
disableDebugChkBox.Checked = bool.Parse(info.Data5);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这样你根本不用担心关闭文件,它是“自动”完成的