Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select image from file dialog, show it in picture box on my window form, and save it in folder in MyCurrentSolution\\images folder, if folder with batch name exists in images folder, that is written in BatchTextbox on winform then save the image with Student ID that will be taken from StudentTextBoxID, other wise create the folder in images folder with batch name taken from BatchTextbox on winform and then save the imgage. and i Want to use start up path, from my solution, because my project exee file can be stored in any of the directory of client. Thanks in Advance.
eg. mySolution\\images\\Batch1\\STd1.jpg
mySolution\\images\\Batch1\\STd11.jpg
mySolution\\images\\Batch2\\STd2.jpg
---------------------------
Posted

C#
string batch ="Batch1";
string std = "STd1";
string dir =Path.Combine(System.IO.Directory.GetCurrentDirectory(), "images",batch);
if(!System.IO.Directory.Exists(dir))
{
   Directory.CreateDirectory(dir);
}

File.Copy(openFileDialog.FileName, Path.Combine(dir, std +Path.GetExtension(openFileDialog.FileName)));


ref:
http://msdn.microsoft.com/en-us/library/system.io.file.copy(v=vs.110).aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(v=vs.110).aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.path.combine(v=vs.110).aspx[^]
 
Share this answer
 
v2
C#
protected void btnSaveImg_Click(object sender, EventArgs e)
    {
        //If not, we can save out a copy
        if (imgPreviewCase.ImageUrl != DefaultImagePath && GetExtension() != "")
        {
            string foldername = "~/Pics/CasePics/" + CaseId.ToString();
            bool isExists = System.IO.Directory.Exists(Server.MapPath(foldername));

            if (!isExists)
                System.IO.Directory.CreateDirectory(Server.MapPath(foldername));

            string filename = "";
            string fullfilename = "";

            //insert into database to get image id
            objCase_MasterDA = new Case_MasterDA();
            int Image_Id = objCase_MasterDA.Insert_Case_Image(CaseId, "");

            //change file name based on id
            filename = CaseId.ToString() + "-" + Image_Id.ToString() + GetExtension();
            fullfilename = foldername + "/" + filename;

            //update new path to database
            Image_Id = objCase_MasterDA.Update_Case_Image(Image_Id, fullfilename);


            //remove image from preview image
            imgPreviewCase.ImageUrl = DefaultImagePath;
            hfExt.Value = "";
        }
        else
        {
            lblError.Text = "Please select image file.";
        }
    }


change accoriding to your need...
 
Share this answer
 
v2
Comments
Abdullah Kundi 22-May-14 6:56am    
Dear i appreciate your effort but i in need code for window application not for web Application

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900