Got stuck thinking in an idea that i got - c#

First of all, sorry that I was unable to give a proper title.
I got stuck with an idea that's been with me today almost the whole day after searching and searching and searching, till it came to a point that I decided to ask it on Stackoverflow!
So here's where I am stuck:
(I am making an auto-installer currently coded in C# and it is Dutch. It works really awesome but I just need one thing to finish my base. For example:
You have 'multiple' objects selected in a checklistbox, those are read from the checklistbox itself, they get trimmed and they get launched after that.
Now that's all working, I wanted to add a waiting method, for example we got:
Malwarebytes & CCleaner as installation 'example'.
Now when both are checked, and I click start, it starts both of the programs.
What I want to do is: to tell the program to start one program, do your thing, once its finished (closed) it should go to the next.
But... There is a problem, my programs are started in an array, so it basically works if there are multiple objects checked, than it will start all of the checked objects. And I really have no idea how to reach the same thing which is basically :
If there are multiple objects selected, start the object(s), do your thing(auto-clicking etc.),once its closed and confirmed its closed, move on to the next object and do the same thing until its been completed. I would like to make it work with a progressbar, but never really looked into a progress bar as they seem confusing.
I have a piece of code that finds the Process ID so maybe I can do something with that, but the Process ID is never the same on the applications that I start, so when they start in an array I got kinda of an issue.
Could someone help me please figuring out what & how to code / do this?
here's the code i use to make this work :
string pad = Application.StartupPath;
foreach (string checkedItem in checkedListBox1.CheckedItems)
{
if (checkedItem.Contains("."))
{
string str = checkedItem;
if (str.Contains("."))
{
int index = str.IndexOf('.');
string folder = str.Substring(0, index);
try
{
bool started = false;
var process = new Process();
process.StartInfo.FileName = pad + "/data/" + folder + "/" + checkedItem;
started = process.Start();
var processID = process.Id;
var processNAAM = process.ProcessName;
textBox1.Text += "Gevonden - ID: " + processID + " NAAM: " + processNAAM + Environment.NewLine;
textBox1.Text += DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + " - " + "Installatie Keuze wordt opgestart." + Environment.NewLine;
process.WaitForExit();
}
catch (Exception)
{
}
}
}
}

First of all, you can make the code simpler and shorter by using the CheckedListBox's CheckedItems property. Secondly, there's no point to all your copying of strings from one to another. Strings are immutable in .NET - they never change. You can keep just one copy and cut from there.
Next, you can use the methods in System.IO.Path to cut the filename without the extension, or to build a full path without worrying about having too many or too few "/"'s.
Third, for your original question - just call WaitForExit on your Process object to make it wait before moving on with the list of processes.
Thirdly
foreach (string checkedItem in checkedListBox1.CheckedItems)
{
if (checkedItem.Contains("."))
{
string baseName = Path.GetFileNameWithoutExtension(checkedItem);
string processPath = Path.Combine(pad, "data", baseName, checkedItem);
Process process = Process.Start(processPath);
process.WaitForExit();
}
}

After the line where you start the process (process.Start()), simply add the following:
process.WaitForExit()
This will pause the containing thread until the target process has exited.

Related

Capturing output from powershell script

I have been working on converting a GUI script from another language to C# in VS2017 for a customer. With help from the folks here I am 95% of the way there, but have run into a couple of snags; just not sure I am doing things in the best way. I'm including just the relevant portions of code below, please let me know if I am not providing enough:
The majority of the code is centered on the wpf form, which collects data for low level technicians to batch deploy a number of Virtual Machines into the VMware environment. This number could easily range into the dozens or even a hundred VMs at once. The information for each VM is specified in the form, then collected in a listview. Once the listview is fully populated it is exported to a csv. Up to this point everything works just fine.
I've next been working on actually launching the powershell/powerCLI script (also working) and capturing output. The log file is opened with a specific reader application the customer uses, which updates in real time, and the captured output is fed to the log. It is important for the technicians to see the output from the code line by line so they can react if there is an issue.
I started with something like this as a test:
string sPSScript = "C:\\Users\\Admin\\Desktop\\TestC#.ps1";
string logFile = "C:\\Users\\Admin\\Desktop\\My.log";
string logReader = "C:\\Users\\Admin\\Documents\\CMTrace.exe";
string standard_output;
System.Diagnostics.Process PSScript = new System.Diagnostics.Process();
PSScript.StartInfo.FileName =
Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) +
"\\WindowsPowerShell\\v1.0\\powershell.exe";
PSScript.StartInfo.Arguments = "-command . '" + sPSScript + "' " +
vCenter.Text;
PSScript.StartInfo.RedirectStandardOutput = true;
PSScript.StartInfo.UseShellExecute = false;
PSScript.Start();
System.Diagnostics.Process LogFile = new System.Diagnostics.Process();
LogFile.StartInfo.FileName = logReader;
LogFile.StartInfo.Arguments = logFile;
LogFile.Start(); while ((standard_output =
PSScript.StandardOutput.ReadLine()) != null)
{
if (standard_output != "")
{
using (StreamWriter file = new StreamWriter(logFile, append: true))
{
file.WriteLine(standard_output);
}
}
}
While this writes to the log file in real time as expected, it creates 100 instances of the logReader application. I understand why, since I am declaring a new StreamWriter object through every pass, but am unsure how better to go about this.
I tried creating the file outside the loop, like this:
StreamWriter file = new StreamWriter(logFile, append: true) { };
System.Diagnostics.Process LogFile = new System.Diagnostics.Process();
LogFile.StartInfo.FileName = logReader;
LogFile.StartInfo.Arguments = logFile;
System.Diagnostics.Process PSScript = new System.Diagnostics.Process();
PSScript.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) + "\\WindowsPowerShell\\v1.0\\powershell.exe";
PSScript.StartInfo.Arguments = "-command . '" + sPSScript + "' " + vCenter.Text;
PSScript.StartInfo.RedirectStandardOutput = true;
PSScript.StartInfo.UseShellExecute = false;
LogFile.Start();
PSScript.Start();
System.Threading.Thread.Sleep(1500);
while ((standard_output = PSScript.StandardOutput.ReadLine()) != null)
{
if (standard_output != "")
{
file.WriteLine(standard_output);
}
}
It doesn't create multiple instances, but it also does not update the log file in real time as the previous code does. It only updates once the script runs, and then only partially. The script produces ~1000 lines of output, and I consistently see only about 840 written to the log file.
I thought about doing something like this:
FileStream logFS;
logFS = new FileStream(logFile, FileMode.Append);
but it appears the only options available to me to write to the file are expecting a byte array.
I am sure that I am missing something stupid simple in this, but would appreciate any suggestions on the easiest way to create the log file, open it in the reader, and then update it with the standard output from the powershell script.
why did the previous code writes in real time?
because you are wrapping it with using. And at the end of using block its gonna call dispose which calls .Flush to write to disk
Your second code block calls WriteLine but never called Flush so it writes to the disk whenever the buffer is full. Just add a .Flush call after WriteLine and you will have real time logging

How to get an incremented value to keep its count

The way my code is currently written, it will change the name of a file to the current date and then it will move it to a completed folder in the sub directory. That all works fine.
What I'm trying to implement is being able to rename it with different numbers as well if there are duplicates. Basically, if the file already exists in the completed folder, it will rename it "the date"(1).csv.
My issue is that because i is set to 1, even though I increment i, it resets it back to 1. I just want it to hold the incremented value while the code is running. if there are three files in the folder it retrieves it from, I want it to label them "the date", "the date"(1), and "the date"(2). Then if the code runs again tomorrow it will start the count over. Most likely my code will never experience more than one file in the folder. I want to implement this as a safety. If for whatever reason there is more than one file in the folder I want my code to be able to still run.
This is what I have so far:
int i = 1;
var newFileName = DateTime.Now.ToString("MMddyyyy") + ".csv";
destinationPath = Path.Combine(GenesisDirectory, "Completed");
Directory.CreateDirectory(destinationPath);
var destinationFileName = Path.GetFileName(newFileName);
destinationPath = Path.Combine(destinationPath, destinationFileName);
if (File.Exists(destinationPath))
{
newFileName = DateTime.Now.ToString("MMddyyyy") + "(" + i + ")" + ".csv";
destinationFileName = Path.GetFileName(newFileName);
var Dironly = Path.GetDirectoryName(destinationPath);
destinationPath = Path.Combine(Dironly, destinationFileName);
i++;
}
File.Move(inputFile, destinationPath);
You might want to make your counter static so it persists across calls. You will need to move it out of any method it is in to do that. There are better ways to do it, but you've somewhat limited the scope of solutions by not providing more of your code.
private static int s_Counter = 1;
public static IncrementCounter () { Interlocked.Increment(ref s_Counter); }
public static int Counter { get { return s_Counter; } }
We use Interlocked.Increment instead of s_Counter += 1 in case you later decide to make your code multi-threaded. (As a rule, static methods/properties should be designed to be thread safe.)
You could just make it as static to make the value persist across the call,but the counter value will reset if you restart your application that will result in some undesirable behaviour.
The best practice is to store your counter value in a database or a file so that even after restarting your application you can access the updated counter value and your code will correctly.

Console.Readline("") repeating at restart

I have this question,
My boss wants a program which you can enter a path, Console.ReadLine(directory);
This is irrelevant, I got this part working. In fact, the whole code/program is working as it should be.
The point of the path is that the program scans all the files in the given directory/subdirectories for the last write time. He want to pay minimum effort in this. So the plan is to use Windows to start this program once every 24 hours.
Only problem with this "minimum effort" part is that you have to enter the path EVERYTIME when it's started. So it actually doesn't go automaticly.
The question is: is there a way to avoid this?
For example Thread.Sleep(); and when it's done sleeping goto a label right under the Console.ReadLine(directory);?
So not once a day, but sleeping for 24 hours and for 1 whole minute working?
If it's any help, here's the code:
using System.IO;
using System.Security.Permissions;
namespace CheckWithinTime
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Which folder do you wish to scan?");
string path = Console.ReadLine();
//copies everything from the console to a .txt file
FileStream filestream = new FileStream(#"C:\Logs\Log.txt", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
//this is the path which you type in a the beginning of the program
string[] files = Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
List<string> updatedFiles = new List<string>();
DateTime from = DateTime.Now.AddDays(-1);
DateTime to = DateTime.Now;
foreach (string name in files)
{
FileInfo file = new FileInfo(name);
string fullname = file.FullName;
//checks if the last writed time occured less then 24 hours ago, if it's not it will not be loggeed
if (file.LastWriteTime >= from && file.LastWriteTime <= to)
{
updatedFiles.Add(name);
Console.WriteLine(file.FullName + " ; " + "last changed at >> " + " ; " + file.LastWriteTime.ToString());
//Console.WriteLine("File created at >> " + file.CreationTime.ToString());
//Console.WriteLine("File last opened at >> " + file.LastAccessTime.ToString());
Console.WriteLine();
}
}
streamwriter.Close();
filestream.Close();
//The mail class basicly sends an e-mail to the server with the log file, but this is irrelevant
Mail();
}
}
}
It used to be just a simple file system watcher. After that it was like it is now, but without the Console.ReadLine() part. And now he wants to give a path.
If anyone can tell me a way to avoid the Console.ReadLine(), but only use call it when you need it. I would appreciate it!
Sorry in advance for my big texts.
The best way to do this would be to either create an XML file or use a notepad file and have windows run a task manager.
You can set up a program to run every so often in windows task manager and all you need to do is save the path to a file and have your C# console application read that file and get the path, the path will always be store and the program will run all the time.
We do this at work; a program has been running for 4 months doing through a bunch of paths and we don't edit it anymore.
Xml file/config is overkill for one setting. Pass it in to the command line string[] args:
string path;
if(args.Length > 0)
{
path = args[0];
Console.WriteLine("Using path: " + path);
}
else
{
Console.WriteLine("Which folder do you wish to scan?");
path = Console.ReadLine();
}
Then use task scheduler as already suggested, but pass the path as a command line argument. You can also then launch it with different paths without your app supporting multiple paths.

Strange Problem with File.Move command

I have encountered a strange problem when using the File.Move command. The Programm actually moves and renames the file, but then throws me an exception that the sourcefile is not found; - what is expected because the File was moved.
The Program works fine if i catch the Exception but i'm wondering why i get these exception.
My Code:
foreach (string str in CPM.prot.FKFinishedBad)
{
try
{
string dir = System.Configuration.ConfigurationSettings.AppSettings["ResultDir"] + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + "_Bearbeitete Protokolle";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Move(System.Configuration.ConfigurationSettings.AppSettings["ResultDir"] + "\\" + str + "_" + CPM.LastJob + ".txt", dir + "\\" + "\\" + str + "_" + CPM.LastJob + "_Nachproduziert" + ".txt");
}
catch (Exception e)
{
}
}
Make sure that each item in CPM.prot.FKFinishedBad is unique - that may be a cause of the phenomenon.
Also, I'd recommend to refactor the code: the directory lines don't need to be repeated and should be outside of the loop.
And please learn to use String.Format and Path.Combine.
Are you sure all of your files exist?
It might happen that one of them is missing (which explains the exception), while the others are processed correctly. you can also check them before the move with File.Exists.
Also, be careful when using empty catch blocks, they can cause a lot of headaches when debugging.
Try to suspend a thread for a half a second (or less) here:
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
//suspend thread for 0.5 sec
}
This is probably related to fact that you create a directory and immediately move a file. So suspend a thread to let "breath" to system.
I have just been experiencing this problem, it took me a while to realise that there is a FileInfo.MoveTo command which appears to do the same thing.
However it doesn't throw an exception, and works.
It is a bit dodgy if there are two ways to do one thing and only one of them works.

C# Problem Reading Console Output to string

i want to launch ffmpeg from my app and retrive all console output that ffmpeg produces. Thing seems obvious, i followed many forum threads/articles like this one but i have problem, though i follow all information included there I seem to end up in dead end.
String that should contain output from ffmpeg is always empty. I've tried to see where is the problem so i made simple c# console application that only lists all execution parameters that are passed to ffmpeg, just to check if problem is caused by ffmpeg itself. In that case everything work as expected.
I also did preview console window of my app. When i launch ffmpeg i see all the output in console but the function that should recieve that output for further processing reports that string was empty. When my param-listing app is launched the only thing I see is the expected report from function that gets output.
So my question is what to do to get ffmpeg output as i intended at first place.
Thanks in advance
MTH
This is a long shot, but have you tried redirecting StandardError too?
Here is a part of my ffmpeg wrapper class, in particular showing how to collect the output and errors from ffmpeg.
I have put the Process in the GetVideoDuration() function just so you can see everything in the one place.
Setup:
My ffmpeg is on the desktop, ffPath is used to point to it.
namespace ChildTools.Tools
{
public class FFMpegWrapper
{
//path to ffmpeg (I HATE!!! MS special folders)
string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";
//outputLines receives each line of output, only if they are not zero length
List<string> outputLines = new List<string>();
//In GetVideoDuration I only want the one line of output and in text form.
//To get the whole output just remove the filter I use (my search for 'Duration') and either return the List<>
//Or joint the strings from List<> (you could have used StringBuilder, but I find a List<> handier.
public string GetVideoDuration(FileInfo fi)
{
outputLines.Clear();
//I only use the information flag in this function
string strCommand = string.Concat(" -i \"", fi.FullName, "\"");
//Point ffPath to my ffmpeg
string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";
Process processFfmpeg = new Process();
processFfmpeg.StartInfo.Arguments = strCommand;
processFfmpeg.StartInfo.FileName = ffPath;
//I have to say that I struggled for a while with the order that I setup the process.
//But this order below I know to work
processFfmpeg.StartInfo.UseShellExecute = false;
processFfmpeg.StartInfo.RedirectStandardOutput = true;
processFfmpeg.StartInfo.RedirectStandardError = true;
processFfmpeg.StartInfo.CreateNoWindow = true;
processFfmpeg.ErrorDataReceived += processFfmpeg_OutData;
processFfmpeg.OutputDataReceived += processFfmpeg_OutData;
processFfmpeg.EnableRaisingEvents = true;
processFfmpeg.Start();
processFfmpeg.BeginOutputReadLine();
processFfmpeg.BeginErrorReadLine();
processFfmpeg.WaitForExit();
//I filter the lines because I only want 'Duration' this time
string oStr = "";
foreach (string str in outputLines)
{
if (str.Contains("Duration"))
{
oStr = str;
}
}
//return a single string with the duration line
return oStr;
}
private void processFfmpeg_OutData(object sender, DataReceivedEventArgs e)
{
//The data we want is in e.Data, you must be careful of null strings
string strMessage = e.Data;
if outputLines != null && strMessage != null && strMessage.Length > 0)
{
outputLines.Add(string.Concat( strMessage,"\n"));
//Try a Console output here to see all of the output. Particularly
//useful when you are examining the packets and working out timeframes
//Console.WriteLine(strMessage);
}
}
}
}

Categories