使用Visual C#控制台应用程序将文件从XML转换为Txt后,如何删除目录中的文件

本文关键字:文件 Txt 删除目录 转换 控制台 Visual 应用程序 XML 使用 | 更新日期: 2023-09-27 18:19:38

我正在创建一个Visual C#应用程序,将xml文件转换为x12 edi txt文件。我想在创建了相应的txt文件后,删除xml目录中的所有文件。我尝试过用file.delete添加foreach方法,但无法使其工作。当我把它放在main方法中时,我被告知该文件正在使用中。当我把它放在主方法之外时,它根本不会被执行。下面是我的应用程序的代码,在主方法和类之外的末尾有file.delete。

namespace XMLParse
{
     class Class1
    {
    public static void Main()
    {
        string[] Files = Directory.GetFiles(@"C:'onlinesales");
        foreach (string filename in Files)
        {
            StringBuilder orderid = new StringBuilder();
            StringBuilder ordernumber = new StringBuilder();
            StringBuilder name = new StringBuilder();
            StringBuilder staddress = new StringBuilder();
            StringBuilder city = new StringBuilder();
            StringBuilder state = new StringBuilder();
            StringBuilder zip = new StringBuilder();
            StringBuilder country = new StringBuilder();
            StringBuilder email = new StringBuilder();
            StringBuilder partnumber = new StringBuilder();
            StringBuilder quantity = new StringBuilder();

            using (XmlReader reader = XmlReader.Create(new StreamReader(filename)))
            {
                reader.ReadToFollowing("OrderID");
                orderid.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("OrderNumber");
                ordernumber.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("Name");
                name.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("Line1");
                staddress.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("City");
                city.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("StateProvinceCode");
                state.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("PostalCode");
                zip.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("CountryCode");
                country.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("Email");
                email.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("ProductSKU");
                partnumber.Append(reader.ReadElementContentAsString());
                reader.ReadToFollowing("Quantity");
                quantity.Append(reader.ReadElementContentAsString());
            }
            using (StreamWriter fileout =
                new StreamWriter("W:" + DateTime.Now.ToString("yyyyy-MM-dd_hh-mm-ss-ff") + ".txt", false, Encoding.ASCII))
            {
                fileout.WriteLine("ISA*00*          *00*          *ZZ*daisywebstore  *12*5016361200     *" + DateTime.Now.ToString("yyMMdd") + "*1559*U*00400*000001649*0*P>~");
                fileout.WriteLine("GS*PO*daisywebstore*5016361200*" + DateTime.Now.ToString("yyyyMMdd") + "*" + DateTime.Now.ToString("HHmm") + "*1649*X*004010~");
                fileout.WriteLine("ST*850*13~");
                fileout.WriteLine("BEG*00*SA*08272226001*" + DateTime.Now.ToString("yyyyMMdd") + "~");
                fileout.WriteLine("REF*DP*089~");
                fileout.WriteLine("DTM*002*20120104~");
                fileout.WriteLine("N1*ST*" + name + "~");
                fileout.WriteLine("N3*" + staddress + "~");
                fileout.WriteLine("N4*" + city + "*" + state + "*" + zip + "~");
                fileout.WriteLine("N1*RE**92*00103653341~");
                fileout.WriteLine("PO1*1*6*EA*33.28*TE*IN*985880-542~");
                fileout.WriteLine("PID*F*****CO2 BB PISTOL     $ 5693~");
                fileout.WriteLine("PO4*3*1*EA~");
                fileout.WriteLine("CT*1~");
                fileout.WriteLine("AMT*1*199.68~");
                fileout.WriteLine("SE*16*13~");
            }
        }
    }
 }
}
 public class Delete
{
 private static void Empty()
   {
    string[] files = Directory.GetFiles(@"C:'onlinesales");
    foreach (string filename in files)
        File.Delete(filename);
   }
  }

使用Visual C#控制台应用程序将文件从XML转换为Txt后,如何删除目录中的文件

您正在关闭XMLReaderusing,但StreamReader仍然打开,这导致了此错误

试试这个

foreach (string filename in Files)
{
    StreamReader read = new StreamReader(filename);
            using (XmlReader reader = XmlReader.Create(read))
            {
            }
    read.Close();
}

 foreach (string filename in Files)
 {
      using (StreamReader read = new StreamReader(filename))
            {
                using (XmlReader reader = XmlReader.Create(read))
                {
                }
            }
 }

请尝试StreamReader构造函数的重载:

new StreamReader(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Delete))

也将new streameader(filename)封装在using语句中。假设文件被保存到另一个目录;只需删除目录

Directory.Delete(@"C:'onlinesales", true);
namespace XMLParse
{
    class Class1
    {
        public static void Main()
        {
            string[] Files = Directory.GetFiles(@"C:'onlinesales");
            foreach (string filename in Files)
            {
                //...YOUR CODE
            }
            //....ADD THIS LINE
            Array.ForEach(Directory.GetFiles(@"c:'onlinesales'", "*.xml"),
              delegate(string path) { File.Delete(path); });
        }
    }
}