簡體   English   中英

處理xml文件並移動到另一個文件夾

[英]Process xml files and move to another folder

處理完每個xml文件后,我需要將其移動到另一個文件夾。 要執行此操作的代碼是什么以及將其放置在何處,請確保如果存在目標中具有相同名稱的文件,則此文件將被覆蓋

                string Path1 = @"D:\dataIn";
                string Path2 = @"D:\dataOut";

                con.Open();
                label1.Text = "";

                foreach (string file in Directory.EnumerateFiles(Path1, "*.xml"))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(file);
                    DataTable dt1 = ds.Tables["Order"];
                    DataTable dt2 = ds.Tables["Details"];
                    using (SqlBulkCopy bc = new SqlBulkCopy(con))
                    {
                        bc.ColumnMappings.Add("account", "account");
                        bc.ColumnMappings.Add("date", "date");
                        bc.ColumnMappings.Add("value", "value");
                        bc.DestinationTableName = "header";
                        bc.WriteToServer(dt1);
                    }
                    using (SqlBulkCopy bc = new SqlBulkCopy(con))
                    {
                        bc.ColumnMappings.Add("itemID", "itemID");
                        bc.ColumnMappings.Add("qty", "qty");
                        bc.ColumnMappings.Add("price", "price");
                        bc.DestinationTableName = "items";
                        bc.WriteToServer(dt2);
                    }                    
                }

您可能正在尋找File.Move

不要忘記IO名稱空間:

using System.IO;

然后是移動文件的代碼,您將把它放在for循環的末尾:

if (File.Exists(newFile)) // Did we find the file?
{
    File.Delete(newFile); // Yep, so delete it.
}
File.Move(file, newFile); // Move the file. 

您也可以使用File.Copy ,但是如果要“移動”它,則必須刪除該文件。

這是您要對foreach循環執行的操作:

            foreach (string file in Directory.EnumerateFiles(Path1, "*.xml"))
            {
                string newFile = path2 + @"\" + Path.GetFileName(file);
                DataSet ds = new DataSet();
                ds.ReadXml(file);
                DataTable dt1 = ds.Tables["Order"];
                DataTable dt2 = ds.Tables["Details"];
                using (SqlBulkCopy bc = new SqlBulkCopy(con))
                {
                    bc.ColumnMappings.Add("account", "account");
                    bc.ColumnMappings.Add("date", "date");
                    bc.ColumnMappings.Add("value", "value");
                    bc.DestinationTableName = "header";
                    bc.WriteToServer(dt1);
                }
                using (SqlBulkCopy bc = new SqlBulkCopy(con))
                {
                    bc.ColumnMappings.Add("itemID", "itemID");
                    bc.ColumnMappings.Add("qty", "qty");
                    bc.ColumnMappings.Add("price", "price");
                    bc.DestinationTableName = "items";
                    bc.WriteToServer(dt2);
                }

                if (File.Exists(newFile)) // Did we find the file?
                {
                    File.Delete(file); // Yep, so delete it.
                }
                File.Move(file, newFile); 
           }

File.Copy將復制文件,並覆蓋該名稱的任何現有文件。 這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM