使用 FileStream returns 创建一个 InvalidOperationException 文件
Creating a file with FileStream returns an InvalidOperationException
它 returns exception
具体在 line 12
。
public void saveToXML()
{
URL newURL = new URL();
newURL.type = type;
newURL.name = name;
newURL.info = info;
newURL.url = url;
newURL.isProtected = isProtected;
newURL.amountOfClicks = amountOfClicks;
XmlSerializer xml = new XmlSerializer(typeof(URL));
string directory = @"C:\Users\PC-User\Documents\Link" + newURL.name + ".xml";
using (var file = File.Create(directory))
{
xml.Serialize(file, url);
}
}
如果需要,异常消息中有更多详细信息:
Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.
谢谢!
考虑这样的事情? :
public async Task saveToXml(){
string directory = @"C:\Users\PC-User\Documents\Link" + newURL.name + ".xml";
await Task.Run(()=>
{
Task.Yield();
using (var file = File.Create(directory))
{
xml.Serialize(file, url);
}
});
}
它 returns exception
具体在 line 12
。
public void saveToXML()
{
URL newURL = new URL();
newURL.type = type;
newURL.name = name;
newURL.info = info;
newURL.url = url;
newURL.isProtected = isProtected;
newURL.amountOfClicks = amountOfClicks;
XmlSerializer xml = new XmlSerializer(typeof(URL));
string directory = @"C:\Users\PC-User\Documents\Link" + newURL.name + ".xml";
using (var file = File.Create(directory))
{
xml.Serialize(file, url);
}
}
如果需要,异常消息中有更多详细信息:
Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.
谢谢!
考虑这样的事情? :
public async Task saveToXml(){
string directory = @"C:\Users\PC-User\Documents\Link" + newURL.name + ".xml";
await Task.Run(()=>
{
Task.Yield();
using (var file = File.Create(directory))
{
xml.Serialize(file, url);
}
});
}