Search code examples
c#.netopenfiledialogsystem.io.file

Change name and move file in C#


private void btn_add_image_Click(object sender, EventArgs e)
    {
        openFileDialog1.Title = "Choose a file";
        openFileDialog1.InitialDirectory = "C:\\";
        openFileDialog1.Filter = "  JPEG Files (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg|All Files (*.*)|*.*";
        openFileDialog1.ShowDialog();
        string file_name = openFileDialog1.FileName;
        string filename2 = openFileDialog1.SafeFileName;
        pictureBox1.Image = Image.FromFile(file_name);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            pictureBox1.Image.Dispose();
            pictureBox1.Image = null;
            string[] extension = getExtension("images\\" + userid);
            if (File.Exists("images\\" + userid + extension[0]))
            {   
                File.Delete("resimler\\" + userid + extension[0]);
            }

        }
        catch (Exception)
        {
            MessageBox.Show("İmage cannot find");
        }

I want to Change File name and save , so i wrote this code if file exists , than delete file and save the choosen with userid name but i cant do change name and save file


Solution

  • if (File.Exists(@"\path\to\source"))
    {
        File.Move(@"\path\to\source",@"\path\to\destination")
    }