在一个目录下有几个子目录,在子目录下面有文件,想把子目录下的文件移到另外的目录下面,请问这如何写?谢谢!1

解决方案 »

  1.   

    Directory类  files类
      

  2.   

    System.IO.File.Create(@"c:\a\b\r.txt");//创建文件
    System.IO.File.Move(@"c:\a\b\r.txt",@"c:\a\b\r.txt");//移动文件
    System.IO.File.Delete(@"c:\a\b\r.txt");//删除文件
      

  3.   

    namespace GetFileSystemEntries
    {
        class Class1 
        {
            static void Main(string[] args) 
            {
                Class1 snippets = new Class1();            string path = System.IO.Directory.GetCurrentDirectory();
                string filter = "*.exe";            snippets.PrintFileSystemEntries(path);
                snippets.PrintFileSystemEntries(path, filter);        
                snippets.GetLogicalDrives();
                snippets.GetParent(path);
                snippets.Move("C:\\proof", "C:\\Temp");
            }        
            void PrintFileSystemEntries(string path) 
            {
                
                try 
                {
                    // Obtain the file system entries in the directory path.
                    string[] directoryEntries =
                        System.IO.Directory.GetFileSystemEntries(path);                 foreach (string str in directoryEntries) 
                    {
                        System.Console.WriteLine(str);
                    }
                }
                catch (ArgumentNullException) 
                {
                    System.Console.WriteLine("Path is a null reference.");
                }
                catch (System.Security.SecurityException) 
                {
                    System.Console.WriteLine("The caller does not have the " +
                        "required permission.");
                }
                catch (ArgumentException) 
                {
                    System.Console.WriteLine("Path is an empty string, " +
                        "contains only white spaces, " + 
                        "or contains invalid characters.");
                }
                catch (System.IO.DirectoryNotFoundException) 
                {
                    System.Console.WriteLine("The path encapsulated in the " + 
                        "Directory object does not exist.");
                }
            }
            void PrintFileSystemEntries(string path, string pattern) 
            {
                try 
                {
                    // Obtain the file system entries in the directory
                    // path that match the pattern.
                    string[] directoryEntries =
                        System.IO.Directory.GetFileSystemEntries(path, pattern);                 foreach (string str in directoryEntries) 
                    {
                        System.Console.WriteLine(str);
                    }
                }
                catch (ArgumentNullException) 
                {
                    System.Console.WriteLine("Path is a null reference.");
                }
                catch (System.Security.SecurityException) 
                {
                    System.Console.WriteLine("The caller does not have the " +
                        "required permission.");
                }
                catch (ArgumentException) 
                {
                    System.Console.WriteLine("Path is an empty string, " +
                        "contains only white spaces, " + 
                        "or contains invalid characters.");
                }
                catch (System.IO.DirectoryNotFoundException) 
                {
                    System.Console.WriteLine("The path encapsulated in the " + 
                        "Directory object does not exist.");
                }
            }        // Print out all logical drives on the system.
            void GetLogicalDrives() 
            {
                try 
                {
                    string[] drives = System.IO.Directory.GetLogicalDrives();                foreach (string str in drives) 
                    {
                        System.Console.WriteLine(str);
                    }
                }
                catch (System.IO.IOException) 
                {
                    System.Console.WriteLine("An I/O error occurs.");
                }
                catch (System.Security.SecurityException) 
                {
                    System.Console.WriteLine("The caller does not have the " +
                        "required permission.");
                }
            }
            void GetParent(string path) 
            {
                try 
                {
                    System.IO.DirectoryInfo directoryInfo =
                        System.IO.Directory.GetParent(path);                System.Console.WriteLine(directoryInfo.FullName);
                }
                catch (ArgumentNullException) 
                {
                    System.Console.WriteLine("Path is a null reference.");
                }
                catch (ArgumentException) 
                {
                    System.Console.WriteLine("Path is an empty string, " +
                        "contains only white spaces, or " +
                        "contains invalid characters.");
                }
            }
            void Move(string sourcePath, string destinationPath) 
            {
                try 
                {
                    System.IO.Directory.Move(sourcePath, destinationPath);
                    System.Console.WriteLine("The directory move is complete.");
                }
                catch (ArgumentNullException) 
                {
                    System.Console.WriteLine("Path is a null reference.");
                }
                catch (System.Security.SecurityException) 
                {
                    System.Console.WriteLine("The caller does not have the " +
                        "required permission.");
                }
                catch (ArgumentException) 
                {
                    System.Console.WriteLine("Path is an empty string, " +
                        "contains only white spaces, " + 
                        "or contains invalid characters.");    
                }
                catch (System.IO.IOException) 
                {
                    System.Console.WriteLine("An attempt was made to move a " +
                        "directory to a different " +
                        "volume, or destDirName " +
                        "already exists."); 
                }
            }
        }
    }