dodgerfan
asked on
Delete all files in a folder - C#
How can you delete all of the files in a directory with ASP.Net/C#?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
// Delete files on the server:
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Data"));
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
file.Delete();
}
// Delete files on the client's machine
// will lead to a serious security breach
// NO GO
cdaly33: your code will cause issues:
string[] files = IO.Directory.GetFiles("pat h with files to delete");
Gives the error:
Cannot convert source type System.IO.FileInfo[ ] to target type string[ ]
string[] files = IO.Directory.GetFiles("pat
Gives the error:
Cannot convert source type System.IO.FileInfo[ ] to target type string[ ]
As I understand user needs to have rights to delete. Just in case.
See also other solution:
https://www.experts-exchange.com/questions/23630424/Asp-net-delete-a-file-in-the-server.html
See also other solution:
https://www.experts-exchange.com/questions/23630424/Asp-net-delete-a-file-in-the-server.html
The folder with the files in it needs to have Full Permission granted to NETWORK_SERVICE
Danger Will Robinson...
Danger Will Robinson...
string[] files = Directory.GetFiles(@"C:\te mp");
foreach (string file in files)
{
File.Delete(file);
}
foreach (string file in files)
{
File.Delete(file);
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window