how to create folder if missing in asp.net website

By Junaid A   Posted on February-10-2023   128

Literature

Problem: how to create folder if missing in website


Solution:

add key in web.config


 <add key="UploadedFilePath" value="C:\menuitemphoto\"></add>


in your asp.net code write below 

     string fileuploaddingpath = ConfigurationManager.AppSettings["UploadedFilePath"];

            try
            {
                if (!Directory.Exists(fileuploaddingpath))
                {
                    
                    DirectoryInfo di = Directory.CreateDirectory(fileuploaddingpath);  // Try to create the directory. if there is no directory in application
                }
            }
            catch (IOException ioex)
            {
                Console.WriteLine(ioex.Message);
            }
 

 

 


            HttpPostedFileBase selectedFile1 = Request.Files["txtImgItemPhoto"];
            if (selectedFile1 != null && selectedFile1.ContentLength > 0)
            {
                

                if (selectedFile1 != null && selectedFile1.ContentLength > 0)
                {
                    var updatedfilename = selectedFile1.FileName.Replace(" ", "-");
                    var fileName = "photo1_" + DateTime.Now.ToFileTimeUtc() + Path.GetFileName(updatedfilename);
                    var fileNamethmb = fileName;
                 //   var aa = Path.Combine(fileuploaddingpath, fileName);
                    var imagePath = Path.Combine(fileuploaddingpath, fileName);
                    selectedFile1.SaveAs(imagePath);
                    dt.MenuItemPictureBox = imagePath;

                }
            }

 

File uploading, file upload

 

Quotation by Hurbert hoover
By Junaid A    10-Feb-2023 Views  128



You may also read following recent articles

How to Run SQL Server Service