How to Create Folder and Save File in It Using C#

How to Create Folder and Save File in It Using C#

I create new folder but how to save file in that please help me.

string createfolder = "E:/tmp/jobres/" + uId;    
System.IO.Directory.CreateDirectory(createfolder);
AsyncFileUpload1.SaveAs(Server.MapPath("/tmp/jobres/" + AsyncFileUpload1.PostedFile.FileName));

but how to store my file in created folder?

5

2 Answers

Since you are using MapPath on save, your directory may be created in the wrong spot. You should be using MapPath when creating your directory as well:

var createfolder = Path.Combine(Server.MapPath("/tmp/jobres/"), uId.ToString());    
System.IO.Directory.CreateDirectory(createfolder);
AsyncFileUpload1.SaveAs(Path.Combine(createdFolder, AsyncFileUpload1.PostedFile.FileName));
0
 string createfolder = "/tmp/jobres/" + uId;                           
 System.IO.Directory.CreateDirectory(createfolder);
 AsyncFileUpload1.SaveAs(Path.Combine(createfolder,AsyncFileUpload1.PostedFile.FileName));
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.