简体   繁体   中英

Removing a directory with files inside AppData

Well, what I want to do is to remove my folder that is located inside Roaming. I want this to work for every user, so the username of every PC is different. I already know that to get path of AppData I need following:

var path = Environment.SpecialFolder.ApplicationData;

But what to do to remove the folder with a specific name (Let's call it ExampleDir)? I tried this:

Path.Combine(path + "Kappa");
Directory.Delete(true.ToString());

But this does not work. I am beginner at C#, still, I want to practice. Will be grateful for help =)

First of all, Path.Combine() is used to replace string concatenation so don't concatenate strings in it. Pass every name you want to concatenate as a parameter and the function will do the rest.

Secondly, to remove a folder containing files you have to use the Directory.Delete(string, bool) overload. The bool value (called recursive ) indicates wether you want to remove files and subfolders in the specified directory (see the MSDN Documentation for Directory.Delete() ) .

And finally, Environment.SpecialFolder.ApplicationData is just an enumration (meaning it's just a number). You have to pass it as a parameter to the Environment.GetFolderPath() method to get the actual path of the AppData folder.

Example:

string AppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string FolderToDelete = Path.Combine(AppDataFolder, "Kappa");

try
{
    Directory.Delete(FolderToDelete, true); //Setting "recursive" to true will remove every subfile/-folder.
}
catch (Exception ex)
{
    //An error occurred, use this block to log it/show it to the user/whatever.
    //ex.Message - The error message.
    //ex.StackTrace - Where in the code the error occurred.
}

EDIT:

As @dman2306 said in his comment, some exception handling is good practice in case of that the deletion fails. I have added this to my above code.

The code in the try block will run until an exception is thrown (if any). If an exception is thrown the execution will move on to the catch block, which is where you catch the exception and perform for example error logging, stopping other procedures, etc.

EDIT 2:

You possibly have to add "Roaming" to Path.Combine() . I am unsure if it's already included or not, and I'm unable to test that now.

 string FolderToDelete = Path.Combine(AppDataFolder, "Roaming", "Kappa"); 

Forget what I said , SpecialFolder.ApplicationData gives you the path to the Roaming folder.

  • First of all you are using Path.Combine() wrong. But you don't need that at all.
  • Second you have to use backslases to separate folders in a path when combining them with +.
  • And third: The method Directory.Delete() wants the path of the folder you want to delete as an argument.

Try this:

Directory.Delete(Environment.SpecialFolder.ApplicationData + "\\Kappa");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM