473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2989
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert | last post by:
I get these errors when creating a asp .net web application project in VS 2003 on a remote web server: "Microsoft Development Environment The Web was created successfully, but an error occurred...
10
by: huzz | last post by:
I have web application that quaries the Active Directory to get user details.. everything works fine but someday I'll get System.Runtime.InteropServices.COMExection and if I restart the client...
2
by: Newbie | last post by:
Hello! I'm trying to figure out how to do the following: I need to show a table (perhaps using DataGrids, repeaters or others) for each salesperson in the sales dept. But the no. of tables...
3
by: Amir Eshterayeh | last post by:
Dear Friends Would you please give me your professional idea about this asp.net problem. I need different virtual directory for different customer with their names like these:...
5
by: Praveen | last post by:
I have script files embedded as resources in my dll. During runtime, I would like to write it out into the app dir (or a app sub-dir) and send the appropriate virtual path to the client. This makes...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
5
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
6
by: Charlie Bear | last post by:
i'm really stuck with this one can anyone help! i have a website that uses c#. it creates a series of directories and files from an xml source. when the xml changes, the directory that the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.