Auto Log Off when opening specific folder (Homework) - c#

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

Related

how can I execute a shortcut(.lnk) using Process.Start()? [duplicate]

Is there a way to run an application via shortcut from a C# application?
I am attempting to run a .lnk from my C# application. The shortcut contains a significant number of arguments that I would prefer the application not have to remember.
Attempting to run a shortcut via Process.Start() causes an exception.
Win32Exception: The specified executable is not a valid Win32 application
This is the code I am using.
ProcessStartInfo info = new ProcessStartInfo ( "example.lnk" );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
Process whatever = Process.Start( info );
Could you post some code. Something like this should work:
Process proc = new Process();
proc.StartInfo.FileName = #"c:\myShortcut.lnk";
proc.Start();
Setting UseShellExecute = false was the problem. Once I removed that, it stopped crashing.
if your file is EXE or another file type like ".exe" or ".mkv" or ".pdf" and you want run that with shortcut link your code must like this.
i want run "Translator.exe" program.
Process.Start(#"C:\Users\alireza\Desktop\Translator.exe.lnk");
If you're using UseShellExecute = false and trying to launch a batch file make sure to add .bat to the end of the filename. You don't need .bat if UseShellExecute = true though. This made me just waste an hour of work... hoping to save someone else.

Process.start on network drive, ask for authentication

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

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.

How to use process.StartInfo.Arguments in command prompt?

I wanted to use my c# visual studio to open command prompt using process.StartInfo.FileName and process.StartInfo.Arguments to give commands automatically however this does not work out. Anyone knows how to do this? ( I am using the cmd to call for my python script )
Try using the following. Hope this helps. Replace notepad.exe with something else you like.
Process p = new Process();
p.StartInfo = new ProcessStartInfo("cmd.exe", "/c notepad.exe");
p.Start();
Please refer to http://ss64.com/nt/cmd.html for more details.
This opens notepad and a file called foo
var procInfo = new ProcessStartInfo("notepad");
procInfo.Arguments = "foo.txt";
var proc = new Process();
proc.StartInfo = procInfo;
proc.Start(); //Actually executes the process
proc.WaitForExit(); //Waits until the process completes, in this case, when you close notepad
So for your thing you would call the python interpreter and pass the python script as arguments, along with anything else the interpreter needs (I have 0.1% knowledge of python)
Important note: Make sure your executable is in the path.

Mimic Windows' 'Run' window in .NET

I would like to mimic the Run command in Windows in my program. In other words, I would like to give the user the ability to "run" an arbitrary piece of text exactly as would happen if they typed it into the run box.
While System.Diagnostics.Process.Start() gets me close, I can't seem to get certain things like environment variables such as %AppData% working. I just keep getting the message "Windows cannot find '%AppData%'..."
You can use the Environment.ExpandEnvironmentVariables method to turn %AppData% into whatever it actually corresponds to.
Depending on what you're trying to do, you could also call CMD.EXE, which will expand your environment variables automatically. The example below will do a DIR of your %appdata% folder, and redirect the stdOut to the debug:
StreamReader stdOut;
Process proc1 = new Process();
ProcessStartInfo psi = new ProcessStartInfo("CMD.EXE", "/C dir %appdata%");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
proc1.StartInfo = psi;
proc1.Start();
stdOut = proc1.StandardOutput;
System.Diagnostics.Debug.Write(stdOut.ReadToEnd());

Categories