C# 文件在重写时被锁定
标题解释了一小部分,所以让我来解释两个场景。场景1会引发错误,场景2就像一个符咒 场景1: 我使用下面的方法签出文档,当文档保存到某个位置时,该位置已经是具有该名称的文件,它会被覆盖,但令人惊讶的是,出于某种原因,它也会锁定该文件:C# 文件在重写时被锁定,c#,file,filestream,fileinfo,filelock,C#,File,Filestream,Fileinfo,Filelock,标题解释了一小部分,所以让我来解释两个场景。场景1会引发错误,场景2就像一个符咒 场景1: 我使用下面的方法签出文档,当文档保存到某个位置时,该位置已经是具有该名称的文件,它会被覆盖,但令人惊讶的是,出于某种原因,它也会锁定该文件: public bool SaveDocument(int bestandsId, string fileName, string path) { //Initialize the Sql Query var sql = "SELECT DATA FR
public bool SaveDocument(int bestandsId, string fileName, string path)
{
//Initialize the Sql Query
var sql = "SELECT DATA FROM Documenten WHERE BESTAND_ID = " + bestandsId;
//Initialize SqlConnection
var connection = new SqlConnection(Instellingen.Instance.DmsConnectionString);
//Initialize SqlCommand
var command = new SqlCommand(sql, connection);
try
{
//Open Connection
connection.Open();
//Fill 'data' from command.ExecuteScalar()
var data = (byte[]) command.ExecuteScalar();
//Write 'data' to file.
File.WriteAllBytes(path + @"\" + fileName, data);
//Return true if no exceptions are raised.
return true;
}
catch (Exception ex)
{
//Initialize Dms Exception
var dmsEx = new DmsException(ex);
//Write Dms Exception to Log File.
DmsException.WriteErrorsToLog(dmsEx);
//Return false, because something went wrong...
return false;
}
finally
{
//Close Sql Connection
connection.Close();
}
}
该方法运行平稳。没有问题发生。但是,当我用下面的方法签入文档时,我得到了这个异常:
场景2:
当我使用SaveDocument
方法将文档保存到一个没有同名文件的位置时,该文件是新创建的,可以编辑,或者您想用它做什么
使用场景2非常有效。如上图所示,文档已准备好再次签入,不会收到错误
请求代码的人:@CodeCaster
---------------------------------开始编辑---------------------------------
public static bool InsertDocument(Document document)
{
try
{
//Exception is thrown when Initializing the FileStream
var fileStream = new FileStream(document.Fileinfo.FullName, FileMode.Open, FileAccess.Read);
var binaryReader = new BinaryReader(fileStream);
var totalNumberOfBytes = new FileInfo(document.Fileinfo.FullName).Length;
var data = binaryReader.ReadBytes((Int32) totalNumberOfBytes);
fileStream.Close();
fileStream.Dispose();
binaryReader.Close();
binaryReader.Dispose();
var pdftext = string.Empty;
try
{
if (document.DocumentType == ".pdf")
{
var reader = new PdfReader(document.Fileinfo.FullName);
var text = string.Empty;
for (var page = 1; page <= reader.NumberOfPages; page++)
{
text += PdfTextExtractor.GetTextFromPage(reader, page);
}
reader.Close();
pdftext = text;
}
}
catch (Exception ex)
{
var dmsEx = new DmsException(ex);
DmsException.WriteErrorsToLog(dmsEx);
}
return InsertIntoDatabase(document.BestandsNaam, document.Eigenaar, document.Omschrijving,
document.DatumToevoeg.ToString(), document.DatumIncheck.ToString(),
document.DatumUitcheck.ToString(), document.UitgechecktDoor,
document.DocumentType, data, pdftext, document.Versie, document.Medewerker,
document.DossierNummer, document.PersonalFolderId.ToString(),
document.DossierFolderId, -1, document.DocumentProgres,
document.OriBestandId.ToString(), 0);
}
catch (Exception ex)
{
var dmsEx = new DmsException("Fout bij inlezen voor toevoeging van nieuw document",
"Klasse Document (InsertDocument)", ex);
ExceptionLogger.LogError(dmsEx);
return false;
}
}
公共静态bool InsertDocument(文档)
{
尝试
{
//初始化文件流时引发异常
var fileStream=newfilestream(document.Fileinfo.FullName,FileMode.Open,FileAccess.Read);
var binaryReader=新的binaryReader(fileStream);
var totalNumberOfBytes=新文件信息(document.FileInfo.FullName).Length;
var data=binaryReader.ReadBytes((Int32)totalNumberOfBytes);
fileStream.Close();
Dispose();
binaryReader.Close();
Dispose();
var pdftext=string.Empty;
尝试
{
如果(document.DocumentType==“.pdf”)
{
var reader=newpdfreader(document.Fileinfo.FullName);
var text=string.Empty;
对于(变量页=1;页
与StreamWriter合作
using (FileStream fs = File.Create(newPath))
{
fs.Write(item.File, 0, item.File.Length);
}
或:
参考资料:这是您构建的一个可爱的SQL注入漏洞。请参数化您的SQL语句。@RaZor他说请!:P@RaZor我只是想减轻一下情绪。除了参数化是一件好事之外,我从未见过int
首先会导致SQL注入漏洞。如果您使用StreamWriter同样的问题也会发生?(使用“using”语句xD)此代码上是否有任何线程运行?您显示的代码没有保留正在使用的文件。请显示引发异常的代码。该问题的问题是file.Create()
将流返回到文件,使文件处于锁定状态。file.writealBytes()
没有这样的问题。
using (FileStream fs = File.Create(newPath))
{
fs.Write(item.File, 0, item.File.Length);
}
File.WriteAllBytes(newPath, item.File);