.C# 拷贝一个图片到指定文件夹下(IO文件操作实例)

string picPath = "";//图片路径作为全局变量,是因为图片上传的时候更改了图片的路径,所以要作为全局变量,以便此时图片的路径是拷贝后的地方。
        OpenFileDialog ofd = new OpenFileDialog();
        private void btnUpload_Click(object sender, System.EventArgs e)
        {
            //图片上传对话框

            //OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "浏览";
            ofd.Filter = "JPEG|*.jpeg;*.jpg|Bitmap|*.bmp;*.dib|GIF|*.gif;*.gfa|Portable Network Graphics|*.png|Windows Metafile|*.wmf|Windows Enhanced Metafile|*.emf";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (ofd.FileName != null)
                {
                    this.imgPicture.Image = Image.FromFile(ofd.FileName);
                    this.txtPicturePath.Text = ofd.FileName; //上传图片路径


                    //转换为byte类型,保存

                    MemoryStream ms = new MemoryStream();
                    this.imgPicture.Image.Save(ms, imgPicture.Image.RawFormat);
                    image = null;
                    image = ms.ToArray(); //赋值byte                   

                    ms.Close();

                   picPath = ofd.FileName;                     //获得选择的文件的路径

                    string filename = Path.GetFileName(picPath);       //获得图片的真实名字
                    string targetPath = @"E:\SEA\Magzine\pic\"+filename;
                    if (!System.IO.Directory.Exists(@"E:\SEA\Magzine\pic"))
                    { // 目录不存在,建立目录 
                        System.IO.Directory.CreateDirectory(@"E:\SEA\Magzine\pic");
                    }
                    System.IO.File.Copy(ofd.FileName,targetPath);
                    picPath = targetPath;
                }
            }

            if (picPath.Equals(""))
            {
                MessageBox.Show("请上传图片!!!");
            }

这样即可以把图片上传显示出来,并且把图片拷贝到了E:\SEA\Magzine\pic目录下,且数据库中也存储的是拷贝后的图片的路径。

string path=System.Windows.Forms.Application.StartupPath + @http://www.cnblogs.com/../;

System.IO.Directory.SetCurrentDirectory(path);

 FileInfo OldFileName = new FileInfo(System.IO.Directory.GetCurrentDirectory() + @"\images\1.gif");

原文地址:https://www.cnblogs.com/zzdxpq/p/2290508.html