0

I am creating a console application to create a file by checking if the directory and file name exist, my console application will be running in administration mode but I want to perform file exists and file create operations by passing custom users' credentials as input.

if (Directory.Exists(directory))
{
    string fullPath = directory + "\demo.txt"
    try
    {
       
        if (File.Exists(fullPath))
        {
            File.Delete(fullPath)
        }
       // here will be my file creation code by taking the custom user credentials as input

    }
    catch (Exception)
    {
        success = false;
    }
}
  • 1
    you need to [Impersonate](https://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net/7250145#7250145) the user – J.Salas Jan 05 '23 at 09:47
  • https://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net – Schwarzie2478 Jan 05 '23 at 10:56
  • Does this answer your question? [How do you do Impersonation in .NET?](https://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net) – Schwarzie2478 Jan 05 '23 at 10:56

1 Answers1

0

You can either directly run the executable as a different user or run Visual Studio as a different user and input the desired credentials.

Running Executable As A Different User

  1. Navigate to the file directory where the .exe file for the program is
  2. Press Shift + Right Click on the .exe file
  3. Click "Run as a different user"
  4. Enter in the desired credentials, then click OK

Running Visual Studio As A Different User

  1. Type Visual Studio in the start menu
  2. Right Click on the desired version of Visual Studio
  3. Click "open file location"
  4. Find the shortcut for the desired version of Visual Studio
  5. Press Shift + Right Click on the shortcut
  6. Click "Run as a different user"
  7. Enter in the desired credentials, then click OK
  8. Continue to debug or run the program using Visual Studio
YungDeiza
  • 3,128
  • 1
  • 7
  • 32