请问如何删除指定路径下的文件???比如说相对路径为"upload\"下的文件?谢谢!

解决方案 »

  1.   

    [C#] 
    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
            try 
            {
                using (StreamWriter sw = File.CreateText(path)) {}
                string path2 = path + "temp";            // Ensure that the target does not exist.
                File.Delete(path2);            // Copy the file.
                File.Copy(path, path2);
                Console.WriteLine("{0} was copied to {1}.", path, path2);            // Delete the newly created file.
                File.Delete(path2);
                Console.WriteLine("{0} was successfully deleted.", path2);
            } 
            catch (Exception e) 
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
    }
      

  2.   

    string[] files = System.IO.Directory.GetFiles(@"C:\tmp");
    for(int i = 0 ; i < files.Length ; i ++)
    {
    System.IO.File.Delete(files[i]);
    }
      

  3.   

    deltree:
    private void del_dir(string path)
    {
    del_file(path);
    string[] dirs = System.IO.Directory.GetDirectories(path);
    for(int i = 0 ; i < dirs.Length ; i ++)
    {
    del_dir(dirs[i]);
    System.IO.Directory.Delete(dirs[i]);
    }
    }
    private void del_file(string path)
    {
    string[] files = System.IO.Directory.GetFiles(path);
    for(int i = 0 ; i < files.Length ; i ++)
    {
    System.IO.File.Delete(files[i]);
    }
    }
    test:
    private void button2_Click(object sender, System.EventArgs e)
    {
    del_dir(@"c:\tmp");
    }
      

  4.   

    chinasdp() 的补充一下判断文件是否存在就更完美了
    private void Button2_Click(object sender, System.EventArgs e)
    {
    string path = @"D:\tmp";
    if (File.Exists(path))
    del_dir(path);
    } private void del_dir(string path)
    {
    del_file(path);
    string[] dirs = System.IO.Directory.GetDirectories(path);
    for(int i = 0 ; i < dirs.Length ; i ++)
    {
    del_dir(dirs[i]);
    System.IO.Directory.Delete(dirs[i]);
    }
    }
    private void del_file(string path)
    {
    string[] files = System.IO.Directory.GetFiles(path);
    for(int i = 0 ; i < files.Length ; i ++)
    {
    System.IO.File.Delete(files[i]);
    }
    }
      

  5.   

    string[] files = System.IO.Directory.GetFiles(path);
    for(int i = 0 ; i < files.Length ; i ++)
    {
    System.IO.File.Delete(files[i]);
    }