文件.Compact Framework 2.0不支持移动然后删除
本文关键字:移动 然后 删除 不支持 Compact Framework 文件 | 更新日期: 2023-09-27 17:49:28
下面的代码在第二次Delete调用时崩溃。
using (var str = new StreamWriter(newFileName))
{
foreach (Entry entry in this.Entries)
{
str.WriteLine(
String.Format(
@"""{0}"";{1:yyyy-dd-MMThh:mm:ss};""none"""
, entry.Data
, entry.Date
)
);
}
}
File.Delete(delFileName);
File.Move(curFileName, delFileName);
File.Move(newFileName, curFileName);
File.Delete(delFileName); // Crash
"The process can not access the file '''asld.csv' because it is being used by another process."
所以它就像File.Move(curFileName, delFileName)
在文件上引起锁(或其他东西)并且之后不释放它。
注意:我正在使用Visual Studio 2008模拟的智能设备。
我修好了。
导致问题的原因实际上是在方法发生异常之前的某个时间点调用的另一个方法。
我有一个"load"方法,我忘记了using子句。
private void load()
{
this.lstEntries = new List<Entry>();
var delFileName = String.Format(@"{1}'{0}d.csv", this.FilePrefix, this.Folder);
var curFileName = String.Format(@"{1}'{0}c.csv", this.FilePrefix, this.Folder);
if (File.Exists(delFileName) && !File.Exists(curFileName))
{
curFileName = delFileName;
}
if (File.Exists(curFileName))
{
using (var str = new StreamReader(curFileName)) // fixed with using