Open all .exe files in a directory c# - c#

I would like to run all the .exe files in a certain directory from my winform application.
Is there a way not to hard code the file path of each exe and be not dependent on knowing beforehand how many exe I have to run?

DirectoryInfo d = new DirectoryInfo(filepath);
foreach (var file in d.GetFiles("*.exe"))
{
Process.Start(file.FullName);
}

You can get all executable files using:
static public List<string> GetAllExecutables(string path)
{
return Directory.Exists(path)
? Directory.GetFiles(path, "*.exe").ToList()
: return new List<string>(); // or null
}
You can run one with:
static public Process RunShell(string filePath, string arguments = "")
{
try
{
var process = new Process();
process.StartInfo.FileName = filePath;
process.StartInfo.Arguments = arguments;
process.Start();
return process;
}
catch ( Exception ex )
{
Console.WriteLine($"Can''t run: {filePath}{Environment.NewLine}{Environment.NewLine}" +
ex.Message);
return null;
}
}
Thus you can write:
foreach ( string item in GetAllExecutables(#"c:\MyPath") )
RunShell(item);

//get exe files included in the directory
var files = Directory.GetFiles("<target-directory>", "*.exe");
Console.WriteLine("Number of exe:" + files.Count());
foreach (var file in files)
{
// start each process
Process.Start(file);
}

Related

Mimic Linux which command in C#

I use a tool's binaries in a C# project called GraphViz.
The problem is I must include the binaries path as hard-coded and I don't want to do that.
IRenderer renderer = new Renderer("C:\\Program Files (x86)\\Graphviz2.38\\bin"); // todo: remove hardcoded GraphViz path
I want to mimic the linux which command.
Simply passing the binary name (e.g dot) and get the path.
GetBinaryPath("dot"); // return the above path
I'd appreciate any ideas or topics to start searching.
Note
Target OS: Windows
.NET version : 4
If you need to find path given only executable name (and installation directory is in your PATH environment variable)
Option 1:
Using where command with Process class. (test for exit code, parse the output)
Option 2:
You can get environment PATH environment variable, split it by ';' and test for your executable name existence.
First you need to find all directories where windows search for a exectuable file and that is from the environment variable %PATH%.
Then you need to find all extensions (.com, .exe, .bat etc) from %PATHEXT%.
Then you just check them like this:
internal class Program {
private static void Main(string[] args) {
if (args.Length != 1) {
Console.WriteLine("Incorrect usage!");
return;
}
var extensions = GetExecutableExtensions(args[0]);
var paths = GetPaths();
var exeFile = GetFirstExecutableFile(args[0], extensions, paths);
if (exeFile == null) {
Console.WriteLine("No file found!");
}
else {
Console.WriteLine(exeFile);
}
}
private static string GetFirstExecutableFile(string file, string[] extensions, string[] paths) {
foreach (var path in paths) {
var filename = Path.Combine(path, file);
if (extensions.Length == 0) {
if (File.Exists(filename)) {
return filename;
}
}
else {
foreach (var ext in extensions) {
filename = Path.Combine(path, file + ext);
if (File.Exists(filename)) {
return filename;
}
}
}
}
return null;
}
private static string[] GetExecutableExtensions(string file) {
var data = GetCmdOutput("echo %PATHEXT%");
var arr = data.TrimEnd('\n', '\r').Split(new [] {';'}, StringSplitOptions.RemoveEmptyEntries);
//If the command passed in ends with a executable extension then we dont need to test all extensions so set it to emtpy array
foreach (var ext in arr) {
if (file.EndsWith(ext, StringComparison.OrdinalIgnoreCase)) {
return new string[0];
}
}
return arr;
}
private static string[] GetPaths() {
var data = GetCmdOutput("echo %PATH%");
return data.TrimEnd('\n', '\r').Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
}
private static string GetCmdOutput(string cmd) {
using (var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "cmd.exe",
Arguments = "/c " + cmd,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
}) {
proc.Start();
return proc.StandardOutput.ReadToEnd();
}
}
}

When i try to delete report from the folder this error comes

The process cannot access the file because it is being used by another process
private void DeleteReport()
{
int invid = Convert.ToInt32(Session["InvId"]);
string FileName = invid + "_Report" + ".pdf";
string path1 = Server.MapPath("~/Report/" + FileName);
if (File.Exists(path1))
{
File.Delete(path1);
}
}
The error tells you, that the file is used and can't be deleted. So nothing wrong. As you did not formulate a
real question, lets try to help you in following way.
I guess that only your program is using the report, so good possible, you block the report
somewhere else.
E.g., the following code
string path = "C:\\Temp\\test.txt";
FileStream file = File.Open(path, FileMode.OpenOrCreate);
if (File.Exists(path))
File.Delete(path);
raises the same error. It does not necessarily mean that the process is another process.
What you can do is for example, for testing purpose, install SysInternal
http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx and add following code around your
File.Delete statement. Then you will see, what process uses the file:
try
{
File.Delete(path);
}
catch (Exception)
{
using (Process tool = new Process())
{
tool.StartInfo.FileName = #"C:\Program Files (x86)\SysinternalsSuite\handle.exe"; //Your path
tool.StartInfo.Arguments = path + " /accepteula";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = #"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach (Match match in Regex.Matches(outputTool, matchPattern))
{
Process p = Process.GetProcessById(int.Parse(match.Value));
MessageBox.Show(p.ProcessName); // OR LOG IT
}
}
throw;
}
Credit for handle.exe call to https://stackoverflow.com/a/1263609/2707156

How can I delete the contents a directory, without deleting the directory itself? Here's what I've tried so far

I'm making a button that reads a file path then deletes the files within the folder. It's currently deleting the entire directory. Here's my code:
public void deleteFiles(string Server)
{
string output = "\\\\" + Server + "\\F\\Output";
string input = "\\\\" + Server + "\\F\\Input";
string exceptions = "\\\\" + Server + "\\F\\Exceptions";
new System.IO.DirectoryInfo(input).Delete(true);
new System.IO.DirectoryInfo(output).Delete(true);
new System.IO.DirectoryInfo(exceptions).Delete(true);
}
Would just deleting the directory and recreating it work? Or do you want to preserve permissions?
You are calling Delete method on DirectoryInfo, you should call it on FileInfo's instead:
var files = new DirectoryInfo(input).GetFiles()
.Concat(new DirectoryInfo(output).GetFiles())
.Concat(new DirectoryInfo(exceptions).GetFiles());
foreach(var file in files)
file.Delete();
Another way:
var files = Directory.GetFiles(input)
.Concat(Directory.GetFiles(output))
.Concat(Directory.GetFiles(exceptions));
foreach(var file in files)
File.Delete(file);
DirectoryInfo.Delete and Directory.Delete delete empty directories, if you want to delete files you could try this method:
public void DeleteFiles(string path, bool recursive, string searchPattern = null)
{
var entries = searchPattern == null ? Directory.EnumerateFileSystemEntries(path) : Directory.EnumerateFileSystemEntries(path, searchPattern);
foreach(string entry in entries)
{
try
{
FileAttributes attr = File.GetAttributes(entry);
//detect whether its a directory or file
bool isDir = (attr & FileAttributes.Directory) == FileAttributes.Directory;
if(!isDir)
File.Delete(entry);
else if(recursive)
DeleteFiles(entry, true, searchPattern);
}
catch
{
//ignore
}
}
}

How do I test in C# if an exe is running correctly?

I have a viewer.exe that loads at startup some models (*.mdl) from a "models" folder. Some of the models crash viewer.exe: "viewer.exe has stopped working. Windows can check online for a solution to the problem".
What I could do is move all the .mdl files in a "source" folder and then manually test for each .mdl file moved to "models" if viewer.exe is running but, there are a lot of files to check. How do I move each *.mdl file from "source" to "models" and test programmatically if viewer.exe is running correctly?
Here is the code I use for my first problem: to move the .mdl files from "source" folder sub-directories in "models". Some of the files had identical names but different size:
String mask = "*.mdl";
String source = #"c:\Source\";
String destination = #"c:\Models\";
String[] files = Directory.GetFiles(source, mask, SearchOption.AllDirectories);
foreach (String file in files)
{
if (File.Exists(file) && !File.Exists(destination + new FileInfo(file).Name))
{
File.Move(file, destination + new FileInfo(file).Name);
}
else
{
FileInfo f = new FileInfo(file);
long s = f.Length;
FileInfo f2 = new FileInfo(destination + new FileInfo(file).Name);
long s2 = f2.Length;
if (s >= s2)
{
File.Delete(destination + new FileInfo(file).Name);
File.Move(file, destination + new FileInfo(file).Name);
}
}
}
use process.start(startInfo) (see http://msdn.microsoft.com/en-gb/library/0w4h05yb.aspx)
Wait a few seconds, check if the process has terminated, then the returned process.hasexited (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx)
then kill it anyway using process.kill() (see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx)
You might need to turn off windows error reporting: http://msdn.microsoft.com/en-us/library/bb513638(VS.85).aspx
Surround the operations which can fail in try-catch statements
try {
File.Delete(destination + new FileInfo(file).Name);
} catch (Exception ex) {
// File could not be deleted
}
try {
File.Move(file, destination + new FileInfo(file).Name);
} catch (Exception ex) {
// File could not be moved
}
In the catch statement do whatever you want to do in case the files could not be processed.
I have disabled windows error reporting and this is how the program looks like now:
String mask = "*.mdl";
String source = #"c:\source\";
String destination = #"C:\Viewer\Models\";
String[] files = Directory.GetFiles(source, mask, SearchOption.AllDirectories);
foreach (String file in files)
{
if (File.Exists(file) && !File.Exists(destination + new FileInfo(file).Name))
{
File.Move(file, destination + new FileInfo(file).Name);
}
else
{
FileInfo f = new FileInfo(file);
long s = f.Length;
FileInfo f2 = new FileInfo(destination + new FileInfo(file).Name);
long s2 = f2.Length;
if (s >= s2)
{
File.Delete(destination + new FileInfo(file).Name);
File.Move(file, destination + new FileInfo(file).Name);
}
}
//mycompiledapp.exe is placed in Viewer folder for this to work
Process myprocess = Process.Start(#"viewer.exe");
Thread.Sleep(3000);
if (myprocess.HasExited) //Process crashes, exiting automatically
{
//Deletes the file that makes the viewer.exe crash
File.Delete(destination + new FileInfo(file).Name);
}
else
{
myprocess.Kill();
}
}

EnumerateFiles with subdirectories in Mono

I am trying Mono on a linux ubuntu.
Enumartion of Files wont work with subdirectories. If i use GetFiles() it will work.
I used a console to get the output of the files so I wont get the enumerat output:
[Test]
public void NotImportentTest ()
{
Directory.CreateDirectory("test");
Directory.CreateDirectory("test/dings");
File.Create("test/dings/hallo").Close();
var curdir = Directory.GetCurrentDirectory();
var searchdir = new DirectoryInfo(curdir + "/test");
var files = searchdir.EnumerateFiles("*",SearchOption.AllDirectories);
foreach (var file in files)
{
Console.WriteLine("Enumerate:{0} in {1}\r\n", file, curdir);
}
foreach (var file in searchdir.GetFiles("*",SearchOption.AllDirectories))
{
Console.WriteLine("GetFiles:{0} in {1}\r\n", file, curdir);
}
}

Categories

Resources