-2

I am trying to make an application that deletes a folder and I cannot seem to find a way to find what user they are on and implement it into the directory deleting. I am coding in c# on Visual Studio

edit: os: windows 10 basically I want to find the username of the computer and set it as a variable than go and delete a certain folder inside of C:/users/(user)

I have a way to get the user which is Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); But I want to set that as a variable and then use that variable to replace

            DirectoryInfo di = new DirectoryInfo(@"C:\Users\Desktop 
            Path\yes");
            foreach(FileInfo file in di.GetFiles())
            {
                 file.Delete();
            }
bzostlol
  • 1
  • 1
  • Does this answer your question? [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) – Nigel Dec 14 '21 at 22:09
  • It's unclear what exactly you're trying to do...is it possibly to find the "home" directory for the current user? If so, [this](https://stackoverflow.com/questions/1140383/how-can-i-get-the-current-user-directory) might help. There are different options in `Environment.SpecialFolder` that you can try until you hopefully find the one you're wanting. If that's not what you're trying to do, could you please edit your question and add more information, for example what OS you're on, a sample of exactly what you're trying to do, and some of the code you've tried...any or all of that would be good – David784 Dec 14 '21 at 22:13
  • _"find a way to find what user they are on and implement it into the directory deleting"_ I have no idea what that means. Please [edit] the question and provide more details along with your attempt to solve the problem. – 41686d6564 stands w. Palestine Dec 14 '21 at 22:13
  • ok i edited it so you can hopefully understand – bzostlol Dec 14 '21 at 22:37
  • 1
    What *exactly* is preventing you from storing the return from `Environment.GetFolderPath()` to a variable? Additionally, there are **numerous** examples here already – Ňɏssa Pøngjǣrdenlarp Dec 14 '21 at 22:48
  • nothing is preventing me, I don't know how to put that variable into the path @ŇɏssaPøngjǣrdenlarp – bzostlol Dec 14 '21 at 22:49

1 Answers1

1

I don't know why would you need to get the user, but ok. You already have the solution using Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); that will get you something like "C:\Users\randomUserName". You can store that in a variable. Then append whatever folder path you need using Path.Combine, you don't really need to use that function but it's best practice and it just saves you so much trouble. So the final product will be something like this.

var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var path = Path.Combine(userPath, "you path here");
var dirInfo = new DirectoryInfo(path);

//Rest of your code here.