I was just looking for a simple script in which checks if explorer.exe is running. If it is, then kill it and restart it. However, if it is not running, then start it.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
public partial class App
{
public static void Main()
{
Process[] prcChecker = Process.GetProcessesByName("explorer");
if (prcChecker.Length > 0)
{
foreach (Process p in prcChecker)
{
p.Kill();
}
}
else if (prcChecker.Length == 0)
Process.Start("explorer.exe");
}
}
I haven´t tested it but maybe it´s something like this you´re looking for
//to kill a process
foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}
//to start
Process.Start("explorer.exe", "");
Related
I want to monitor some directory using FileSystemWatcher but FileSystemWatcher.created is not working at all .
CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Threading;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static string fileName;
static string dirFileName;
static void Main(string[] args)
{
string _Dir = ConfigurationManager.AppSettings["Dir"];
Console.WriteLine("MAIDProcessFileTest processing started");
FileSystemWatcher _watch = new FileSystemWatcher();
_watch.Path = _Dir;
_watch.Created += FileCreated;
Console.ReadLine();
}
private static void FileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Before is treat");
fileName = Path.GetFileName(e.FullPath);
dirFileName = Path.GetDirectoryName(e.FullPath) + #"\" + Path.GetFileName(e.FullPath);
}
}
}
Can anybody tell me that why this code is not working ... whenever I debug _watch.Created += FileCreated; is not working at all. where is the error can anybody tell me . Please Help
You need to start it
_watch.EnableRaisingEvents = true;
FileSystemWatcher.EnableRaisingEvents Property
You should probably start here though
FileSystemWatcher Class
It has a great example, and everything you need to know to use it
I am working on collecting urls from the web site in C# using WatiN framework. In my program it is fetching only one url. I don't know what is the problem. Any help will be appreciated.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
namespace magicbricks
{
class scroll
{
[STAThread]
static void Main(string[] args)
{
Browser browserInstance;
browserInstance = new IE(#"http://www.99acres.com/property-in-chennai- ffid?search_type=QS&search_location=CP32&lstAcn=CP_R&lstAcnId=32&src=CLUSTER&isvoicesearch=N&keyword_suggest=chennai%20%28all%29%3B&fullSelectedSuggestions=chennai%20%28all%29&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiY2hlbm5haSAoYWxsKSIsIkNJVFlfMzIsIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=chennai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_32%2C%20PREFERENCE_S%2C%20RESCOM_R");
foreach (var links in browserInstance.Links.Filter(Find.ByClass("b")))
{
Console.WriteLine(links.Url);
String filePath = "C:/Users/User/Desktop/New folder";
String fileName = "newop4.csv";
using (StreamWriter sr = new StreamWriter(Path.Combine(filePath, fileName), true))
{
sr.WriteLine(links.Url);
}
Console.ReadLine();
}
}
}
}
the above code prints only one url in the console.
Remove the Console.ReadLine(); As you are in a ForEach loop. If you still want the Console.ReadLine(); move it out the foreach
The Console.ReadLine(); waits for a user input, after you enter any value you should see the next URL.
I created a new class for the testing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;
using System.Diagnostics;
using DannyGeneral;
using System.Windows.Forms;
using System.Threading;
using System.Management;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
namespace HardwareMonitoring
{
class CpuUsages
{
public static string processes;
public static string cputest()
{
PerformanceCounter cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
var unused = cpuCounter.NextValue(); // first call will always return 0
System.Threading.Thread.Sleep(1000); // wait a second, then try again
//Console.WriteLine("Cpu usage: " + cpuCounter.NextValue() + "%");
processes = "Cpu usage: " + cpuCounter.NextValue() + "%";
return processes;
}
}
}
Then in form1 i added a new timer set it to 1000ms enable it when running the program and inside the timer tick event i did:
private void timer3_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
CpuUsages.cputest();
cpuusage = CpuUsages.processes;
label26.Text = cpuusage;
}
}
This way it's working very slow take a long time to make the loop foreach.
In general i wanted to loop over each running process and get it's cpuusage.
But if i remove the foreach loop like this:
private void timer3_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcesses();
CpuUsages.cputest();
cpuusage = CpuUsages.processes;
label26.Text = cpuusage;
}
Then it will work fast i will see in label26 the cpuusage updating eavery second.
The problem is that it will show the cpuusage for only on process.
What can i do to solve it ?
In general i wanted to create automatic number of labels for each process in the list and display each process cpuusage. But it's so slow and take so long when i use the foreach loop.
Is there any way to solve it ?
This:
foreach (Process process in processes)
{
CpuUsages.cputest();
cpuusage = CpuUsages.processes;
label26.Text = cpuusage;
}
will make your program sleeps for 1 second * (number of processes running on your machine). No wonder the foreach loop is slow.
Remove those Sleep calls, and have your loop running in another Thread, avoiding slowing down the User interface.
Also I don't see why you are iterating over the processes returned by Process.GetProcesses() : you are not using them.
I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?
I am getting all websites from localhost IIS manager 6 using DirectoryEntry class, I would like to get local path of each web application, but not sure how to get it, Is there anyway I can enumerate all properties of directory entry ?
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
Console.WriteLine(e.Properties["ServerComment"].Value.ToString());
// how can I enumerate all properties and there values here ?
// maybe write to a xml document to find the local path property
I think you can use following code to find what you need. In case other questions just use the same approach. This code works for IIS6.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
namespace IIS_6
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
string VirDirSchemaName = "IIsWebVirtualDir";
foreach (DirectoryEntry e in root.Children)
{
foreach (DirectoryEntry folderRoot in e.Children)
{
foreach (DirectoryEntry virtualDirectory in folderRoot.Children)
{
if (VirDirSchemaName == virtualDirectory.SchemaClassName)
{
Console.WriteLine(String.Format("\t\t{0} \t\t{1}", virtualDirectory.Name, virtualDirectory.Properties["Path"].Value));
}
}
}
}
}
}
}
With regards to IIS 7 I wrote this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// to add it from %windir%\System32\InetSrv\Microsoft.Web.Administration.dll
using Microsoft.Web.Administration;
namespace IIS_7
{
class Program
{
static void Main(string[] args)
{
using (ServerManager serverManager = new ServerManager())
{
foreach (var site in serverManager.Sites)
{
Console.WriteLine(String.Format("Site: {0}", site.Name));
foreach (var app in site.Applications)
{
var virtualRoot = app.VirtualDirectories.Where(v => v.Path == "/").Single();
Console.WriteLine(String.Format("\t\t{0} \t\t{1}", app.Path, virtualRoot.PhysicalPath));
}
}
}
}
}
}