2015-09-09 20 views
-2

我有一個C#應用程序(WCF soap服務),它正在創建PDF文檔並保存在web.config中定義的路徑中。考慮例如路徑爲:C:\Doc\Pdf\即在代碼路徑中位置變量。我喜歡爲每天生成文件夾,並將當天的pdf存儲在其文件夾中。如何在C#中指定生成的文件夾(目錄)名稱

我試圖使用CreateDirectory但我不知道如何指定要生成的文件夾的名稱。

此代碼僅保存PDF在C:\Doc\Pdf\,它也沒有創造任何目錄:

string pdfFileName = "Application" + "_" + documentData.APPLICATIONDATE.ToString("MMddyyyy") + "_" + documentData.APPLICATIONDATE.ToString("hhmmsstt") + "_" + documentData.BorrowerLastName; 

location = ConfigurationManager.AppSettings["PdfPath"].ToString(); 

DirectoryInfo di =System.IO.Directory.CreateDirectory(location); 

wordDoc.SaveAs(location + pdfFileName, WdSaveFormat.wdFormatPDF); 
+0

你做的文件名幾乎一樣。什麼阻止你像'DateTime.Now.ToString(「yyyyMMdd」)''添加到'location'? – CodeCaster

+0

@CodeCaster,但在第一步DirectoryInfo di = System.IO.Directory.CreateDirectory(location);根本不創建任何目錄。 – Alma

+0

然後該目錄已經存在,否則會引發異常。 – CodeCaster

回答

2

在這方面,我認爲,你可以簡單地使用Directory.CreateDirectory方法傳遞你期望得到的合併路徑創建。

稱爲CreateDirectory目錄方法創建工作的所有目錄中指定的路徑,如果路徑已經存在不拋出異常,它根本什麼都不缺

所以,你的代碼coulde是

string dayPath = DateTime.Today.ToString("yyyyMMdd"); 
string newPath = Path.Combine(location, dayPath); 
Directory.CreateDirectory(newPath); 
........ 
相關問題