Storing file by creating datefolders and saving inside it

与世无争的帅哥 提交于 2019-12-13 09:23:50

问题


I want to store excel file Inside date folder which I will create dynamically. So I wrote the code Like below

public void ExportExcel(string strWorkbookName, DataSet ds)
    {
        string strFilePath = "";
        string strDateFolder = "";
        try
        {
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(ds);
                //wb.SaveAs(ConfigurationRead.GetAppSetting("ReportDirectory") + "Report.xlsx");
                strDateFolder = Directory.CreateDirectory(DateTime.Now.ToString("dd-MM-yyyy"));
                strFilePath = ConfigurationRead.GetAppSetting("ReportDirectory") + "\\" + strDateFolder + "\\" + "Report.xlsx";
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

But even the folder is not created and I get error as

cannot implicitly convert type system io directoryinfo to string

at line:-

strDateFolder = Directory.CreateDirectory(DateTime.Now.ToString("dd-MM-yyyy"));


回答1:


I think you need something like that:

string reportDirectory = ConfigurationRead.GetAppSetting("ReportDirectory");
strDateFolder = Directory.CreateDirectory(
    Path.Combine(reportDirectory, DateTime.Now.ToString("dd-MM-yyyy"))).Name;
strFilePath = Path.Combine(reportDirectory, strDateFolder, "Report.xlsx");

Hope this works. Could not fully check it.



来源:https://stackoverflow.com/questions/49187909/storing-file-by-creating-datefolders-and-saving-inside-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!