如何检查文件夹中存在的文件并替换新文件 [英] how to check file exists in folder and replace the new one
本文介绍了如何检查文件夹中存在的文件并替换新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下
excel旧文件必须删除,新文件必须自动替换。
My code as follows
that excel old file has to be deleted and new file has to be replace automatically.
private void Preseaintake()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Presea_Intakerpts '" + dtfromdate.Text.Trim() + "', '" + dttodate.Text.Trim() + "'";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adp.Fill(table);
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlsheet;
object misvalue = System.Reflection.Missing.Value;
xlWorkBook = (Microsoft.Office.Interop.Excel.Workbook)xlapp.Workbooks.Add(1);
xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.ActiveSheet;
for (int i = 0; i < table.Columns.Count; i++)
{
xlsheet.Cells[1, i + 1] = table.Columns[i].ColumnName;
}
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
xlsheet.Cells[i + 2, j + 1] = table.Rows[i][j].ToString().Trim();
}
}
xlWorkBook.SaveAs(Application.StartupPath + "\\Preseaintake\\newtest.xls", misvalue, misvalue, misvalue, misvalue, misvalue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue,
misvalue, misvalue, misvalue);
xlWorkBook.Close(true, misvalue, misvalue);
xlsheet = null;
xlapp = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
return;
}
}
private void sendmail()
{
try
{
MailMessage mis = new MailMessage();
SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
smtpserver.Credentials = new NetworkCredential("reply@gmail.com", "hdfdf");
smtpserver.Host = "smtp.gmail.com";
smtpserver.Port = 587;
smtpserver.EnableSsl = true;
mis.From = new MailAddress("reply@gmail.com", "PreseaIntakeDetails");
mis.IsBodyHtml = true;
mis.To.Add("gdf@gmail.com");
mis.Subject = "PreseaIntake Report";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(Application.StartupPath + "\\Preseaintake\\newtest.xls");
mis.Attachments.Add(attachment);
smtpserver.Send(mis);
mis.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
return;
}
}
i想删除旧文件并替换为新的。
我该怎么办?
在我上面的代码我有什么变化制作。
请帮帮我。
问候,
narasiman P.
i want to delete the old file and replace with new one.
for that how can i do?
in my above code what changes i have to made.
please help me.
regards,
narasiman P.
推荐答案
File.Exists [ ^ ]和 File.Delete [ ^ ]
在ASP.NET论坛上发帖 [ ^ ]
Stack Overflow上的答案 [ ^ ]
File.Exists[^] and File.Delete[^]
Answer at ASP.NET forums[^]
Answer at Stack Overflow[^]
DirectoryInfo dir_file = new DirectoryInfo(dirpath);
foreach(文件信息fi在dir_file.GetFiles()中)
{
string filepath =;
filepath = dir_file +\\+ fi;
if(!File.Exists(filepath))
{
System.IO.FileInfo fi_del = new System.IO.FileInfo(filepath);
try
{
fi_del.Delete();
}
catch(System.IO.IOException e)
{
//控制台。 WriteLine(e.Message);
MessageBox.Show(e.Message);
}
}
}
可能这将有助于你
DirectoryInfo dir_file = new DirectoryInfo(dirpath);
foreach (FileInfo fi in dir_file.GetFiles())
{
string filepath = "";
filepath = dir_file + "\\" + fi;
if (!File.Exists(filepath))
{
System.IO.FileInfo fi_del = new System.IO.FileInfo(filepath);
try
{
fi_del.Delete();
}
catch (System.IO.IOException e)
{
//Console.WriteLine(e.Message);
MessageBox.Show(e.Message);
}
}
}
may b this will help u
我认为在您现有的代码中请做出这种类型的修改...
< br $>
I think in your existing code please make this type of modification...
string filePath = Application.StartupPath + "\\Preseaintake\\newtest.xls";
if(System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath)
xlWorkBook.SaveAs(filePath, misvalue, misvalue, misvalue, misvalue, misvalue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue,
misvalue, misvalue, misvalue);
xlWorkBook.Close(true, misvalue, misvalue);
xlsheet = null;
xlapp = null;
这篇关于如何检查文件夹中存在的文件并替换新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文