简体   繁体   English

无法从我的程序写入另一台计算机上的XML文件

[英]Cannot write to XML file in another computer from my program

When I write to an XML file, an exception occurs: "Cannot access file because it used by another process". 当我写入XML文件时,发生异常:“无法访问文件,因为它被另一个进程使用”。 How can I fix that problem? 我该如何解决这个问题?

You can use things like "Process Explorer" (easy to find) on the machine in question to double-check which process is locking a file. 您可以在有问题的计算机上使用“ Process Explorer”(易于查找)之类的方法来仔细检查哪个进程正在锁定文件。 If you don't own the competing process, the best you can do is ask the operator to kindly close the file and/or app that is blocking you. 如果您拥有竞争程序,那么您可以做的最好的事情就是请操作员友好地关闭阻止您的文件和/或应用程序。

Assuming you do you manage the other process that is locking the file? 假设您管理锁定该文件中的其他过程? The most common cause of unexpected locks is files not being closed cleanly. 意外锁定的最常见原因是文件没有被完全关闭。 Check that you are religiously closing all file handles after use, ideally using using so that they are closed even in error conditions - for example: 请检查您是否虔诚地关闭所有文件使用后处理,最理想的是采用using ,让他们在错误情况下,即使关闭-例如:

using(Stream dest = File.Create(path)) {
    // write to dest
}

Most likely this means another program has this file locked. 这很可能意味着另一个程序已将此文件锁定。 Try saving in another location and make sure you're properly disposing objects used to write to the file when you're done writing. 尝试保存在其他位置,并确保在完成写入后正确放置用于写入文件的对象。 Also double-check you have proper permissions to write this folder (try creating a basic text file there) 还要仔细检查您是否具有写入此文件夹的适当权限(尝试在其中创建基本文本文件)

Keep in mind that your program may be running with different permissions than what you are logged in with. 请记住,您的程序可能以与登录时所使用的权限不同的权限运行。

The XML file that you are trying to write to will be currently open by any other process [file opened] and will be in a locked state. 您尝试写入的XML文件将由任何其他进程打开[打开的文件],并处于锁定状态。 You cannot modify a file that has been locked. 您无法修改已锁定的文件。

Close any file handles that are currently using the resource. 关闭当前正在使用资源的任何文件句柄。

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

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