如何在创建文件之前检查文件是否存在

时间:2022-02-04 23:49:28

I am creating an xml file. I need to check first if the file exists or not. If the file does not exist, create it and add the data cmg from a .cs file.

我正在创建一个xml文件。我需要先检查文件是否存在。如果该文件不存在,则创建它并从.cs文件中添加数据cmg。

If the file exists, don't create the file just add the data cmg from a .cs file.

如果文件存在,不要创建文件,只需从.cs文件中添加数据cmg。

My code looks like this:

我的代码是这样的:

string filename="c:\\employee.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents 
the Encoding Type//
tw.Formatting=Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument(); //Indicates the starting of document (Required)//
tw.WriteStartElement("Employees"); 
tw.WriteStartElement("Employee","Genius");
tw.WriteStartElement("EmpID","1");
tw.WriteAttributeString("Name","krishnan");
tw.WriteElementString("Designation","Software Developer");
tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument(); 
tw.Flush();
tw.Close();
  • so next time we add data to file we need to check if the file exits and add data to xml file
  • 因此,下次我们向文件添加数据时,我们需要检查文件是否存在,并将数据添加到xml文件中。
  • and as we have made empID as a primary key, if user tries to make duplicate entry we need to avoid
  • 我们已经将empID作为主键,如果用户试图复制我们需要避免的重复条目。

Is this possible to do?

这可能吗?

4 个解决方案

#1


6  

if (!File.Exists(filename))
{
    // create your file
}

or

if (File.Exists(filename))
{
    File.Delete(filename);
}

// then create your file

File class is in System.IO namespace (add using System.IO; to your file)

文件类在系统中。IO命名空间(使用System.IO添加;你的文件)

#2


1  

You can't append records to an XML file, you have to read the file and then rewrite it.

您不能将记录追加到XML文件中,您必须读取该文件,然后重写它。

So, just check if the file exists, and read the records from it. Then write the file including all previous records and the new record.

所以,检查文件是否存在,并从中读取记录。然后写入文件,包括所有以前的记录和新的记录。

#3


0  

Have a look at the File.Exists method here

看一下这个文件。存在的方法

#4


0  

Testing for existance of a file before attempting to create it inherently is subject to a "things change after check" race condition. Who can guarantee you that your application isn't preempted and put to sleep for a moment after you checked, someone else creates/deletes that file, your app gets to run again and does exactly the opposite of what you intended ?

在尝试创建文件之前,测试文件的存在性会受到“检查后更改”竞争条件的影响。谁能保证你的应用程序不会被抢占并在你检查后休眠片刻,其他人创建/删除那个文件,你的应用程序会再次运行,并与你想要的完全相反?

Windows (as well as all UN*X variants) supports file open/create modes that allow to perform that create-if-nonexistant/open-if-existant operation as a single call.

Windows(以及所有UN*X变体)支持文件打开/创建模式,允许以单个调用的方式执行创建-if-non -存在/打开-存在操作。

As far as .NET goes, this means for your task (create an XML file) you'd first create a System.IO.FileStream with the appropriate modes, see http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx and then pass that stream to the XmlWriter constructor. That's safer than simply performing an "exists" check and hoping for the best.

至于。net,这意味着您的任务(创建一个XML文件)首先要创建System.IO。使用适当模式的FileStream,请参见http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx,然后将该流传递给XmlWriter构造函数。这比简单地执行“存在”检查并期待最好的结果更安全。

#1


6  

if (!File.Exists(filename))
{
    // create your file
}

or

if (File.Exists(filename))
{
    File.Delete(filename);
}

// then create your file

File class is in System.IO namespace (add using System.IO; to your file)

文件类在系统中。IO命名空间(使用System.IO添加;你的文件)

#2


1  

You can't append records to an XML file, you have to read the file and then rewrite it.

您不能将记录追加到XML文件中,您必须读取该文件,然后重写它。

So, just check if the file exists, and read the records from it. Then write the file including all previous records and the new record.

所以,检查文件是否存在,并从中读取记录。然后写入文件,包括所有以前的记录和新的记录。

#3


0  

Have a look at the File.Exists method here

看一下这个文件。存在的方法

#4


0  

Testing for existance of a file before attempting to create it inherently is subject to a "things change after check" race condition. Who can guarantee you that your application isn't preempted and put to sleep for a moment after you checked, someone else creates/deletes that file, your app gets to run again and does exactly the opposite of what you intended ?

在尝试创建文件之前,测试文件的存在性会受到“检查后更改”竞争条件的影响。谁能保证你的应用程序不会被抢占并在你检查后休眠片刻,其他人创建/删除那个文件,你的应用程序会再次运行,并与你想要的完全相反?

Windows (as well as all UN*X variants) supports file open/create modes that allow to perform that create-if-nonexistant/open-if-existant operation as a single call.

Windows(以及所有UN*X变体)支持文件打开/创建模式,允许以单个调用的方式执行创建-if-non -存在/打开-存在操作。

As far as .NET goes, this means for your task (create an XML file) you'd first create a System.IO.FileStream with the appropriate modes, see http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx and then pass that stream to the XmlWriter constructor. That's safer than simply performing an "exists" check and hoping for the best.

至于。net,这意味着您的任务(创建一个XML文件)首先要创建System.IO。使用适当模式的FileStream,请参见http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx,然后将该流传递给XmlWriter构造函数。这比简单地执行“存在”检查并期待最好的结果更安全。