C# 从文件夹下载最新的excel文件
我正在根据SYSDATEcurrent日期将excel文件存储在一个文件夹中。现在我不想下载文件夹中的所有文件 但是我想从基于时间的文件夹中下载最新的文件。下面是我的代码的样子。目前,我正在从zip文件夹下载所有excel文件C# 从文件夹下载最新的excel文件,c#,asp.net,excel,download,C#,Asp.net,Excel,Download,我正在根据SYSDATEcurrent日期将excel文件存储在一个文件夹中。现在我不想下载文件夹中的所有文件 但是我想从基于时间的文件夹中下载最新的文件。下面是我的代码的样子。目前,我正在从zip文件夹下载所有excel文件 else if (strSelectedReportType == "RCOMReports") { string strReportFile = ConfigurationManager.AppSe
else if (strSelectedReportType == "RCOMReports")
{
string strReportFile = ConfigurationManager.AppSettings["RCOMReports"].ToString();
string strFilePath = ConfigurationManager.AppSettings["ReportDirectory"].ToString() + "\\" + DateTime.Now.ToString("dd-MM-yyyy");
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
if (Directory.Exists(strFilePath))
{
DirectoryInfo di = new DirectoryInfo(strFilePath);
FileInfo[] FileInfo = di.GetFiles();
if (FileInfo.Length > 0)
{
foreach (FileInfo item in FileInfo)
{
zip.AddFile(item.FullName, "Files");
}
}
MemoryStream ms = new MemoryStream();
Stream Objstream = new MemoryStream(ms.ToArray());
Objstream.Seek(0, SeekOrigin.Begin);
zip.AddEntry(strReportFile, Objstream);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", strReportFile);
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(HttpContext.Current.Response.OutputStream);
HttpContext.Current.Response.End();
}
else
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "alert('No Reports as on today's date..!!');", true);
}
}
}
现在我不想Zip也找到最后一个文件并下载它
var directory = new DirectoryInfo(ConfigurationManager.AppSettings["ReportDirectory"]);
// the latest excel file
var file = directory.GetFiles().OrderByDescending(c => c.LastWriteTime).FirstOrDefault();
if (file == null)
{
return;
}
var content = File.ReadAllBytes(file.FullName);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + file.Name + ".xlsx");
HttpContext.Current.Response.BufferOutput = true;
HttpContext.Current.Response.OutputStream.Write(content, 0, content.Length);
HttpContext.Current.Response.End();
@谢谢nikhil,我把我的文件拿到这里了。现在我该如何让它下载@nikhilmehta:我在FileInfo file=newfileinfomyfile.Name处有文件名;如果file.Exists存在,它仍然给我提供false