473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Direc toryNotFoundExc eption: Could not find a part of the path
"C:\temp\188\_d s27.tmp". at System.IO.__Err or.WinIOError(I nt32 errorCode,
String str) at System.IO.FileS tream..ctor(Str ing path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync,
String msgPath, Boolean bFromProxy) at System.IO.FileS tream..ctor(Str ing
path, FileMode mode) at System.Web.Http PostedFile.Save As(String filename) at
mole.proposals. dltaskorders.ad dTaskOrder.btnU pload_Click(Obj ect sender,
EventArgs e) in c:\inetpub\wwwr oot\dltaskorder s\addtaskorder. aspx.cs:line 286

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

}
else
{
foreach(System. Web.UI.HtmlCont rols.HtmlInputF ile HIF in hif)
{
try
{
string fn =
System.IO.Path. GetFileName(HIF .PostedFile.Fil eName);
HIF.PostedFile. SaveAs(baseLoca tion + fn);
filesUploaded++ ;
status += fn + "<br>";
}
catch(Exception err)
{
lblError.Text = "Error saving file " + baseLocation
+ "<br>" + err.ToString();
}
}

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

I had a similar problem and i solve it using the following lines:
string folder = Path.GetDirecto ryName(path);
if(!Directory.E xists(folder))
{
try
{
Directory.Creat eDirectory(fold er);
}
catch(Exception ex)
{
EventLog.WriteE ntry("LogServic e", ex.Message,
EventLogEntryTy pe.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
GetDirectoryNam e 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.co m

"Newbie" <Ne****@discuss ions.microsoft. com> wrote in message
news:1A******** *************** ***********@mic rosoft.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.Direc toryNotFoundExc eption: Could not find a part of the path
"C:\temp\188\_d s27.tmp". at System.IO.__Err or.WinIOError(I nt32 errorCode,
String str) at System.IO.FileS tream..ctor(Str ing path, FileMode mode,
FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync,
String msgPath, Boolean bFromProxy) at System.IO.FileS tream..ctor(Str ing
path, FileMode mode) at System.Web.Http PostedFile.Save As(String filename)
at
mole.proposals. dltaskorders.ad dTaskOrder.btnU pload_Click(Obj ect sender,
EventArgs e) in c:\inetpub\wwwr oot\dltaskorder s\addtaskorder. aspx.cs:line
286

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

}
else
{
foreach(System. Web.UI.HtmlCont rols.HtmlInputF ile HIF in hif)
{
try
{
string fn =
System.IO.Path. GetFileName(HIF .PostedFile.Fil eName);
HIF.PostedFile. SaveAs(baseLoca tion + fn);
filesUploaded++ ;
status += fn + "<br>";
}
catch(Exception err)
{
lblError.Text = "Error saving file " + baseLocation
+ "<br>" + err.ToString();
}
}

if(filesUploade d == 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.GetDirecto ryName(path);
if(!Directory.E xists(folder))
{
try
{
Directory.Creat eDirectory(fold er);
}
catch(Exception ex)
{
EventLog.WriteE ntry("LogServic e", ex.Message,
EventLogEntryTy pe.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
1742
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 when trying to configure the application root for this Web. Web projects may not operate correctly without an application root. The returned error was: Active Directory Services cannot find the web server. A possible cause for this is an...
10
5427
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 machine then it works again. here is one of the method where am calling the AD public bool UserExist(string UserName) {
2
1702
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 to be shown is to be determined during runtime. It's possible that some salespersons have no sales figures to begin with and therefore no table to show. Thanks in advance and please help!
3
1737
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: www.oursite.com/Jack www.oursite.com/Sue www.oursite.com/Tom
5
1902
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 deploying my dll much easier - just a single dll, not having to worry about deploying script files. However, the runtime doesn't seem have enough permissions to write out to the app dir (or any of it's subdir), by default. The folder where I...
12
3153
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 restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass something else that strings between the domains...
5
3491
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 case. One handle remains open. Debugging shows that it's actually the IIS cache and not ASP.NET that owns this handle. During IIS shutdown, the handle is closed with the following stack trace: ChildEBP RetAddr 0006fbe4 5a403e05...
7
2300
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, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
6
1779
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 change applies to is deleted and recreated. All the children for that directory are also re created (as the parent directory then has a different name). the problem i have is that *sometimes* the files or directories don't get created. It either...
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.