简体   繁体   English

System.IO.IOException错误,无法访问该文件

[英]System.IO.IOException error, can't get access to the file

I can't understand where problem is, despite the fact, that this code pretty easy. 我无法理解问题在哪里,尽管事实上,这个代码很容易。

I have such function: 我有这样的功能:

public void WriteToDoc(string path)    
{
     XDocument doc = new XDocument(new XElement("General parameters",
                                   new XElement("num_path", num_path.Text),
                                   new XElement("Gen_Peroid", Gen_Peroid.Text),
                                   new XElement("Alg_Perioad", Alg_Perioad.Text))
                                  );
     doc.Save(path); // here he gives that exception
}

num_path.Text , Gen_Peroid.Text and Alg_Perioad.Text are string . num_path.TextGen_Peroid.TextAlg_Perioad.Textstring

This is how I use this function: 这就是我使用这个功能的方法:

File.Create(@"C:\ProgramData\RadiolocationQ\Q.xml");
WriteToDoc(@"C:\ProgramData\RadiolocationQ\Q.xml");

It creates file, but nothing was written in that file. 它创建文件,但该文件中没有写入任何内容。 So the exact error System.IO.IOException error, The process cannot access the file because it is being used by another process. 所以确切的错误System.IO.IOException错误,进程无法访问该文件,因为它正被另一个进程使用。 How is it possible to get such error? 怎么可能得到这样的错误?

Try 尝试

System.IO.File.Create(@"C:\ProgramData\RadiolocationQ\Q.xml").Close();

Edit: Reinhards answer is better. 编辑:Reinhards答案更好。 My is just closing the File.Create() stream, which locks the File, but there is no need for. 我只是关闭File.Create()流,它锁定文件,但没有必要。

You are the process that has it open! 你是打开它的过程!

Don't call File.Create first - it leaves the file stream open, and you can't write over the file. 不要先调用File.Create - 它会使文件流保持打开状态,并且您无法覆盖该文件。

XDocument.Save will create the file - you don't have to: XDocument.Save将创建该文件 - 您不必:

Serialize this XDocument to a file, overwriting an existing file, if it exists. 将此XDocument序列化为文件,覆盖现有文件(如果存在)。

XDocument.Save will create a file if one doesnt exist. 如果一个文件不存在, XDocument.Save将创建一个文件。 There is no need for File.Create() File.Create()不需要

File.Create() is not closing, It is locking the file on you. File.Create()没有关闭,它正在锁定你的文件。

As the other answers state, you are not closing your output file. 正如其他答案所述,您没有关闭输出文件。 The proper way of saving XML files using LinqToXML is: 使用LinqToXML保存XML文件的正确方法是:

System.Xml.XmlWriterSettings xws = new System.Xml.XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = "\t";

FileStream fsConfig = new FileStream(path, FileMode.Create);
using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(fsConfig, xws))
{
       doc.Save(xw);
}
fsConfig.Close();

This releases the file & stream. 这将释放文件和流。 You can omit the XmlWriterSettings if not needed. 如果不需要,可以省略XmlWriterSettings

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

相关问题 获取错误 System.IO.IOException: '进程无法访问文件' - Getting error System.IO.IOException: 'The process cannot access the file' System.IO.IOException:'进程无法访问文件' - System.IO.IOException: 'Process can not access the file' ELMA BPM 服务重启时出现错误“System.IO.IOException: The process can not access the file...” - Error "System.IO.IOException: The process can not access the file..." at ELMA BPM service restart System.IO.IOException错误 - System.IO.IOException error System.IO.IOException:进程无法访问由System.IO .__ Error.WinIOError使用的文件 - System.IO.IOException: The process cannot access the file being used by System.IO.__Error.WinIOError System.IO.IOException:进程无法访问该文件 - System.IO.IOException: The process cannot access the file LiteDB 5 System.IO.IOException:进程无法访问文件 - LiteDB 5 System.IO.IOException: The process cannot access the file 我该如何修复'System.IO.IOException:'进程无法访问文件' - How can i fix the 'System.IO.IOException: 'The process cannot access the file' 错误:帮助D:System.IO.IOException:该进程无法访问文件,因为它正在被另一个进程使用 - Error: Help D: System.IO.IOException: The process cannot access the file because it is being used by another process System.IO.IOException - System.IO.IOException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM