Process.start on network drive, ask for authentication - c#

Considering this code:
Process process = new Process();
process.StartInfo.FileName = "explorer";
process.StartInfo.Arguments = "\\some_network_host\path";
process.Start();
I would like to connect to a shared resource and open the path in Explorer.exe, however, the user might not be authenticated yet. If the user is not authenticated, I would like to open a Windows authentication popup just like the one I see when I run \\some_network_host\path, however, my actual code just opens "My Document" instead (if the user is not already authenticated). If the user is already authenticated, it opens the explorer.exe window showing the shared resource.
Thank you.

This code works fine for me
Process process = new Process();
process.StartInfo.FileName = #"\\existing_network_host\path";
process.StartInfo.UseShellExecute = true;
process.StartInfo.ErrorDialog = true;
process.Start();
The keey difference is true value for StartupInfo.ErrorDialog

Related

How do I prevent ProcessStartInfo's print verb from opening a window?

I'm attempting to print off a file generated in my application using the following code:
var psi = new ProcessStartInfo("Temp.txt");
psi.UseShellExecute = true;
psi.Verb = "print";
psi.CreateNoWindow = true;
var process = System.Diagnostics.Process.Start(psi);
This works, and sends the file to the printer as expected. However, in the process, an instance of notepad opens for naught but a second. This is an application meant to run in the background, so those windows popping up are incredibly distracting.
Adding pi.WindowStyle = ProcessWindowStyle.Hidden; before the final line of the above snippet does help marginally. But still causes a brief, small popup - which drags the user out of any fullscreen application they may be in at the time.

Run new process as user interacting with UI

I have a GUI process that is running as nt authority\system. I'd like to launch another process (preferably via Process class) as the user that is interacting with the GUI process. If I just call Process.Start the new process will also run as nt authority\system but I want it to be domain\user.
Edit: for clarification, I don't have the current user's username or password. I just want to run the process as though the user was starting it themselves without having to ask for username/password.
Use StartInfo property with a valid credentials.
Process proc = new Process();
proc.StartInfo.Domain = "YourDomain";
proc.StartInfo.UserName = "Username";
proc.StartInfo.Password = "YourPassword";
You can do this:
Process proc = new System.Diagnostics.Process();
System.Security.SecureString ssPwd = new System.Security.SecureString();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "C:/YourPath/YourProcess.exe";
proc.StartInfo.Arguments = "Args"; //Arguments if any, otherwise delete this line
proc.StartInfo.Domain = "domainname";
proc.StartInfo.UserName = "username";
proc.StartInfo.Password = "password";
proc.Start();
var psi = new ProcessStartInfo();
psi.Verb = "runas";
psi.FileName = "notepad.exe";
Process.Start(psi);
'run as' argument while invoking a process with Process object, will ask for password of the user.
It is not possible to run a process as User account from a Local System Account.
You can do a workaround to solve your problem
Start a Main Process 'A' as the logged on user.
Now, start the required Local System Account 'B' process from the Main Process(A).
Now 'A' will monitor for a trigger from process B, if the main code (the code that must be run in user account) needs to be run. If the trigger hits, the required code can be run.
Monitoring can be done by monitoring(reading) a text file for every certain duration, and the trigger can be sent from B(system account process) which is writing to the text file. Any other better monitoring approach can be taken.

Auto Log Off when opening specific folder (Homework)

The code below logs off the PC. But what I want is that when I click on a specific folder, for example: E:\Picture, Windows should log off.
I don't know how to put such condition in the code. Can anyone point me in the right direction?
Code for logging of:
using (Process proc = new Process())
{
proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "shutdown.exe");
proc.StartInfo.Arguments = "-l";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
}
Since this is homework, here some starting points
Create a local hook on explorer.exe
Find out if you can use WinAPI to find process information on the explorer
...
execute shutdown.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644960(v=vs.85).aspx

C# System.Diagnostics.Process how to handle multiple batch files

I'm trying to start tomcat from my c# wpf app using the startup.bat and after deploying the war files, I want to close it. Here is how I've been doing it and it seemed to work fine.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = Tomcat_DIR_tbox.Text + #"\bin\startup.bat";
proc.StartInfo.WorkingDirectory = Tomcat_DIR_tbox.Text + #"\bin\";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
// do some bla bla
//Close Tomcat
proc.StartInfo.Filename = Tomcat_DIR_tbox.Text + #"\bin\shutdown.bat";
proc.Start();
proc.WaitForExit();
Now I've been thinking that although it works, it's not quite right ? the Tomcat application started never stops unless I tell it to, it feels wrong to use the same process to also shut it down ?
What I've down now is something like this : Kept the process for starting up tomcat, and created another one which calls for the shutdown:
//Close tomcat
System.Diagnostics.Process proc_shutdown = new System.Diagnostics.Process();
proc_shutdown.StartInfo.WorkingDirectory = Tomcat_DIR_tbox.Text + #"\bin\";
proc_shutdown.StartInfo.FileName = Tomcat_DIR_tbox.Text + #"\bin\shutdown.bat";
proc_shutdown.StartInfo.CreateNoWindow = true;
proc_shutdown.Start();
proc_shutdown.WaitForExit();
The only thing I don't like about this method is that it brings a message which asks me if I allow the shutdown.bat to be called from my application, which I can uncheck "Don't ask me again for this file" but I still don't like the user experience it provides.
I also tried calling directly proc.Close() instead of doing proc.Filename = path_to_shutdown and proc.Start(), but it seemed to hang up my application and never close tomcat.
So what is the best way to start tomcat, do some stuff and then close it up ? Thanks , Razvan.
Based on my observation, what you are doing is fine to start and stop the tomcat server from WPF application. But, if you want to hide the security warning, Then Open Internet Explorer Go to Tools -> Internet Options and then select "security" tab from there select (Custom level..).. In that in the Miscellaneous group there will be option called
"Launching Applications and Unsafe files" -> Select Enable Radio button. Then it won't show you the "allow the shutdown.bat" popup.
Hope, it helps.

Process.Start creates a pop window

I've an exe which runs a process to open the DeviceManager. But unfortunately, it asks for a confirmation to provide 'yes' or 'No' which waits for user input for long time and does not continue with execution.
How to get rid of this? I do not want to provide a confirmation again as I do not want to pause the EXE run with this.
StartInfo.CreateWindow = false would not hold for this as it just for starting in another cmd window.
Code below:
Process p = new Process();
p.StartInfo.FileName = "devcon.exe";
p.StartInfo.CreateNoWindow = false;
p.Start();
The messagebox you are seeing is UAC (User Account Control) which was implemented since Vista.
To bypass the box you might be able to try providing the credentials programmatically before launching the process, I can't test but something like this:
var processInfo = new ProcessStartInfo
{
FileName = "devcon.exe",
UserName = "Administrator",
Domain = "yourdomain or leave blank",
Password = adminpassword,
UseShellExecute = false,
};
Process.Start(processInfo);
Otherwise the user will have to have admin rights, or the password!
The other option would be to disable UAC. However that wouldn't allow the user to do anything they couldn't do normally, it will probably tell you that you can't make any changes without the process running as admin.
The parent process should be run by administrator account.
Also show all code please.

Categories