File.Delete(@"C:TempDataAuthors.txt");
Directory.Delete(@"folderPath", true);
string rootFolderPath = @"C:SomeFolderAnotherFolderFolderCOntainingThingsToDelete";
string filesToDelete = @"*DeleteMe*.doc"; // Only delete DOC files containing "DeleteMe" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
System.Diagnostics.Debug.WriteLine(file + "will be deleted");
// System.IO.File.Delete(file);
}
// Simple synchronous file deletion operations with no user interface.
// To run this sample, create the following files on your drive:
// C:UsersPublicDeleteTest est1.txt
// C:UsersPublicDeleteTest est2.txt
// C:UsersPublicDeleteTestSubDir est2.txt
public class SimpleFileDelete
{
static void Main()
{
// Delete a file by using File class static method...
if(System.IO.File.Exists(@"C:UsersPublicDeleteTest est.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:UsersPublicDeleteTest est.txt");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
// ...or by using FileInfo instance method.
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:UsersPublicDeleteTest est2.txt");
try
{
fi.Delete();
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
// Delete a directory. Must be writable or empty.
try
{
System.IO.Directory.Delete(@"C:UsersPublicDeleteTest");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
// Delete a directory and all subdirectories with Directory static method...
if(System.IO.Directory.Exists(@"C:UsersPublicDeleteTest"))
{
try
{
System.IO.Directory.Delete(@"C:UsersPublicDeleteTest", true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
// ...or with DirectoryInfo instance method.
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:UsersPublicpublic");
// Delete this dir and all subdirs.
try
{
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
}
string gpath;
string path=@"c:UsersAdamDesktop";
string name="file";
string f="";
int i=0;
string ext=".txt";
while(File.Exists(path + name + f + ext))
{
i++;
f = i.ToString();
}
gpath = path + name + f + ext;
button2.Enabled = true;
File.Create(gpath);
File.Delete(gpath);//why there is an Error??