I am trying to create a program, where in the Windows Forms Form a program will be started when a button is clicked.
In the Console app, some folders and files are created and then with a logging class the messages are logged in a txt file.
What I want to do is to return this message also as an output in the Form textbox.
I am new to C# and not sure how I can send this message back to my Form. I already tried with console output and to read it in Form. But the Problem was that the output was only available when the program was finished. Preferably the message should be sent every time the logWrite is called.
What I have tried:
DwnldVwrForm1.cs:
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
using System.Management;
namespace DownloadViewer
{
public partial class DwnldVwrForm1 : Form
{
private Process firstProc;
private Process logProc;
private int processId;
private string maxRegmonth;
private string selectedFlow;
public string logTextBox;
public DwnldVwrForm1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string pathJsonFile = @"C:\Projects\DownloadViewer\FolderPath.json";
string pathJsonString = File.ReadAllText(pathJsonFile);
FolderPath folderpath1 = JsonConvert.DeserializeObject<FolderPath>(pathJsonString);
for (int i = 0; i < folderpath1.FolderPathLinks.Count; i++)
{
if (folderpath1.FolderPathLinks[i].Projectname == selectedFlow)
{
string dirPath = folderpath1.FolderPathLinks[i].Destinationpath;
try
{
firstProc = new Process();
firstProc.StartInfo.FileName = @"C:\Programs\"+ selectedFlow + @"\setup.exe";
firstProc.StartInfo.RedirectStandardOutput = true;
firstProc.StartInfo.RedirectStandardError = true;
firstProc.EnableRaisingEvents = true;
firstProc.StartInfo.CreateNoWindow = true;
firstProc.StartInfo.UseShellExecute = false;
firstProc.Start();
processId = firstProc.Id;
label1.Text = "Folder path: " + dirPath;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
private void listBox1_Click(object sender, EventArgs e)
{
selectedFlow = listBox1.SelectedItem.ToString();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Windows.Forms;
namespace CreateFiles
{
class Program
{
static void Main(string[] args)
{
Directory.CreateDirectory(@"C:\test\202308);
LogWriter.LogWrite("Folder 202308 was created");
}
}
}
LogWriter.cs
using System;
using System.IO;
namespace CreateFiles
{
public static class LogWriter
{
public static void LogWrite(string logMessage)
{
string pathMainLogFile = @"C:\Output\Logs\MainLog.txt";
try
{
using (StreamWriter z = File.AppendText(pathMainLogFile))
{
Log(logMessage, z);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void Log(string logMessage, TextWriter txtWriter)
{
try
{
txtWriter.WriteLine("{0} {1} {2} {3} {4}", "CHOD", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString(), " :", logMessage);
}
catch (Exception ex)
{
}
}
}
}