Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i am uploading one file from local pc...while uploading i am saving the path into one table as a record at the same time i am saving the file into one folder it is done successfully but..when i want to delete one record from that table at the same time i want to delete file of that record which is saved in folder also...is it possible.
Posted

Yes.
It's two operations, but it's just a case of issuing a SQL DELETE and a File.Delete in sequence.
What I would probably do is start a transaction, then enter a try..catch block. Issue the SQL DELETE, then the File.Delete to remove the file, then commit the transaction. In the catch block, rollback so that the DB still refers to the file if there is a problem with deleting it (unless the exception is "file not found" in which case commit it anyway to get them back into sync)
 
Share this answer
 
C#
string rootFolderPath = @"C:\\SomeFolder\\AnotherFolder\\FolderCOntainingThingsToDelete";
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);
}


above method will delete All File in the Folder

C#
System.IO.File.Delete(filePath as String);


above method will delete Single File
 
Share this answer
 
v2
C#
FileInfo TheFile = new FileInfo(Server.MapPath("") + "//Folder_Name//" + File_Name);
                if (TheFile.Exists)
                {
                    File.Delete(Server.MapPath("") + "//Folder_Name//" + File_Name););
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900