468,761 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,761 developers. It's quick & easy.

creating a directory during runtime

hi!

i am able to upload files successfully. but i have one minor problem. files
are uploaded correctly if the directory exists, it is throwing an exception
if the directory doesnt exist. but the directory name is created during
runtime. is there a way to create a directory during runtime?

exception:
Error saving file C:\temp\188\
System.IO.DirectoryNotFoundException: Could not find a part of the path
"C:\temp\188\_ds27.tmp". at System.IO.__Error.WinIOError(Int32 errorCode,
String str) at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync,
String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String
path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at
mole.proposals.dltaskorders.addTaskOrder.btnUpload _Click(Object sender,
EventArgs e) in c:\inetpub\wwwroot\dltaskorders\addtaskorder.aspx. cs:line 286

code:
private void btnUpload_Click(object sender, System.EventArgs e)
{
string baseLocation = "C:\\temp\\" + Session["sReference"].ToString()
+ "\\";
string status = "";
if((lstAttachments.Items.Count == 0) && (filesUploaded == 0))
{
lblError.Text = "Error - a file name must be specified.";
return;

}
else
{
foreach(System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
{
try
{
string fn =
System.IO.Path.GetFileName(HIF.PostedFile.FileName );
HIF.PostedFile.SaveAs(baseLocation + fn);
filesUploaded++;
status += fn + "<br>";
}
catch(Exception err)
{
lblError.Text = "Error saving file " + baseLocation
+ "<br>" + err.ToString();
}
}

if(filesUploaded == hif.Count)
{
lblError.Text = "These " + filesUploaded + " file(s) were "
+ "uploaded:<br>" + status;
}
hif.Clear();
lstAttachments.Items.Clear();
}
}
Nov 17 '05 #1
3 2814
Hi!!!

I had a similar problem and i solve it using the following lines:
string folder = Path.GetDirectoryName(path);
if(!Directory.Exists(folder))
{
try
{
Directory.CreateDirectory(folder);
}
catch(Exception ex)
{
EventLog.WriteEntry("LogService", ex.Message,
EventLogEntryType.Error);
}
}

I hope it solves your problem.

Nov 17 '05 #2
Newbie,

You should be able to get the directory in the filename using the static
GetDirectoryName method on the Path class. Then, you can pass that to the
static CreateDirectory method on the Directory class to create the directory
before you save the file.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Newbie" <Ne****@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
hi!

i am able to upload files successfully. but i have one minor problem.
files
are uploaded correctly if the directory exists, it is throwing an
exception
if the directory doesnt exist. but the directory name is created during
runtime. is there a way to create a directory during runtime?

exception:
Error saving file C:\temp\188\
System.IO.DirectoryNotFoundException: Could not find a part of the path
"C:\temp\188\_ds27.tmp". at System.IO.__Error.WinIOError(Int32 errorCode,
String str) at System.IO.FileStream..ctor(String path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync,
String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String
path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename)
at
mole.proposals.dltaskorders.addTaskOrder.btnUpload _Click(Object sender,
EventArgs e) in c:\inetpub\wwwroot\dltaskorders\addtaskorder.aspx. cs:line
286

code:
private void btnUpload_Click(object sender, System.EventArgs e)
{
string baseLocation = "C:\\temp\\" + Session["sReference"].ToString()
+ "\\";
string status = "";
if((lstAttachments.Items.Count == 0) && (filesUploaded == 0))
{
lblError.Text = "Error - a file name must be specified.";
return;

}
else
{
foreach(System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
{
try
{
string fn =
System.IO.Path.GetFileName(HIF.PostedFile.FileName );
HIF.PostedFile.SaveAs(baseLocation + fn);
filesUploaded++;
status += fn + "<br>";
}
catch(Exception err)
{
lblError.Text = "Error saving file " + baseLocation
+ "<br>" + err.ToString();
}
}

if(filesUploaded == hif.Count)
{
lblError.Text = "These " + filesUploaded + " file(s) were "
+ "uploaded:<br>" + status;
}
hif.Clear();
lstAttachments.Items.Clear();
}
}

Nov 17 '05 #3
thanks a lot, it's now working.

"Ruben Silva PT" wrote:
Hi!!!

I had a similar problem and i solve it using the following lines:
string folder = Path.GetDirectoryName(path);
if(!Directory.Exists(folder))
{
try
{
Directory.CreateDirectory(folder);
}
catch(Exception ex)
{
EventLog.WriteEntry("LogService", ex.Message,
EventLogEntryType.Error);
}
}

I hope it solves your problem.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Newbie | last post: by
3 posts views Thread by Amir Eshterayeh | last post: by
12 posts views Thread by Mats Lycken | last post: by
7 posts views Thread by Nathan Sokalski | last post: by
6 posts views Thread by Charlie Bear | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.