C#设置新图像后如何从文件夹中删除以前的图像 [英] C# how to delete previous image from folder after setting new image

查看:100
本文介绍了C#设置新图像后如何从文件夹中删除以前的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表格,我可以编辑我的帐户的设置名称,姓氏,登录,密码以及Image.I需要当我选择图像然后点击按钮保存,必须从文件夹中删除上一张图片。如何我这样做了吗?

这是我正在编辑设置的表格 [ ^ ]

这是用户的帐户 [ ^ ]



我尝试过:



 //这是保存按钮点击
private void bunifuImageButton1_Click(object sender,EventArgs e)
{
using(var context = new Suren_BankEntities())
{

用户建立=上下文。 Users.Find(Session.UserId);
if(metroTextBox1.Text!= String.Empty)
{
established.Name = metroTextBox1.Text;
}
if(metroTextBox2.Text!= String.Empty)
{
established.Surname = metroTextBox2.Text;
}
if(metroComboBox1.Text!= String.Empty)
{
established.Country = metroComboBox1.Text;
}
if(metroTextBox7.Text!= String.Empty)
{
established.CardNumber = int.Parse(metroTextBox7.Text);
}
if(metroTextBox5.Text!= String.Empty)
{
established.Login = metroTextBox5.Text;
}
if(metroTextBox6.Text!= String.Empty)
{
established.Password = metroTextBox6.Text;
}
if(pictureBox1.Image!= null)
{
established.Photo = this.n;
}
context.SaveChanges();
MessageBox.Show(设置已编辑!);
if(us == null)
{
us = new UserSettings();
us.Dock = DockStyle.Fill;
Profile.Instance.FormContainer.Controls.Add(us);
}
us.BringToFront();
}
}

//这是上传按钮点击
private void bunifuImageButton6_Click(object sender,EventArgs e)
{

this.op = new OpenFileDialog();
if(op.ShowDialog()== DialogResult.OK)
{
if(Directory.Exists(img))
{
try
{
long random_number = DateTimeOffset.Now.ToUnixTimeMilliseconds();
File.Copy(op.FileName,@img \+ random_number + op.SafeFileName);
pictureBox1.Image = new Bitmap(op.FileName);
this.n = random_number + op.SafeFileName;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

//用户设置的构造函数
public UserSettings()
{
DateTime now = DateTime.Today;
InitializeComponent();
using(var ctx = new Suren_BankEntities())
{
this.user = ctx.Users.Find(Session.UserId);
label3.Text =Name:+ user.Name;
label4.Text =Surname:+ user.Surname;
label5.Text =生于+ user.Country;
label6.Text =年龄:+(now.Year - user.Date.Year)+岁岁;
label7.Text =卡号为:+ user.CardNumber;
label13.Text =AMD:+ user.AMD;
label12.Text =RUR:+ user.RUR;
label11.Text =USD:+ user.USD;
label8.Text =登录:+ user.Login;
label9.Text =密码:+ user.Password;
if(File.Exists(@img\+ user.Photo))
{
pictureBox2.Image = new Bitmap(@img \+ user.Photo);
}
else
{
pictureBox2.Image = new Bitmap(@img\ devault.png);
}
}
}

解决方案

文件。删除方法(字符串)(System.IO) [ ^ ]

I have form where i can edit my account's settings` Name,Surname,Login,Password and also the Image.I need when i choose the image then click on button save, the previous image must be deleted from folder.How can i do it?
This is the form where i'm editing settings[^]
This is User's account[^]

What I have tried:

 // This is Save button click
private void bunifuImageButton1_Click(object sender, EventArgs e)
        {
            using (var context = new Suren_BankEntities())
            {
                
                User founded = context.Users.Find(Session.UserId);
                if(metroTextBox1.Text != String.Empty)
                {
                    founded.Name = metroTextBox1.Text;
                }
                if (metroTextBox2.Text != String.Empty)
                {
                    founded.Surname = metroTextBox2.Text;
                }
                if (metroComboBox1.Text != String.Empty)
                {
                    founded.Country = metroComboBox1.Text;
                }
                if (metroTextBox7.Text != String.Empty)
                {
                    founded.CardNumber = int.Parse(metroTextBox7.Text);
                }
                if (metroTextBox5.Text != String.Empty)
                {
                    founded.Login = metroTextBox5.Text;
                }
                if (metroTextBox6.Text != String.Empty)
                {
                    founded.Password = metroTextBox6.Text;
                }
                if (pictureBox1.Image != null)
                {
                    founded.Photo = this.n;
                }
                context.SaveChanges();
                MessageBox.Show("Settings has edited!");
                if (us == null)
                {
                    us = new UserSettings();
                    us.Dock = DockStyle.Fill;
                    Profile.Instance.FormContainer.Controls.Add(us);
                }
                us.BringToFront();
            }
        }

//This is Upload button click
 private void bunifuImageButton6_Click(object sender, EventArgs e)
        {
            
            this.op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                if (Directory.Exists("img"))
                {
                    try
                    {
                        long random_number = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                        File.Copy(op.FileName, @"img\" + random_number + op.SafeFileName);
                        pictureBox1.Image = new Bitmap(op.FileName);
                        this.n = random_number + op.SafeFileName;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

//Constructor of User Settings
 public UserSettings()
        {
            DateTime now = DateTime.Today;
            InitializeComponent();
            using (var ctx = new Suren_BankEntities())
            {
                this.user = ctx.Users.Find(Session.UserId);
                label3.Text = "Name: " + user.Name;
                label4.Text = "Surname: " + user.Surname;
                label5.Text = "Born in " + user.Country;
                label6.Text = "Age: " + (now.Year - user.Date.Year) + " years old";
                label7.Text = "Card Number is: " + user.CardNumber;
                label13.Text = "AMD: " + user.AMD;
                label12.Text = "RUR: " + user.RUR;
                label11.Text = "USD: " + user.USD;
                label8.Text = "Login: " + user.Login;
                label9.Text = "Password: " + user.Password;
                if (File.Exists(@"img\" + user.Photo))
                {
                    pictureBox2.Image = new Bitmap(@"img\" + user.Photo);
                }
                else
                {
                    pictureBox2.Image = new Bitmap(@"img\default.png");
                }
            }
        }

解决方案

File.Delete Method (String) (System.IO)[^]


这篇关于C#设置新图像后如何从文件夹中删除以前的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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