Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I delete the files in a directory after I have converted them from XML to Txt using a Visual C# console application?
    text
    copied!<p>I am creating a Visual C# application to covert xml files to x12 edi txt files. I would like to delete all the files in the xml directory after I have created their txt equivalents. I have tried adding a foreach method with file.delete, but cannot get it to work. When I place it within the main method, I am told the file is in use. When I place it outside of the main method, it is not executed at all. Below is the code for my application with the file.delete at the end, outside of the main method and class.</p> <pre><code>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&gt;~"); 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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload