Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Using C# VS2013 Ultimate


C#
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();
           }
Posted
Updated 27-Oct-15 2:33am
v3
Comments
Philippe Mori 27-Oct-15 12:05pm    
Such requirement absolutly make no sense at all. Are you writing virus...

Questions such as this can be easily reseached using Google. See https://www.google.com/search?q=logout+windows+C#[^].
 
Share this answer
 
Comments
Malee-Ha 27-Oct-15 8:32am    
I did but wasn't able to find what I specifically needed...!!
F-ES Sitecore 27-Oct-15 8:36am    
It's hard to know what you specifically need as you haven't explained.

http://stackoverflow.com/questions/14466373/log-off-a-windows-user-locally-using-c-sharp
Richard MacCutchan 27-Oct-15 9:32am    
Well you are not going to find the exact code. But there are samples of how to logoff from within a C# application. Look again.
Malee-Ha 27-Oct-15 9:44am    
Yes I do have that but I need alteration in the code which I'm not able to do.
http://www.codingdefined.com/2014/07/lock-logoff-restart-shutdown-computer.html
Can you please go through this code and tell me how to create acc. to my need?
Richard MacCutchan 27-Oct-15 10:02am    
What do you mean? The sample code is complete, you just add it to the section of your application at the appropriate place.
If you already have the code you need to log-off the current user then all you need to monitor the directory or directories you desire is to use a FileSystemWatcher.

See this link:
https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

Set your filters accordingly

watcher.Path = "C:\\SomeFolder";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;

Capture the the LastAccess in your event handler and execute your log-off code.
 
Share this answer
 
v2
Comments
Zoltán Zörgő 27-Oct-15 14:14pm    
Won't work. FileSystemWatcher has no event for "accessing" only for creation, deletion and modification.
Altrough your question makes little sense, shutting down windows is not complicated at all, you have got several answers in the posted comments.
The interesting part is to get notified about file operation. File system watchers are just not for that. You could enable object access auditing[^] and watch for events in the event log. But you can also monitor file handles, like here: http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318[^]
 
Share this answer
 
You have no way of knowing if someone "clicks on a certain folder".

What you ask does not make any sense. Why would you want to do this anyway?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900