How to delete a specific file from folder using asp.net

后端 未结 5 622
感情败类
感情败类 2021-02-01 21:12

here\'s the deal I got a datagridviewer which is called gridview1 and a fileupload1 when i upload a file it updates the gridview1 and table in database with the file name and pa

5条回答
  •  难免孤独
    2021-02-01 21:32

    string sourceDir = @"c:\current";
    string backupDir = @"c:\archives\2008";
    
    try
    {
        string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
        string[] txtList = Directory.GetFiles(sourceDir, "*.txt");
    
        // Copy picture files. 
        foreach (string f in picList)
        {
            // Remove path from the file name. 
            string fName = f.Substring(sourceDir.Length + 1);
    
            // Use the Path.Combine method to safely append the file name to the path. 
            // Will overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
        }
    
        // Copy text files. 
        foreach (string f in txtList)
        {
    
            // Remove path from the file name. 
            string fName = f.Substring(sourceDir.Length + 1);
    
            try
            {
                // Will not overwrite if the destination file already exists.
                File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
            }
    
            // Catch exception if the file was already copied. 
            catch (IOException copyError)
            {
                Console.WriteLine(copyError.Message);
            }
        }
    
        // Delete source files that were copied. 
        foreach (string f in txtList)
        {
            File.Delete(f);
        }
        foreach (string f in picList)
        {
            File.Delete(f);
        }
    }
    
    catch (DirectoryNotFoundException dirNotFound)
    {
        Console.WriteLine(dirNotFound.Message);
    }
    

提交回复
热议问题