You must Sign In to post a response.
  • Category: ASP.NET

    Get image and image name from folder

    Dear all,
    Please give me some idea how to get the image and image name stored it in folder.
    The images are stored in folder.i need to get the images and images name from the folder and stored it in database using C#.

    Please send me some useful links and give me some suggestions how to perform this .


    Thanks in advance,

    Anu
  • #725344
    With the help of Directory namespace you can get the al files from directory with the extesnion .bmp, .jpg, .png etc
    check following code snippet for more details

    try
    {
    //collect all image extension first
    var Extension= new string[] { ".bmp", ".png", ".gif", ".jpg"};
    FileInfo[] objFInfo = (from objFile in new DirectoryInfo(path).GetFiles()
    where Extension.Contains(objFInfo.Extension.ToUpper())
    select objFile)
    .ToArray();
    foreach (file Fi in objFInfo )
    {
    MessageBox.Show(Fi.Name);
    }
    }



    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #725355
    Hi,

    try like below code to get all image files information from the folder

    DataTable dt = new DataTable();
    DataRow dr;
    String[] s1;
    protected void Button1_Click(object sender, EventArgs e)
    {
    //Initialize Directory path
    s1 = Directory.GetFiles(@"D:\test");
    //File Name, File Type, File size, create date
    for (int i = 0; i <= s1.Length - 1; i++)
    {
    if (i == 0)
    {
    //Add Data Grid Columns with name
    dt.Columns.Add("File_Name");
    dt.Columns.Add("File_Type");
    dt.Columns.Add("File_Size");
    dt.Columns.Add("Create_Date");
    }
    //Get each file information
    FileInfo f = new FileInfo(s1[i]);
    FileSystemInfo f1 = new FileInfo(s1[i]);

    if (f1.Extension == ".jpg" || f1.Extension == ".png" || f1.Extension == ".gif") //add another locations too
    {
    dr = dt.NewRow();
    //Get File name of each file name
    dr["File_Name"] = f1.Name;
    //Get File Type/Extension of each file
    dr["File_Type"] = f1.Extension;
    //Get File Size of each file in KB format
    dr["File_Size"] = (f.Length / 1024).ToString();
    //Get file Create Date and Time
    dr["Create_Date"] = f1.CreationTime.Date.ToString("dd/MM/yyyy");
    //Insert collected file details in Datatable
    dt.Rows.Add(dr);
    }
    }
    if (dt.Rows.Count > 0)
    {
    //Finally Add DataTable into DataGridView
    GridView1.DataSource = dt;
    GridView1.Databind();
    }
    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #725363
    Thankyou for your information,
    how to get the image ,for ex the image like india.jpg is stored in folder ,
    i need to get the file name (india) as well as image (india.jpg) to be stored it in database


    thanks in advance,
    Anu

  • #725366
    Hi,

    If you want only file name without extenstion then try in the above code


    dr["File_Name"] = f1.Name.ToString().Replace(f1.Extension,"");

    Regards
    N.Ravindran
    Your Hard work never fails

  • #725370
    Thankyou for your information,
    how to get the image ,for ex the image like india.jpg is stored in folder ,
    i need to get the file name (india) as well as image (india.jpg) to be stored it in database


    thanks in advance,
    Anu

  • #725373
    Hi,

    Answer is already available in the above code. You can update that code as per requirement like this to get information

    string filename = f1.Name.ToString().Replace(f1.Extension,"");
    strine imageName= f1.Name.ToString();

    Regards
    N.Ravindran
    Your Hard work never fails

  • #725374
    Dear Mr.Ravindran,


    I Used this code ,but i need image file name as well as Corresponding image to be get from folder to be saved it in database.

    kindly help me in this issue ...


    Thanks
    anu


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.