我正在开发xamarin.ios应用程序,它在两个三个月之前运行良好,没有任何问题。但是现在我偶尔会收到以下错误。
“路径上的共享冲突/用户/ vofox3 / Library / Developer / CoreSimulator / Devices / A357B468-BDBA-4397-B2D7-40A6333E93CD / data / Containers / Data / Application / 5D715BAA-DD43-404D-8D94-0E11224745E8 / Documents /WBidMax/User.xml”。
我使用下面的代码来读取User.XML文件。
/// <summary>
/// Load configuration details from XML
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath"></param>
/// <returns></returns>
public static T DeserializeFromXml<T>(string filePath)
{
try
{
T wBidConfiguration;
using (TextReader configurationFileStream = new StreamReader(filePath))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
wBidConfiguration = (T)xmlSerializer.Deserialize(configurationFileStream);
return wBidConfiguration;
}
}
catch (Exception ex)
{
throw;
}
}
以下用于编写XML文件的代码。
public static bool SerializeToXml<T>(T configType, string filePath)
{
bool status = false;
try
{
XmlWriterSettings xmlWriterSettings;
XmlSerializerNamespaces xmlSerializerNamespaces;
xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
Encoding = Encoding.UTF8
CloseOutput = true
};
xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add("", "");
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
using (FileStream configurationFileStream = new FileStream(filePath, FileMode.Create))
{
using (XmlWriter xmlWriter = XmlWriter.Create(configurationFileStream, xmlWriterSettings))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(xmlWriter, configType, xmlSerializerNamespaces);
}
}
status = true;
}
catch (Exception ex)
{
status = false;
}
return status;
}
你能告诉我这个错误的原因吗?
答案 0 :(得分:0)
看起来发生了并发文件访问。在写入操作期间锁定文件可能会为您解决此问题。特别是如果您从不同的线程或任务访问该文件。