Saturday 23 July 2011

C# Move File or Rename File

Now i am going to share to you on how to move a file:
We going to move New folder (3) into New folder (2)

 DirectoryInfo Directory = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
* this will return the folder we SELECT to be move

Directory.MoveTo(Directory.Parent.FullName + "\\New folder (2)\\New folder (3)");
* this is the commands to MOVE the file.
*Directory.MoveTo(the destination + \\New folder (3)");
* IF you want to RENAME just change the  New folder (3) to ANY name you want.

Programming Look Like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace Try_an_Error
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
                DirectoryInfo Directory = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                Directory.MoveTo(Directory.Parent.FullName + "\\New folder (2)\\New folder (3)");

            }
        }
    }
}

3 comments: