![](/img/trans.png)
[英]The process cannot access the file .exe because it is being used by another process
[英]The process cannot access the file because it is being used by another process
我從SQL Server數據庫字段獲取二進制數據,並在我的應用程序具有權限的目錄中本地創建文檔。 但是我仍然得到標題中指定的錯誤。 我已嘗試在網上發布了許多建議,包括在Stackoverflow上的帖子中提出的建議。 我還使用了ProcessExplorer> Find Handle來找到鎖,它沒有返回任何內容,好像文件沒有被鎖定一樣。
我使用下面的代碼將文件保存到文件系統,然后我嘗試在另一個方法的應用程序進程中將此文件復制到新位置。 正是這種復制方法采用了拋出異常的新創建文件的路徑。
文件本身是用它的內容創建的,我可以通過Windows資源管理器打開它,沒有任何問題。
我錯過了一些完全明顯的東西嗎 我是否正確地從數據庫創建文件? 任何有關解決或更好地診斷問題的幫助將非常感激。
// Get file from DB
FileStream fs = new FileStream(
"C:\myTempDirectory\myFile.doc", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter br = new BinaryWriter(fs);
br.Write("BinaryDataFromDB");
fs.Flush();
fs.Close();
fs.Dispose();
// Copy file
File.Copy(sourceFileName, destinationFilename, true);
在處理完流后,嘗試添加對GC.Collect()
的調用以強制清理垃圾收集器。
// Get file from DB
using(FileStream fs = new FileStream("C:\myTempDirectory\myFile.doc", FileMode.OpenOrCreate, FileAccess.Write))
using(BinaryWriter br = new BinaryWriter(fs))
{
br.Write("BinaryDataFromDB");
fs.Flush();
}
//Force clean up
GC.Collect();
// Copy file
File.Copy(sourceFileName, destinationFilename, true);
更改您的代碼如下,問題是當您需要它時,文件流不會被垃圾收集:
// Get file from DB
using (FileStream fs = new FileStream("C:\myTempDirectory\myFile.doc", FileMode.OpenOrCreate, FileAccess.Write))
{
BinaryWriter br = new BinaryWriter(fs);
br.Write("BinaryDataFromDB");
fs.Flush();
fs.Close();
}
// Copy file
File.Copy(sourceFileName, destinationFilename, true);
怎么using
?
string source = @"C:\myTempDirectory\myFile.doc";
using(FileStream fs = new FileStream(
source, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
{
BinaryWriter br = new BinaryWriter(fs);
br.Write("BinaryDataFromDB");
}
File.Copy(sourceFileName, destinationFilename, true);
編輯 :嘗試通知FileShare.Read
權限。
我開始明白,雖然錯誤調試器說錯誤在於此代碼..但這不是錯誤的地方..我遇到了類似的問題,我帶來了這個解決方案..考慮上面的所有帖子..可能它可以幫助一些人..
從數據庫中檢索並使用正常編碼將其保存為“temp.bmp”時,我使用Image在圖片框中顯示它,而不使用此代碼時使用“using”關鍵字:
PictureBox1.Image = Image.FromFile("temp.bmp");
它提出了錯誤,我真的沒有錯誤的頭和尾...所以我想出了這個解決方案。
而不是直接分配它嘗試此代碼:
Bitmap img;
using (Bitmap bmp = new Bitmap("temp.bmp"))
{
img = new Bitmap(bmp);
}
pictureBox1.Image = img;
來到文件流部分我剛才使用了普通代碼,如下所示:
FileStream fs = new FileStream("filepath",FileMode.Create);
它像一塊蛋糕一樣工作
它確實有幫助
是不是可以同時使用Filestream和BinaryWriter? 例如,不能在同一文件上同時調用streamwriter和streamreader。 但是,據我所知,這沒有意義,在這種情況下不應該這樣。 也許嘗試關閉br?
在執行復制之前,請嘗試處理BinaryWriter對象。
是否有后台進程(如防病毒或文件系統索引)將文件鎖定的時間足夠長,導致應用程序出現問題? 嘗試暫停3秒鍾以查看問題是否消失。
using System.Threading;
fs.Dispose();
Thread.Sleep(3000);
// Copy file
這就是我做的:
Stream stream = new MemoryStream();
var tempStream = new FileStream(pathToFile, FileMode.Open);
tempStream.CopyTo(stream);
tempStream.Close();
然后在任何地方使用stream
對象。
我發布申請后似乎才遇到這個問題。 如果您關閉消息框並再次嘗試調試,則不會發生任何事情。 MS Help表示要完全退出Visual Studio並重新啟動它。 它更容易調出任務管理器並結束這個過程。 它的名稱將是解決方案名稱,后跟“.vshost”(例如項目名稱:PLC.sln;進程名稱:PLC.vshost.exe)。 該過程將自動重啟。 在Windows 7中,通常可以在結束進程時看到某些事情發生。 但是在10中,任務管理器窗口很少改變。 我在更改代碼后嘗試測試程序時收到此消息。 顯然,當停止調試時,.exe不會關閉,當VS嘗試編寫新編譯的exe文件時,會發生錯誤。 我有時必須結束該過程兩次以防止錯誤消息返回。 (我必須兩次遇到錯誤;結束進程兩次然后重新運行沒有幫助。)這是Visual Studio中的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.