How do I run an external program like Notepad or Calculator via a C# program?
Maybe it'll help you:
using(System.Diagnostics.Process pProcess = new System.Diagnostics.Process())
{
pProcess.StartInfo.FileName = #"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
}
Use System.Diagnostics.Process.Start
Hi this is Sample Console Application to Invoke Notepad.exe ,please check with this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Demo_Console
{
class Program
{
static void Main(string[] args)
{
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "Notepad.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
}
}
}
For example like this :
// run notepad
System.Diagnostics.Process.Start("notepad.exe");
//run calculator
System.Diagnostics.Process.Start("calc.exe");
Follow the links in Mitchs answer.
Related
I have created a console app in which i want to trigger multiple links at a specific time.after searching I have done something like this:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace cronjob_Test_App
{
class Program
{
static void Main(string[] args)
{
StartProcess();
}
public static void StartProcess()
{
// Process.Start("https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe");
var psi = new ProcessStartInfo("chrome.exe");
string a, b;
a = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
b = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
psi.Arguments = a;
Process.Start(psi);
psi.Arguments = b;
Process.Start(psi);
}
}
}
it starts all the links simultaneously.I want the first link to complete and then start the second one.how can I do it or if there is some other good way please suggest.
I am using windows scheduler along with this console app to start the console app at a specific time.
You can try this. use use Process.Start and set url as second parameter.
string a = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
string b = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
Process.Start("chrome.exe", a);
Process.Start("chrome.exe", b);
Use the Process.WaitForExit() Method
var psi = new ProcessStartInfo("chrome.exe");
string a, b;
a = "http://www.google.com/";
b = "http://www.bing.com/";
psi.Arguments = a;
var p1= Process.Start(psi);
p1.WaitForExit();
psi.Arguments = b;
var p2 = Process.Start(psi);
p2.WaitForExit();
Console.ReadLine();
Also you can add a time delay to the method that takes (int) milliseconds as parameter.
example : p1.WaitForExit(500)
PS : The process won't wait for the entire web page to load.
Edited :
If you are trying to download a file then please make use of WebClient
using (WebClient client = new WebClient())
{
Task taskA = Task.Factory.StartNew(() => client.DownloadFile("https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe",
#"F:\Installer.exe"));
taskA.Wait();
Console.WriteLine("Task A has completed.");
Task taskB = Task.Factory.StartNew(() => client.DownloadFile("https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe",
#"F:\Installer.exe"));
taskA.Wait();
Console.WriteLine("Task B has completed.");
}
I want to play one video in different bitrates. Like i uploaded one video in 1080P resolution i want play that video in 720P, 480P, 360P, 240P, 144P etc.
I want this solution in asp.net using C#.
Like youtube provide the facility to watch video in different resolutions.
Please help me regarding this.
I tried the following code but not working:
using Softpae.Media;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
Job2Convert myJob = new Job2Convert();
MediaServer ms = new MediaServer();
myJob.pszSrcFile = "E:\\EhabVideoLibrary\\videos\\sinbad.mkv";
myJob.pszDstFile = "E:\\EhabVideoLibrary\\videos\\sinbad.mp4";
myJob.pszDstFormat = "mp4";
myJob.pszAudioCodec = "aac";
myJob.nAudioChannels = 2;
myJob.nAudioBitrate = -1;
myJob.nAudioRate = -1;
myJob.pszVideoCodec = "h264";
myJob.nVideoBitrate = -1;
myJob.nVideoFrameRate = -1;
myJob.nVideoFrameWidth = -1;
myJob.nVideoFrameHeight = -1;
bool ret = ms.ConvertFile(myJob);
}
}
}
You can use FFplay of the FFmpeg project. (ffmpeg.org) With FFmpeg it's possible to encode and transcode almost every codec in the resolution you want. In this thread is the use of a command line application using C# described.
I've never tried it, but there are also libraries provided for .NET using FFmpeg like this:
ffmpegdotnet.codeplex.com
intuitive.sk/fflib
Success with it!
Here is an example code using ffmpeg (I tested it under Win7 VM):
using System;
namespace ConsoleApplication_FFmpegDemo
{
class Program
{
static void Main(string[] args)
{
string inputVideo = #"C:\Users\Public\Videos\Sample Videos\Wildlife.wmv";
string outputVideo = #"C:\Users\Public\Videos\Sample Videos\Wildlife.mp4";
string ffmpegArg = string.Format("-i \"{0}\" -vf scale=320:240 \"{1}\"", inputVideo, outputVideo);
string ffmpegPath = #"C:\Portable\ffmpeg-win32-static\bin\ffmpeg.exe";
FFmpegTask ffmpegTask = new FFmpegTask(ffmpegPath, ffmpegArg);
ffmpegTask.Start();
Console.ReadLine();
}
}
}
And the FFmpegTask.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication_FFmpegDemo
{
public class FFmpegTask
{
public Process process = new Process();
public FFmpegTask(string ffmpegPath, string arguments)
{
process.StartInfo.FileName = ffmpegPath;
process.StartInfo.Arguments = arguments;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
}
public bool Start()
{
return process.Start();
}
}
}
I am trying to create a program that will shutdown my system when a specific string or character is sent from my Arduino. When I debug the program and send string "S" from my Arduino, the console application simply closes without a printout.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Diagnostics;
namespace Arduino_Test
{
class Program
{
static void Main(string[] args)
{
SerialPort myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM5";
myport.Open();
string data_rx = myport.ReadLine();
string s = "S";
if(data_rx == s)
{
Console.WriteLine(data_rx);
var psi = new ProcessStartInfo("shutdown", "/s /f /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
}
}
}
}
I suspect something is wrong within the if statement. Furthermore I am in need of help regarding Visual Studio 2013, I am unfamiliar with C# and the application. My project is a console application, is it possible to create a windows forms application using the same code? I am under experienced in this field and your patience is appreciated.
You will want to define some new terminal state, but lets pretend that it should run forever. If that is the case, simply put your read code in a loop.
while(true) {
string data_rx = myport.ReadLine();
string s = "S";
if(data_rx == s)
{
Console.WriteLine(data_rx);
var psi = new ProcessStartInfo("shutdown", "/s /f /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
}
}
I am trying to run a scheduled task from C# without opening a new command line window, using the following code without any success (it prompts a window every time I use it)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
Process p1 = new Process();
p1.StartInfo.FileName = #"C:\Windows\System32\schtasks.exe";
p1.StartInfo.Verb = "runas";
p1.StartInfo.Arguments = "/run /tn CCleaner";
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.UseShellExecute = false;
p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p1.StartInfo.CreateNoWindow = true;
p1.Start();
p1.Close();
}
catch (Exception ex)
{
}
}
}
}
How could I solve this problem ?
Thank you so much for your attention
You can use a library that provides access to Task Scheduler API.
For example using http://taskscheduler.codeplex.com/
using (var ts = new TaskService())
{
Task task = ts.GetTask("My task");
task.Run();
}
I have tried to run the batch file from c# using the following code and i want to display the result in WPF textbox. Could you please guide me how to do this?
using System;
namespace Learn
{
class cmdShell
{
[STAThread] // Lets main know that multiple threads are involved.
static void Main(string[] args)
{
System.Diagnostics.Process proc; // Declare New Process
proc = System.Diagnostics.Process.Start("C:\\listfiles.bat"); // run test.bat from command line.
proc.WaitForExit(); // Waits for the process to end.
}
}
}
This batch file is to list the files from the folder. Once the batch is executed result should be displayed in the textbox. If the batch file having more than one commands, then result of each commands should be displayed in textbox.
You need to redirect the standard output stream:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Process proc = new Process();
proc.StartInfo.FileName = "test.bat";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine(output); // or do something else with the output
proc.WaitForExit();
Console.ReadKey();
}
}
}
I have resolved the issues with process hanging and getting output instantly as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Process proc = new Process();
proc.StartInfo.FileName = "test.bat";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.OutputDataReceived += proc_OutputDataReceived;
proc.Start();
proc.BeginOutputReadLine();
}
}
void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
txtprogress.Text = txtprogress.Text + "\n" + e.Data;
txtprogress.ScrollToEnd();
}));
}
}