Kireet
Kireet

Reputation: 33

In Manipulating the Windows file system I'm getting an exception while moving file from one directory to other

namespace WindowsFileOperation
{
    class WindowsFile
    {
       static void Main(string[] args)
       {
        Directory.CreateDirectory(@"C:\Users\kireett\Desktop\mydata");

        DirectoryInfo myDir = new DirectoryInfo(@"C:\Users\kireett\Desktop\mydata");

        FileInfo[] file = myDir.GetFiles();
        foreach (FileInfo f in file)
        {
            Console.WriteLine("name:{0}, Size:{1} lastAccessTime : {2} lastWriteTime :{3} Directory : {4} extension : {5}",
            f.Name, f.Length, f.LastAccessTime, f.LastWriteTime, f.Directory,f.Extension);
        }
        Directory.Move(@"C:\Users\kireett\Desktop\mydata\1.html", @"C:\Users\kireett\Desktop\Data sheet");
        Console.ReadKey();
       }
    }
}

Actually I have that "1.html" file in mydata directory. My aspect is moving that file into another directory "Data sheet". At Directory.Move I am getting an exception. Please help me.

Upvotes: 0

Views: 43

Answers (1)

Charlie
Charlie

Reputation: 4895

1) Use File.Move(...) instead of Directory.Move(...) because you want to move the file not Directory.

2) You need to move it to another file (rather than a folder), this can also be used to rename.

File.Move(@"C:\Users\kireett\Desktop\mydata\1.html", @"C:\Users\kireett\Desktop\Data sheet\2.html");

Upvotes: 1

Related Questions