我想覆盖并将新值存储在CSV文件中

时间:2019-07-02 07:15:47

标签: c#

我正在根据自己的要求修改CSV文件中的数据,并且能够成功对其进行修改。但是以前的数据不会被删除。我怎样才能做到这一点?

 List csvfiles = new List();
        private void btnimport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.CheckFileExists = true;
            openFileDialog.AddExtension = true;
            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "CSV files (.csv)|.csv";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string fileName in openFileDialog.FileNames)
                {
                    csvfiles.Add(fileName);
                }
            }
        }

public void csvedit() { string path = @"C:\Users\Sunil\Videos\original\GSK.csv"; string csvContent = System.IO.File.ReadAllText(path); Regex r = new Regex(@"("",""(?=[0-9]{2}\/[0-9]{2}\/[0-9]{4}))"); var newStr = r.Replace(csvContent, "\n").Trim("\"".ToArray()); newStr.Trim("\"".ToArray()); System.IO.File.WriteAllText(path, newStr); } private void btnexport_Click(object sender, EventArgs e) { csvedit(); string installedPath = "C:\\Users\\Sunil\\Videos\\changed"; //Check whether folder path is exist if (!System.IO.Directory.Exists(installedPath)) { // If not create new folder System.IO.Directory.CreateDirectory(installedPath); } //Save pdf files in installedPath ?? foreach (string sourceFileName in csvfiles) { string destinationFileName = System.IO.Path.Combine(installedPath, System.IO.Path.GetFileName(sourceFileName)); System.IO.File.Copy(sourceFileName, destinationFileName); MessageBox.Show("File Exported Successfully"); } }

0 个答案:

没有答案