How to Perform File operations using custom user credentials in C# - c#

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;
}
}

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
Navigate to the file directory where the .exe file for the program is
Press Shift + Right Click on the .exe file
Click "Run as a different user"
Enter in the desired credentials, then click OK
Running Visual Studio As A Different User
Type Visual Studio in the start menu
Right Click on the desired version of Visual Studio
Click "open file location"
Find the shortcut for the desired version of Visual Studio
Press Shift + Right Click on the shortcut
Click "Run as a different user"
Enter in the desired credentials, then click OK
Continue to debug or run the program using Visual Studio

Related

How to fix browser not opening in debug mode?

I am unable to start my web add-in in google chrome or any other browser I clicked on start button which have written IIS Express instead of start it goes into debug mode after clicking the button but nothing happens.
Here is screenshot before starting :
after start mode :
One reason is the your visual studio is not run as ADMIN. You must run visual studio with Administrator Privilege's - to do that on the short cut icon of Visual studio, right click and "run as administrator" - or for permanently check that go to Properties, Click Advanced, then click "Run as administrator" - When you run it this way you see a Label "ADMIN" on the right up corner of the VS.
Now if this is not work, alternative you can run get into the debugger using this call System.Diagnostics.Debugger.Launch();
Create an empty page and make this call on PageStart, then just load this page from your browser
System.Diagnostics.Debugger.Launch();
Check this setting in your web project's settings and ensure it's not set to "don't open a page"
and the reason behind the HTTP Error 403.14 - Forbidden error is the directory browsing feature is turned off. you can enable by following below steps:
Open a command prompt, and then go to the IIS Express folder on your computer. For example, go to the following folder in a command prompt:
C:\Program Files\IIS Express
Type the following command, and then press Enter:
appcmd set config /section:directoryBrowse /enabled:true
In VS2022 you can check/uncheck a flag to launch the web project in the default browser.
To do this, right click on the relevant project in your solution and click Properties. In the menu on the left click Debug, then 'Open debug launch profiles UI'.
In the dialog which appears, select the relevant profile on the left pane, then scroll down to 'Launch browser'. Check/uncheck this option as required.
Alternatively, open the Properties/launchSettings.json file within the project and add the following line under the relevant profiles entry:
"launchBrowser": true,

Execute Setup with admin rights

I have a windows forms app developed in c# and have created a setup file for it.
.Net Framework used is 3.0.
Now, setup should force for admin rights for installation and then proceed else not.
I found articles on creating manifest file which works fine but when program is launched after installation it again asks for admin rights which is not i want.
And this prompt should also work on Windows XP SP 1 and above OS.
To prompt Installer to invoke the UAC prompt to the user you can do the following:
Right click your Setup Project > View > Launch Conditions.
Right click Launch Conditions > Add a new Condition in your Launch Conditions.
Right click the Condition, choose Properties Window.
Set Condition to AdminUser. (You can also try setting Privileged property)
Build and install
OR
Edit your .vdproj, change requiresElevation attribute to true
"MsiBootstrapper"
{
"RequiresElevation" = "11:TRUE"
}

Programmatically monitoring which tab is opened in visual studio

Let's start with an example, User have several tabs opened for a solution>project in Visual Studio (Default.aspx, Default.aspx.cs) I programmatically want to monitor devenv.exe process and want to know which tab (file to be exact) user is working on. If there's some log file or process object has some attribute that tells? Or it has to be done with image processing?
You can write an extension for Visual Studio and monitor DTE.Events.WindowEvents.WindowActivated. From an active window you can get Document.FullName for a file user is working on.
See for example Display document path of the active window in the status bar.

Run Wpf application from CMD/Run like calc?

I want to provide my WPF application a custom command so I can start up it from command prompt by not writing it's name but by writing specific command like calc or appwiz.cpl. I have searched google for the same but it is taking me wrong like almost concepts are of command line argument but I am not looking for the same. Is there any possibility to start a custom application from command line or run utility.
Thanks in advance.
Yes. That can be done easily using Windows Registry. You can open any desired 3rd party program by just typing its name in RUN or Start menu Search box and press Enter.
You'll need to set the application path in Windows Registry so that Windows can know about the application at the time of execution.
Type regedit in RUN or Start Menu search box and press Enter. It'll
open Registry Editor.
Now go to:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
Now we'll need to create a new key under "App Paths" key. Right-click on "App Paths" key and select "New -> Key". Set its name to your desired application name along with its extension e.g. My_Application.exe
Select the key created in Step 3 and in right-side pane, set value of "Default" to the full path of application's EXE file e.g. C:\Program Files\My Application\My Application.exe
Again in right-side pane, create a new String value "Path" and set its value to the folder containing your application EXE file e.g. C:\Program Files\My Application\
That's it. Now you can launch your desired application by just typing its name in RUN or Start menu Search box.
NOTE: If you want a ready-made registry script to do the task automatically, then copy paste following code in Notepad and save the file with name "AskVG.REG" (including quotes).
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\My Application]
#="C:\\Program Files\\My Application\\My Application.exe"
"Path"="C:\\Program Files\\My Application\\"
You just need to replace BOLD part of the above script with the correct application name and path. After saving the file, run it and it'll add the program path to Registry so that you can launch it directly from RUN and Start menu Search box.
Edit:
Based on your comment you can write the following action with C# code

How to attach the debugger as soon as a file is double-clicked?

As most applications, mine opens files in 2 ways:
(1) Via the "File | Open" menu. This code is fully debugged.
(2) By double-clicking a file whose extension is associated to my application. This part needs some debugging.
What is the process to debug the latter program execution path?
Is there a way to place a statement like: CallDebugger(); ???
TIA
You could try by setting your application to Launch the Debugger Automatically.
To setup an application to launch the debugger automatically
Start the Registry Editor (regedit).
In the Registry Editor, open the HKEY_LOCAL_MACHINE folder.
Navigate to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options.
In the Image File Execution Options folder, locate the name of the application you want to debug, such as myapp.exe. If you cannot find the application you want to debug:
Right-click the Image File Execution Options folder, and on the shortcut menu, click New Key.
Right-click the new key, and on the shortcut menu, click Rename.
Edit the key name to the name of your application; myapp.exe, in this example.
Right-click the myapp.exe folder, and on the shortcut menu, click New String Value.
Right-click the new string value, and on the shortcut menu, click Rename.
Change the name to debugger.
Right-click the new string value, and on the shortcut menu, click Modify.
The Edit String dialog box appears.
In the Value data box, type vsjitdebugger.exe.
Click OK.
From the Registry menu, click Exit.
The directory containing vsjitdebugger.exe must be in your system path. To add it to the system path, follow these steps:
Open the Control Panel in Classic view, and double-click System.
Click Advanced System Settings.
In System Properties, click the Advanced tab.
On the Advanced tab, click Environment Variables.
In the Environment Variables dialog box, under System variables, select Path, then click the Edit button.
In the Edit System Variable dialog box, add the directory to the Variable value box. Use a semicolon to separate it from other entries in the list.
Click OK to close the Edit System Variable dialog box.
Click OK to close the Environment Variables dialog box.
Click OK to close the System Properties dialog box.
Now, use any method to start your application. Visual Studio will start and load the application.
Source: http://msdn.microsoft.com/en-us/library/a329t4ed(v=vs.100).aspx
Note:
You may need to run your application As Administrator to succesfully launch the debugger.

Categories