热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何删除其他进程使用的文件-Howtodeleteafileusedbyotherprocess

Heresthedeal.这是交易。MyWinAppisrunning,right?inletssayprocessA.Itcreatesafileand

Here's the deal.

这是交易。

My WinApp is running, right? in let's say process 'A'.It creates a file and keeps the handle (keeps the file open for writing, this is a must).
Then it starts other msbuild process, let's call it 'B'. This process is started with the System.Diagnostic.Process class.
At some point, my WinApp (A) needs to delete the previously created file (remember it was created by A itself), and that's when I get an IOException with the message "The process cannot access the file 'X' because it is being used by another process". And it actually is!... If I terminate process 'B', only then 'A' can successfully delete the file.

我的WinApp正在运行,对吗?让我们说进程'A'。它创建一个文件并保留句柄(保持文件打开以便写入,这是必须的)。然后它启动其他msbuild进程,让我们称之为'B'。此过程从System.Diagnostic.Process类开始。在某些时候,我的WinApp(A)需要删除以前创建的文件(记住它是由A本身创建的),当我得到一个IOException时,消息“进程无法访问文件'X',因为它正在由另一个进程使用“。它实际上是!...如果我终止进程'B',只有'A'才能成功删除该文件。

So my questions are:
1) Is there a way I can tell the process I create not no handle the files I opened?
2) Is there another way to achieve my scenario?

所以我的问题是:1)有没有办法可以告诉我创建的过程不是没有处理我打开的文件? 2)有没有其他方法来实现我的方案?

4 个解决方案

#1


It looks like System.Diagnostic.Process.Start calls CreateProcess with the bInheritHandles argument set to true.

看起来System.Diagnostic.Process.Start调用CreateProcess,并将bInheritHandles参数设置为true。

You could try setting UseShellExecute to true in ProcessStartInfo, or directly P/Invoke to CreateProcess.

您可以尝试在ProcessStartInfo中将UseShellExecute设置为true,或者直接将P / Invoke设置为CreateProcess。

#2


It is possible to force a file handle closed without closing the process is using the handle, though this might cause the application to crash. In other words, doing what you wannt to do may cause 'B' to crash. That said, what you are asking for is definitely possible, since the application, Process Explorer can do this. I you search the messageboard in that link, you might find it informative, though even the act of finding which application is using a handle is an exercise in frustration, never mind actually closing the handle.

可以强制关闭文件句柄而不关闭进程正在使用句柄,但这可能会导致应用程序崩溃。换句话说,做你想做的事可能会导致'B'崩溃。也就是说,你要求的绝对是可能的,因为Process Explorer可以做到这一点。我在那个链接中搜索留言板,你可能会发现它提供了丰富的信息,尽管即使是找到哪个应用程序正在使用句柄的行为也是一种挫败的练习,更不用说实际关闭句柄了。

#3


I don't know of a guaranteed way to delete a file with open handles, but if you can wait until a system restart for the file to be deleted, you can use the same technique as the MoveFile utility from Sysinternals.

我不知道删除具有打开句柄的文件的保证方法,但如果您可以等到系统重新启动以删除该文件,则可以使用与Sysinternals中的MoveFile实用程序相同的技术。

This program adds registry values to the HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations key which Windows checks on boot, and this will guarantee the file gets deleted, albeit not immediately.

此程序将注册表值添加到Windows检查引导的HKLM \ System \ CurrentControlSet \ Control \ Session Manager \ PendingFileRenameOperations项,这将保证文件被删除,尽管不是立即删除。

#4


I had come across a similar problem when I was trying to read a file that was exclusively locked.

当我试图读取一个完全锁定的文件时,我遇到了类似的问题。

I was trying to do it using :

我试图用它来做:

FileStream exclusiveWriter = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);  

The complete discussion is available at :

完整的讨论可在以下网站获得:

File exclusively locked by another process - MSDN Forum disucssion

文件由另一个进程独占锁定 - MSDN论坛的disucssion

Hope this helps.

希望这可以帮助。


推荐阅读
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • GreenDAO快速入门
    前言之前在自己做项目的时候,用到了GreenDAO数据库,其实对于数据库辅助工具库从OrmLite,到litePal再到GreenDAO,总是在不停的切换,但是没有真正去了解他们的 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • 我创建了一个新的AWSSSO(使用内部IDP作为身份源,因此不使用ActiveDirectory)。我能够登录AWSCLI、AWSGUI,但 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了Monkey错误分析日志定位相关的知识,希望对你有一定的参考价值。Monkey测试可以发现的问题 ... [详细]
  • monkey初接触
    第一次听说monkey,根本不知道是什么东西,脑海里就一个印象,很厉害的自动化测试工具,可是体验了一下,似乎不 ... [详细]
  • Iamworkingonaprojectwhichrequiresopentokandcallkitfornotifyingusers.However,theappli ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • Win10下游戏不能全屏的解决方法及兼容游戏列表
    本文介绍了Win10下游戏不能全屏的解决方法,包括修改注册表默认值和查看兼容游戏列表。同时提供了部分已经支持Win10的热门游戏列表,帮助玩家解决游戏不能全屏的问题。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
author-avatar
手机用户2602907295
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有