如何在c#中删除一个文件,例如C:\test.txt,尽管应用类似于批处理文件中的相同方法。

if exist "C:\test.txt"

delete "C:\test.txt"

else 

return nothing (ignore)

使用File类,这非常简单。

if(File.Exists(@"C:\test.txt"))
{
    File.Delete(@"C:\test.txt");
}

正如Chris在评论中指出的,实际上不需要执行File。自文件以来存在检查。如果文件不存在,Delete不会抛出异常,但如果使用绝对路径,则需要检查以确保整个文件路径有效。


if (File.Exists(path))
{
    File.Delete(path);
}

if (System.IO.File.Exists(@"C:\test.txt"))
    System.IO.File.Delete(@"C:\test.txt"));

but

System.IO.File.Delete(@"C:\test.txt");

只要文件夹存在,就会这样做。


像这样使用System.IO.File.Delete:

System.IO.File.Delete(@“C: \用法”)

从文档中可以看到:

如果要删除的文件不存在,则不会抛出异常。


如果您希望避免DirectoryNotFoundException异常,则需要确保文件的目录确实存在。文件。存在实现了这一点。另一种方法是像这样利用Path和Directory实用程序类:

string file = @"C:\subfolder\test.txt";
if (Directory.Exists(Path.GetDirectoryName(file)))
{
    File.Delete(file);
}

  if (System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt"))
    {
        // Use a try block to catch IOExceptions, to 
        // handle the case of the file already being 
        // opened by another process. 
        try
        {
            System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
            return;
        }
    }

如果您正在使用FileStream读取该文件,然后想要删除它,请确保在调用file . delete (path)之前关闭FileStream。我有这个问题。

var filestream = new System.IO.FileStream(@"C:\Test\PutInv.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
filestream.Close();
File.Delete(@"C:\Test\PutInv.txt");

有时你想删除一个文件,无论情况如何(无论异常发生,请删除该文件)。针对这种情况。

public static void DeleteFile(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }

            bool isDeleted = false;
            while (!isDeleted)
            {
                try
                {
                    File.Delete(path);
                    isDeleted = true;
                }
                catch (Exception e)
                {
                }
                Thread.Sleep(50);
            }
        }

注意:如果指定的文件不存在,则不会抛出异常。


您可以导入系统。IO命名空间使用:

using System.IO;

如果filepath是文件的全路径,可以检查是否存在并删除。

if(File.Exists(filepath))
{
     try
    {
         File.Delete(filepath);
    } 
    catch(Exception ex)
    {
      //Do something
    } 
}  

这是最简单的方法,

if (System.IO.File.Exists(filePath)) 
{
  System.IO.File.Delete(filePath);
  System.Threading.Thread.Sleep(20);
}

线程。睡眠将有助于完美的工作,否则,它将影响下一步如果我们做拷贝或写入文件。

另一种方法是,

if (System.IO.File.Exists(filePath))
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(filePath);
}

if (File.Exists(Path.Combine(rootFolder, authorsFile)))    
{    
// If file found, delete it    
File.Delete(Path.Combine(rootFolder, authorsFile));    
Console.WriteLine("File deleted.");    
} 

动态

 string FilePath = Server.MapPath(@"~/folder/news/" + IdSelect)
 if (System.IO.File.Exists(FilePath + "/" + name+ ".jpg"))
   {
    System.IO.File.Delete(FilePath + "/" + name+ ".jpg");
   }

删除目录下的所有文件

string[] files = Directory.GetFiles(rootFolder);    
foreach (string file in files)    
{    
File.Delete(file);    
Console.WriteLine($"{file} is deleted.");    
}