如何在我的Soloution文件夹'图像'中保存图像如果文件夹Batch1存在于图像文件夹中保存其他明智创建它并使用标准Id保存图像 [英] How Do I Save Image In My Soloution Folder 'Images' If A Folder Batch1 Exist In Image Folder Save It Other Wise Create It And Save Image With Std Id

查看:92
本文介绍了如何在我的Soloution文件夹'图像'中保存图像如果文件夹Batch1存在于图像文件夹中保存其他明智创建它并使用标准Id保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件对话框中选择图像,在窗口窗体的图片框中显示,并将其保存在MyCurrentSolution \\ images文件夹中的文件夹中,如果图像文件夹中存在具有批名称的文件夹,则写入然后在winform上的BatchTextbox保存带有将从StudentTextBoxID获取的学生ID的图像,另外在图像文件夹中创建文件夹,其中批处理名称取自winform上的BatchTextbox,然后保存。我希望从我的解决方案中使用启动路径,因为我的项目执行文件可以存储在客户端的任何目录中。在此先感谢。

例如。 mySolution \\images\\Batch1 \\STd1.jpg

mySolution \\images \\Batch1 \\STd11.jpg

mySolution \\images\\Batch2 \\STd2.jpg

---------------------- -----

解决方案

  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 [< a href =http://msdn.microsoft.com/en-us/library/system.io.file.copy(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

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 [ ^ ]


  protected   void  btnSaveImg_Click( object  sender,EventArgs e)
{
// 如果没有,我们可以保存一份副本
如果 (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 = ;

// 插入数据库以获取图片ID
objCase_MasterDA = new Case_MasterDA();
int Image_Id = objCase_MasterDA.Insert_Case_Image(CaseId, );

// 根据id更改文件名
filename = CaseId.ToString()+ - + Image_Id.ToString()+ GetExtension();
fullfilename = foldername + / + filename;

// 更新数据库的新路径
Image_Id = objCase_MasterDA .Update_Case_Image(Image_Id,fullfilename);


// 从预览图片中删除图片
imgPreviewCase.ImageUrl = DefaultImagePath;
hfExt.Value = ;
}
else
{
lblError.Text = 请选择图像文件。;
}
}





根据您的需要进行更改......


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
---------------------------

解决方案

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[^]


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...


这篇关于如何在我的Soloution文件夹'图像'中保存图像如果文件夹Batch1存在于图像文件夹中保存其他明智创建它并使用标准Id保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆