Move File

 


            System.IO.DirectoryInfo myDirectory = new DirectoryInfo(path);

            MoveFile(path, myDirectory.Parent.FullName + "/backup/");


   public static void MoveFile(String path, String targetPath)

    {

        FileInfo fi1 = new FileInfo(path);



        string fileName = fi1.Name;

        string sourcePath = fi1.DirectoryName;



        // Use Path class to manipulate file and directory paths. 

        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);

        string destFile = System.IO.Path.Combine(targetPath, fileName);


        CreateFolder(targetPath);

        //if (!Directory.Exists(targetPath))

        //{

        //    Directory.CreateDirectory(targetPath);

        //}


        File.Copy(sourceFile, destFile, true);

        fi1.Delete();

    }



    public static void CreateFolder(String path)

    {

        string stParentName = System.IO.Path.GetDirectoryName(path);

        if (!Directory.Exists(stParentName))

        {

            Directory.CreateDirectory(stParentName);

        }

    }


留言

熱門文章