简体   繁体   English

如何在Windows中以编程方式关闭打开的文件/应用程序?

[英]How to programmatically close an open file/application in windows?

I am writing data to a csv file as result of operation. 作为操作的结果,我正在将数据写入csv文件。 Than I open the file in excel to examine the data. 比我在excel中打开文件检查数据。 Often time I forget to close the file in excel and the next operation can't write to the same file. 通常,我常常忘记在excel中关闭文件,而下一个操作无法写入同一文件。 It looks very difficult if not impossible but is there a way I can programmatically close the file (=application that is using the file) before I write to it? 这看起来非常困难,即使不是不可能,但是有没有办法在我写入文件之前以编程方式关闭该文件(=正在使用该文件的应用程序)?

I guess the first thing will be to determine which application is using the file and then shutdown that app? 我想第一件事是确定哪个应用程序正在使用该文件,然后关闭该应用程序? Maybe someone has written a class for this or something? 也许有人为此写过一堂课?

Okay, per the comments: 好的,根据评论:

Flag the file as ReadOnly in the first operation. 在第一个操作中将文件标记为ReadOnly。 Then you can launch Excel and prevent it from getting an opportunistic lock on the file. 然后,您可以启动Excel并防止它获得文件上的机会锁定。 You can still update the file in subsequent operations by either removing the RO attribute, or using the -Force switch when you write the updates. 您仍然可以在后续操作中通过删除RO属性或在编写更新时使用-Force开关来更新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM