简体   繁体   English

创建另一个文件后未创建文件

[英]File is not created after another has been created

I am trying to create 2 XML files in the same folder. 我正在尝试在同一文件夹中创建2个XML文件。 For some reason it does create the first one, but does not create the second one. 由于某种原因,它会创建第一个,但不会创建第二个。

Could it be that the first one is still being created when an attempt to create the second file is made, and therefore the latter fails? 尝试创建第二个文件时是否可能仍在创建第一个文件,因此后者失败了?

I don't get any errors with the code: 我的代码没有任何错误:

if (File.Exists(FileNameTextBox.Text + ".AA.xml"))
{
    MessageBox.Show("Already exists. renaming to *.old" + Environment.NewLine +
                    "if there is already an *.old file, this will be deleted.");
    if (File.Exists(FileNameTextBox.Text + ".AA.xml.old"))
    {
        File.Delete(FileNameTextBox.Text + ".AA.xml.old");
    }
    File.Move(FileNameTextBox.Text + ".AA.xml", FileNameTextBox.Text + ".AA.xml.old");
}
if (!File.Exists(FileNameTextBox.Text + ".AA.xml"))
{
    XmlTextWriter textWritter = new XmlTextWriter(FileNameTextBox.Text + ".AA.xml", null);
    textWritter.WriteStartDocument();
    textWritter.WriteStartElement("Data");
    textWritter.WriteEndElement();
    textWritter.Close();
}

if (File.Exists("BB.xml"))
{
    if (File.Exists("BB.xml.old"))
    {
        File.Delete("BB.xml.old");
    }
    File.Move("BB.xml", "BB.xml.old");
}
if (!File.Exists("BB.xml"))
{
    XmlTextWriter textWritterPC3 = new XmlTextWriter("BB.xml", null);
    textWritterPC3.WriteStartDocument();
    textWritterPC3.WriteStartElement("Data");
    textWritterPC3.WriteEndElement();
    textWritterPC3.Close();
}

Whats in FileNameTextBox.Text ? FileNameTextBox.Text什么? Does it specify a directory path? 是否指定目录路径?

Your second file is created without saying which directory. 创建第二个文件时,无需说出哪个目录。 So it will be created in the current directory - which is not necessarily the directory specified by FileNameTextBox.Text 因此它将在当前目录中创建-不一定是FileNameTextBox.Text指定的目录

You are not specifying an absolute path for your file names, so you are using whatever the current directory happens to be, which is not reliable. 您没有为文件名指定绝对路径,因此使用的是当前目录,这是不可靠的。 Also you may need to call DirectoryInfo.Refresh() or FileInfo.Refresh() to make sure you are seeing the latest directory information (whether the file exists or not). 另外,您可能需要调用DirectoryInfo.Refresh()FileInfo.Refresh()以确保您看到的是最新目录信息(文件是否存在)。

The comment is only making the last if case execute the first row.. The last if should look like this. 注释仅使最后一个if语句执行第一行。最后一个if语句应如下所示。 I don't know if it's only in your example. 我不知道是否仅在您的示例中。 Your example 你的例子

if (!File.Exists("BB.xml")) //            {
            XmlTextWriter textWritterPC3 = new XmlTextWriter("BB.xml", null);

should be 应该

if (!File.Exists("BB.xml")) //            
{
    XmlTextWriter textWritterPC3 = new XmlTextWriter("BB.xml", null);
    textWritterPC3.WriteStartDocument();
    textWritterPC3.WriteStartElement("Data");

    textWritterPC3.WriteEndElement();
    textWritterPC3.Close();
}

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

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