当我调试我的项目时,我收到以下错误:
"无法将文件"obj\Debug\My Dream.exe"复制到"bin\Debug\My Dream.exe".进程无法访问文件'bin\Debug\My Dream.exe',因为它正被另一个人使用处理."
使用Process Explorer,我看到MyApplication.exe已经关闭但系统进程仍然使用它,尽管我之前停止了调试.每当我更改代码并开始调试时,它都会发生.如果我将项目复制到USB并进行调试,它运行正常.
为什么?我该如何解决这个错误?
我使用Window 7 Professional.使用Xp我从未遇到过这个错误.
我有一个功能,发送消息(很多)和他们的附件.
它基本上循环遍历目录结构,并从文件结构创建电子邮件
c:\emails\message01
\attachments
c:\emails\message02
\attachments
Run Code Online (Sandbox Code Playgroud)
使用.net c#,标准内容创建消息.
创建所有消息后......我有另一个直接运行的函数,它将消息文件夹复制到另一个位置.
问题是 - 文件被锁定了......
注意:我没有移动文件,只是复制它们....
有关如何使用c#复制锁定文件的任何建议?
更新
我有这个添加附件方法
private void AddAttachments(MailMessage mail)
{
string attachmentDirectoryPath = "c:\messages\message1";
DirectoryInfo attachmentDirectory = new DirectoryInfo(attachmentDirectoryPath);
FileInfo[] attachments = attachmentDirectory.GetFiles();
foreach (FileInfo attachment in attachments)
{
mail.Attachments.Add(new Attachment(attachment.FullName));
}
}
Run Code Online (Sandbox Code Playgroud) 我发送文件作为附件:
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
Run Code Online (Sandbox Code Playgroud)
然后我想将文件移动到另一个文件夹,但是当我尝试这样做时
try
{
//File.Open(oldFullPath, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
File.Move(oldFullPath, newFullPath);
}
catch (Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)
它抛出了一个异常,即该文件已在另一个进程中使用.如何解锁此文件以便将其移动到此位置?
有没有办法检测指定文件夹中的锁定文件并通过命令行释放它们?
伪代码中的这样的东西:
for file in folder do
unlock file
Run Code Online (Sandbox Code Playgroud) 我在 Visual Studio 中有一个奇怪的问题:
如果在调试过程中抛出未处理的异常,我将无法构建我的 C# 应用程序:
Unable to copy file "C:\Projects\A\bin\A.dll" to "..\..\bin\A.dll".
The process cannot access the file '..\..\bin\A.dll' because it is being used
by another process.
Could not copy "C:\Projects\A\bin\A.dll" to "..\..\bin\A.dll".
Exceeded retry count of 10. Failed.
The file is locked by: "Microsoft Visual Studio 2019 (14904)"
Run Code Online (Sandbox Code Playgroud)
是否有永久解决方案来解决这个奇怪的错误?(我目前唯一的解决方法是重新启动 Visual Studio)
我尝试过但对我不起作用的方法:
在我的脚本的主要目的完成后,作为"清理",调用一个函数来递归查看每个文件夹并删除以预定的一组扩展名结尾的所有文件.
我在测试期间,发现一些文件扩展名在删除列表中的文件实际上会抛出一个错误:[Errno 1] Operation not permitted: '/location/of/locked/file.png.查看文件本身,它似乎是锁定(在Mac上).
REMOVE_FILETYPES = ('.png', '.jpg', '.jpeg', '.pdf')
def cleaner(currentPath):
if not os.path.isdir(currentPath):
if currentPath.endswith(REMOVE_FILETYPES) or os.path.basename(currentPath).startswith('.'):
try:
os.remove(currentPath)
print('REMOVED: \"{removed}\"'.format(removed = currentPath))
except BaseException as e:
print('ERROR: Could not remove: \"{failed}\"'.format(failed = str(e)))
finally:
return True
return False
if all([cleaner(os.path.join(currentPath, file)) for file in os.listdir(currentPath)]):
try:
os.rmdir(currentPath)
print('REMOVED: \"{removed}\"'.format(removed = currentPath))
except:
print('ERROR: Could not remove: \"{failed}\"'.format(failed = currentPath))
finally:
return True …Run Code Online (Sandbox Code Playgroud) 在目录中创建文件后,只要我创建文件的程序正在运行,目录就会被锁定.有没有办法释放锁?我需要稍后重命名该行目录,我总是得到IOException一句"访问路径"......"拒绝".
Directory.CreateDirectory(dstPath);
File.Copy(srcPath + "\\File1.txt", dstPath + "\\File1.txt"); // no lock yet
File.Create(dstPath + "\\" + "File2.txt"); // causes lock
Run Code Online (Sandbox Code Playgroud) 当我尝试在 Azure Devops 中使用发布管道进行部署时,我遇到了锁定 DLL 文件的问题。
错误的截图是:
输出为文本:
2021-03-31T17:43:18.2626492Z ##[error]Error Code: ERROR_FILE_IN_USE
More Information: Web Deploy cannot modify the file 'Microsoft.Data.SqlClient.SNI.x64.dll' on the destination because it is locked by an external process. In order to allow the publish operation to succeed, you may need to either restart your application to release the lock, or use the AppOffline rule handler for .Net applications on your next publish attempt. Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_FILE_IN_USE.
Error count: 1.
Run Code Online (Sandbox Code Playgroud)
该问题似乎是由错误处置的 .NET 资源引起的。(例如:非托管 SqlConnection)无论如何,我无法更改源代码。
这是我的发布管道。
这是我的问题: …
azure locked-files azure-devops azure-pipelines-release-pipeline
locked-files ×8
c# ×4
attachment ×1
azure ×1
azure-devops ×1
azure-pipelines-release-pipeline ×1
command-line ×1
debugging ×1
directory ×1
macos ×1
python ×1
python-3.x ×1
smtp ×1
windows ×1