I have a problem with the function Directory.delete?
Take a look at my code:
strin开发者_JAVA技巧g desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
try
{
if (Directory.Exists(Path.Combine(desktopPath, "Hackers.avi")))
Directory.Delete(Path.Combine(desktopPath, "Hackers.avi"), true);
after runing the file is still exist on my desktop , why??
It is unlikely that Hackers.avi
is a directory - .avi
is normally used an extension for a video file (see Audio Video Interleave on Wikipedia for more information).
Try using File.Delete
instead of Directory.Delete
:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
try
{
string pathToFile = Path.Combine(desktopPath, "Hackers.avi");
File.Delete(pathToFile);
// etc...
I also omitted the call to File.Exists
because you don't have to check for a file's existence before deleting it. File.Delete
does not throw if the file doesn't exist.
You want to delete file, sou you must use 'File.Delete'
精彩评论