You would need to ensure that the directory is created prior to creating the file.
var appDataPath = Server.MapPath("~/App_Data/");
if (!Directory.Exists(appDataPath)) {
Directory.Create(appDataPath);
}
var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".pdf");
using (var output = File.Create(filePath)) {
// Do stuff here
}
Marked as answer by kokoto on Feb 17, 2012 06:57 AM
Thanks for the respose, i tried your code and the follwoing error occured.
Compiler Error Message:CS0117: 'System.IO.Directory' does not contain a definition for 'Create'
if (!Directory.Exists(appDataPath)) {
Directory.Create(appDataPath); (error at this line)
}
Crisfervil,
from the first error; here is the
Stack Trace:
[DirectoryNotFoundException: Could not find a part of the path 'C:\User\Documents\Sites\MySite\page\form.pdf'.]
ASP._Page_Order_cshtml.Execute() in c:\User\Documents\Sites\MySite\page.cshtml:247
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
System.Web.WebPages.WebPage.ExecutePageHierarchy() +156
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext context) +249
kokoto
Member
42 Points
24 Posts
System.IO.DirectoryNotFoundException: Could not find a part of the path
Feb 15, 2012 06:59 AM|LINK
I have been getting this message. sometimes the code just works fine, sometimes the error occurs.
Could not find a part of the path 'C:\User\Documents\ Sites\MySite\page\form.pdf'.
here is the code that creates the file and the error line
@using System.IO; ..... @{ ..... try{ var output = new FileStream(Server.MapPath("form.pdf"),FileMode.Create); var writer = PdfWriter.GetInstance(doc, output); ...... catch (DocumentException dex) { throw (dex); } catch (IOException ioex) { throw (ioex); (the error is on this line) } finally { doc.Close(); }crisfervil
Member
30 Points
5 Posts
Re: System.IO.DirectoryNotFoundException: Could not find a part of the path
Feb 15, 2012 01:06 PM|LINK
Can you put the stack trace to know if the exception was thrown in the "new FileStream..." line or in the next one?
pranavkm
Participant
796 Points
106 Posts
Re: System.IO.DirectoryNotFoundException: Could not find a part of the path
Feb 15, 2012 06:58 PM|LINK
You would need to ensure that the directory is created prior to creating the file.
var appDataPath = Server.MapPath("~/App_Data/"); if (!Directory.Exists(appDataPath)) { Directory.Create(appDataPath); } var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".pdf"); using (var output = File.Create(filePath)) { // Do stuff here }kokoto
Member
42 Points
24 Posts
Re: System.IO.DirectoryNotFoundException: Could not find a part of the path
Feb 16, 2012 11:38 AM|LINK
Thanks for the respose, i tried your code and the follwoing error occured.
Compiler Error Message: CS0117: 'System.IO.Directory' does not contain a definition for 'Create'
if (!Directory.Exists(appDataPath)) {
Directory.Create(appDataPath); (error at this line)
}
Crisfervil,
from the first error; here is the
Stack Trace:
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: System.IO.DirectoryNotFoundException: Could not find a part of the path
Feb 16, 2012 12:33 PM|LINK
That should be CreateDirectory:
if (!Directory.Exists(appDataPath)) { Directory.CreateDirectory(appDataPath); }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
plotnick
Member
2 Points
1 Post
Re: System.IO.DirectoryNotFoundException: Could not find a part of the path
May 10, 2012 08:28 PM|LINK
You don't have to check, Directory.CreateDirectory() never throws an exception if the directory already exists.