0

In C# I am attempting to delete the same folder for any user that uses my app. I tried every wildcard and Visual studio does not like it. In Powershell works without problem, the issue here is not all users can use the script in admin mode.

My current code:

Directory.Delete(@"C:\Users\*\AppData\Roaming\Microsoft\Teams", true);

What I am attempting to delete is in the Roaming folder of windows and the folder is called "Teams" I am unsure how to code this to work on any user.

Ive tried: Directory.Delete(Environment.SpecialFolder.ApplicationData + "\\Teams");

I get feedback telling me it cannot find it. I even tried the Microsoft method: Directory.GetDirectories()

I am unable to get a sample of code that shows how to utilize it.

Ray
  • 7,940
  • 7
  • 58
  • 90
Microspec
  • 1
  • 1
  • 1
    `Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)` – Charlieface Jul 13 '23 at 11:01
  • Environment.SpecialFolder.ApplicationData is a numeric enum value, use the Environment.GetFolderPath method to get the actual path. – PaulF Jul 13 '23 at 11:02
  • 1
    Does this answer your question? [C# getting the path of %AppData%](https://stackoverflow.com/questions/867485/c-sharp-getting-the-path-of-appdata) – Charlieface Jul 13 '23 at 11:03
  • The question is unclear. `Powershell works no problem` Powershell is a .NET app so Delete works. The path you used isn't valid though even in Powershell. `Users*` is invalid. Powershell doesn't delete a path either. It *searches for matching files and deletes them one by one. You'll have to search for matching paths using `Directory.EnumerateFiles` or `Directory.GetFiles` and delete them – Panagiotis Kanavos Jul 13 '23 at 11:03
  • 1
    It's unclear what you want to delete though, or even if you should do it. You *definitely* shouldn't delete other users' and other applications' folders. In your own application, cleaning up is the job of the uninstaller or Windows itself. When you install an application, the installer actually tells Windows which files and paths were added so Windows can remove them during uninstall *or* repair them if they get deleted. – Panagiotis Kanavos Jul 13 '23 at 11:06
  • 1
    If you don't know how to use `Directory.GetFiles`, you **shouldn't** be thinking about deleting files for multiple users. There are thousands of examples, starting with the [documentation itself](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=net-7.0#system-io-directory-getfiles(system-string-system-string)). You'll end up destroying people's data, documents and settings. Windows goes to great lengths to prevent this, by requiring user consent – Panagiotis Kanavos Jul 13 '23 at 11:11
  • 1
    Can you please tell me the name of your app? I just want to make sure I never install or use it. – Thomas Weller Jul 13 '23 at 16:48

0 Answers0