string l_strOrignSongtxtFullPath = Path.Combine(AddSong_Path, Path.Combine(Common.StaticPath.vod, Common.StaticPath.songtxt));
string l_strSongTxtBakFullPath = Path.Combine(System.IO.Path.GetDirectoryName(l_strOrignSongtxtFullPath),"songwpbak.txt");
try
{
//判断文件目录是否存在
if (File.Exists(l_strSongTxtBakFullPath))
{
File.Delete(l_strSongTxtBakFullPath);
}
else
{
File.Create(l_strSongTxtBakFullPath, 1000, FileOptions.WriteThrough);
}
//获取歌曲单内容
byte[] sr = File.ReadAllBytes(l_strOrignSongtxtFullPath);
FileInfo fi = new FileInfo(l_strOrignSongtxtFullPath);
long filelen = fi.Length;
FileStream l_FileStream = new FileStream(l_strSongTxtBakFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
//第二次运行会在上面一行出错
//StreamWriter l_StreamWriter = new StreamWriter(l_strSongTxtBakFullPath, true, Encoding.Default);
songcount = filelen / 173;
(int i = 0, j = 0; i < filelen; i += 173, j++)
{
if (sr[171 + i] == 0x0D && sr[172 + i] == 0x0A) //判断内容格式是否正确
{
string l_strSongPath = AddSong_Path + "\\" + Common.StaticPath.song + Encoding.Default.GetString(sr, j * 173, 2)
+ "\\" + Encoding.Default.GetString(sr, j * 173 + 2, 2)
+ "\\" + Encoding.Default.GetString(sr, j * 173 + 4, 2)
+ "\\" + Encoding.Default.GetString(sr, j * 173, 8);
//判断目录下是否存在这个视频文件 如果不存在
if (!File.Exists(l_strSongPath + ".mpg") && !File.Exists(l_strSongPath + ".avi") && !File.Exists(l_strSongPath + ".wav"))
{
//则根据歌单号 删除在歌单资料表的记录
string l_MstrSongNo = Encoding.Default.GetString(sr, j * 173, 8).ToString();
OleDbConnection MyConnection = new OleDbConnection(Common.CommonObject.constr);
MyConnection.Open();
string sqlstr = "delete * from 歌单资料 where 编号 ='" + l_MstrSongNo + "'";
OleDbCommand comm = new OleDbCommand(sqlstr, MyConnection);
comm.ExecuteNonQuery();
MyConnection.Close();
continue;
}
l_FileStream.Write(sr, 173 * j, 173); }
}
l_FileStream.Close();
l_FileStream.Dispose();
File.Delete(l_strOrignSongtxtFullPath);
File.Copy(l_strSongTxtBakFullPath, l_strOrignSongtxtFullPath, true);
File.Delete(l_strSongTxtBakFullPath);
Thread.Sleep(2000);
}
catch (IOException ee)
{
MessageBox.Show("文件操作异常:"+ee.ToString());
}
这种一般是没关闭引发的。你试试释放fi。