I have a little problem with my code, I would like to use two variables string numberTextBox1 and numberTextBox2 in a static void function to replace the variables chiffre_1 and chiffre_2
I looked at converting string to var ==> failure
I tried to set the variables numberTextBox 1 and 2 ==> as a function argument
I tried to set function variables equal to TextBox1.Text and TextBox2.Text ==> failed
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPython;
using IronPython.Hosting;
using System.IO;
namespace CSharp_with_Python_Script
{
public partial class Form1 : Form
{
string nombreTextbox1;
string nombreTextbox2;
string chiffre_1;
string chiffre_2;
public Form1()
{
InitializeComponent();
}
static void Execute(nombreTextBox1,
nombreTextBox2)
{
var psi = new ProcessStartInfo();
psi.FileName =
#"C:\Users\adm\AppData\Local\Programs\Python\Python37-32\python.exe";
var script = #"C:\Users\adm\Documents\Visual Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\Class_Plot.py";
var chiffre_1 = nombreTextBox1;
var chiffre_2 = nombreTextBox2;
psi.Arguments = $"\"{script}\" \"{chiffre_1}\" \"{chiffre_2}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var errors = "";
var result = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
result = process.StandardOutput.ReadToEnd();
}
}
private void Btn_ChangePicture_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation = (#"C:\Users\adm\Documents\Visual
Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\PLOT_MATPLOTLIB.png");
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void Btn_Script_Click(object sender, EventArgs e)
{
Execute();
}
private void button1_Click(object sender, EventArgs e)
{
nombreTextbox1 = textBox1.Text;
nombreTextbox2 = textBox2.Text;
}
}
}
I would like the variables chiffre_1 and chiffre_2 of the function to be equal to the variables entered in the function Execute
Ps: I am a beginner in C # and novice in the functions
try that
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPython;
using IronPython.Hosting;
using System.IO;
namespace CSharp_with_Python_Script
{
public partial class Form1 : Form
{
string nombreTextbox1;
string nombreTextbox2;
string chiffre_1;
string chiffre_2;
public Form1()
{
InitializeComponent();
}
static void Execute()
{
chiffre_1 = nombreTextBox1;
chiffre_2 = nombreTextBox2;
var psi = new ProcessStartInfo();
psi.FileName =
#"C:\Users\adm\AppData\Local\Programs\Python\Python37-32\python.exe";
var script = #"C:\Users\adm\Documents\Visual Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\Class_Plot.py";
var chiffre_1 = nombreTextBox1;
var chiffre_2 = nombreTextBox2;
psi.Arguments = $"\"{script}\" \"{chiffre_1}\" \"{chiffre_2}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var errors = "";
var result = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
result = process.StandardOutput.ReadToEnd();
}
}
private void Btn_ChangePicture_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation = (#"C:\Users\adm\Documents\Visual
Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\PLOT_MATPLOTLIB.png");
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void Btn_Script_Click(object sender, EventArgs e)
{
Execute();
}
private void button1_Click(object sender, EventArgs e)
{
nombreTextbox1 = textBox1.Text;
nombreTextbox2 = textBox2.Text;
}
}
}
When throw event button1_Click it's set nombreTextbox1 and nombreTextbox2 after that Btn_Script_Click launch Execute() method and use a private property to set chiffre_1 and chiffre_2 with a new value.
Related
Hello I was writing a basic text editor in C# on visual studio windows form using the .NET framework 3.1 long term support version everything worked fine until I wrote the save file script
Here's the code for "Form1.cs" which is where the open and save file functions reside
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security.Principal;
namespace Text_Editor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string locsave;
private void openbtn_Click(object sender, EventArgs e)
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
if (principal.IsInRole(WindowsBuiltInRole.Administrator) != true)
{
MessageBox.Show("Run as Admin");
System.Windows.Forms.Application.ExitThread();
}
else
{
OpenFileDialog openfile = new OpenFileDialog();
if (openfile.ShowDialog() == DialogResult.OK)
{
var locationArray = openfile.FileName;
string location = "";
locsave = locationArray;
foreach (char peice in locationArray)
{
location = location + peice;
}
var contentArray = File.ReadAllText(location);
string content = "";
label4.Text = location;
foreach (char peice in contentArray)
{
content = content + peice;
}
richTextBox1.Text = content;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Test");
}
private void savebtn_Click(object sender, EventArgs e)
{
if (#label4.Text == "")
{
MessageBox.Show("Open a text file first");
}
else
{
StreamWriter outputfile = new StreamWriter(locsave);
outputfile.Write(richTextBox1.Text); //this is where the error occures and it throws the error of access denyed
outputfile.Close();
}
}
}
}
Does anyone have a suggestion about what to do I looked around on google for a solution but it seemed most did not work and some others I did not understand.
I'm using this code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace System_Information
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GetInfo();
}
private void GetInfo()
{
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "msinfo32.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = "/report D:\\Info\\report.txt";
process.StartInfo.WorkingDirectory = "D:\\Info";
process.Start();
process.WaitForExit();
process.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
but it's creating a very big file size 1.5MB of information.
if I want to use the msinfo32 but to show for example only the memory , video car , and mother board info? How can I select it to show only this info ?
I am hoping someone can help me in getting an issue of mine to work, I feel as if it is an easy one, however not having any luck in fixing what I am trying to do. I want to be able to pause a video which I am playing using vlc.dotnet below is a brief summary of the structure of my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using Vlc.DotNet.Forms;
using System.Threading;
using Vlc.DotNet.Core;
using System.Diagnostics;
namespace TS1_C
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button8.Click += new EventHandler(this.button8_Click);
}
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string chosen = listBox1.SelectedItem.ToString();
string final = selectedpath2 + "\\" + chosen; //Path
playfile(final);
}
void playfile(string final)
{
var control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
control.Play();
}
private void button8_Click(object sender, EventArgs e)
{
}
}
}
As you can see I have one method which takes a double click from an item in a list box and plays it using the method playfile. However I want to be able to pause the video using my button known as button8. I have tried many things even this
control.Paused += new System.EventHandler<VlcMediaPlayerPausedEventArgs>(button8_Click);
Which I put into the playfile method, however nothing seems to work. I am wondering if my whole method in which I play a file using playfile(); is completely wrong. I am hoping someone can help me in trying to achieve what I need
Thank you
Your control should be initialized only once:
private VlcControl control;
public Form1()
{
InitializeComponent();
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
}
then, your play method could be simplified:
void playfile(string url)
{
control.Play(url);
}
And for your pause method:
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
}
Need to zip text files from a predefined location & save the zipped files in another location of hard disk using windows services & delete the old text file (a scheduled service)
I tried the following code in which i used 'icsharpcode' for zipping the files. But after installing the service.. i am getting a message that "this services has started & then stopped..." without showing any required output.
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Design;
using System.Diagnostics.Eventing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using ICSharpCode.SharpZipLib.Zip;
namespace createzip
{
public partial class createzip : ServiceBase
{
Timer timer1 = new Timer();
public createzip()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(onelapsedtime);
timer1.Enabled = true;
timer1.Interval = 60000;
}
protected override void OnStop()
{
timer1.Enabled = false;
}
private void onelapsedtime(object source, ElapsedEventArgs e)
{
string folder = "#E:\\zipped files";
Directory.SetCurrentDirectory(folder);
string output = "#E:\\output";
string outputfilename = Path.Combine(output, "this file is zipped");
using (var x = new ZipFile(output))
{
foreach (var f in Directory.GetFiles(folder))
x.Add(f);
}
string[] filenames = Directory.GetFiles(folder);
using ( ZipOutputStream s = new
ZipOutputStream(File.Create(output))) //(args[1])))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetDirectoryName(file));
//entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourcebytes;
do
{
sourcebytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourcebytes);
}
while (sourcebytes > 0);
}
}
s.Finish();
s.Close();
return;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace trail2zip
{
public partial class trail2zip : ServiceBase
{
Timer timer;
string path1 = #"E:\zipped files\New Text Document.txt";
string path2 = #"E:\output\filezipname.zip";
string path3 = #"E:\zipped files\R_23122015.txt";
int timerInterval = 60000;
public trail2zip()
{
InitializeComponent();
timer = new Timer();
timer.Elapsed+=new ElapsedEventHandler(this.timer_Elapsed);
timer.Interval = timerInterval;
timer.Enabled = true;
}
protected override void OnStart(string[] args)
{
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer.SynchronizingObject = null;
timer.Elapsed -= new ElapsedEventHandler(this.timer_Elapsed);
timer.Dispose();
timer = null;
}
public void timer_Elapsed(object sender, ElapsedEventArgs e)
{
ZipFile z = ZipFile.Create(path2); //(filezipname);
z.BeginUpdate();
z.Add(path1);
z.Add(path3);
z.CommitUpdate();
z.Close();
}
}
}
Im trying to create a minecraft server wrapper but im having trouble reading the output of the process. I want to read the output in the serverProcess_ErrorDataRecevied event and I know that what there is now doesn't work. But what could I put there instead to read it?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace MC_Server_UI
{
public partial class Form1 : Form
{
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Java\\jre7\\bin\\java.exe", "Xmx1024M - jar " + "craftbukkit.jar" + " nogui");
Process serverProcess;
OpenFileDialog ofd;
FolderBrowserDialog fbd;
public Form1()
{
InitializeComponent();
fbd = new FolderBrowserDialog();
fbd.ShowDialog();
startInfo.WorkingDirectory = fbd.SelectedPath;
startInfo.RedirectStandardInput = startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
serverProcess = new Process();
serverProcess.StartInfo = startInfo;
serverProcess.EnableRaisingEvents = true;
serverProcess.ErrorDataReceived += new DataReceivedEventHandler(serverProcess_ErrorDataReceived);
serverProcess.Exited += new EventHandler(serverProcess_Exited);
serverProcess.Start();
}
private void serverProcess_ErrorDataReceived(object sender, EventArgs e)
{
richTextBox1.AppendText(serverProcess.StandardError.ReadToEnd());
}
private void serverProcess_Exited(object sender, EventArgs e)
{
}
}
}
See:
How to spawn a process and capture its STDOUT in .NET?
ProcessStartInfo.RedirectStandardOutput
Code:
serverProcess.StartInfo.RedirectStandardOutput = true;
serverProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
serverProcess.Start();
serverProcess.BeginOutputReadLine();
Or:
serverProcess.StartInfo.RedirectStandardOutput = true;
var output = serverProcess.StandardOutput.ReadToEnd();
See also: ProcessStartInfo.RedirectStandardError
serverProcess.StartInfo.RedirectStandardError = true;
var error = serverProcess.StandardError.ReadToEnd();