I create the code for text to speech.
I want it to open web browser how to do this??
I am using windows 7 OS.
I also download xampp.
using System.Speech.Synthesis;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
synthesizer.Volume = 100; // 0...100
synthesizer.Rate = -2; // -10...10
// Synchronous
synthesizer.Speak("Hello World");
// Asynchronous
synthesizer.SpeakAsync("Hello World");
}
}
}
First, add this namespace
using Microsoft.Win32;
using System.Diagnostics;
Then, run this code which will find the default web browser and launch it.
Process p = new Process();
string browser = string.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(#"HTTP\shell\open\command", false);
//trim off quotes
browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browser.EndsWith("exe"))
{
//get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
}
}
finally
{
if (key != null) key.Close();
}
p.StartInfo.FileName = browser;
p.StartInfo.Arguments = "http://www.google.com";
p.Start();
Related
I am trying to capture the stdout of a process started in my code but still put the output to the console app that it is running.
My project is a .net core Windows application.
I attach a procedure thread to the process to get the stdout and in that thread I do a console write to also output it to the attached application. I suspect that it is trying to write it to my applications non existent console instead of the joined process. If so, is there a way to get the output and still have it written to the executed processes console?
Code -
private Process proc = null;
private void WriteStandardOutput()
{
using (StreamWriter writer = File.CreateText(exeOutputPath + "\\_out.txt"))
using (StreamReader reader = proc.StandardOutput)
{
writer.AutoFlush = true;
for (; ; )
{
string textLine = reader.ReadLine();
if (textLine == null)
break;
writer.WriteLine(textLine);
Console.Out.WriteLine(textLine);
}
}
if (File.Exists(exeOutputPath + "\\_out.txt"))
{
FileInfo info = new FileInfo(exeOutputPath + "\\_out.txt");
// if the error info is empty or just contains eof etc.
if (info.Length < 4)
info.Delete();
}
}
///////////////function code
int exitCode;
if (!File.Exists(exePath) && !exePath.Contains("cmd.exe"))
throw new Exception("No executable specified for RunExtApp - " + this.name);
if (exePath.Contains("cmd.exe"))
runParams = "/K " + runParams;
//Start the executable
extApp = new ProcessStartInfo();
extApp.Arguments = runParams;
extApp.FileName = exePath; // Path.GetFileName(exePath);
extApp.WorkingDirectory = Path.GetDirectoryName(exePath);
extApp.UseShellExecute = false;
extApp.RedirectStandardOutput = true;
extApp.RedirectStandardError = true;
// Do you want to show a console window?
//extApp.WindowStyle = Hidden.ProcessWindowStyle;
//extApp.CreateNoWindow = true;
// Run the external process & wait for it to finish
using (proc = Process.Start(extApp))
{
Thread stdOutThread = new Thread(new ThreadStart(WriteStandardOutput));
stdOutThread.IsBackground = true;
stdOutThread.Name = "StandardOutput";
stdOutThread.Start();
proc.WaitForExit();
stdOutThread.Join();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
proc.Close();
if (exitCode > 0)
throw new Exception("Failed to run external code - " + exePath + ". exit code - " + exitCode.ToString());
}
Test Exe - .net framework console application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleWriteTester
{
class Program
{
static void Main(string[] args)
{
for(int i = 0; i < 30; i ++)
{
Console.WriteLine("Line " + i.ToString());
System.Threading.Thread.Sleep(1000);
}
}
}
}
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();
}
}
}
The code looks lengthy but it's a simple program.
I have built a console app (TakeScreenshots) that will take website screenshots from firefox, chrome & ie in that order & save them in a folder. When I manually run TakeScreenshots.exe, all 3 screenshots are saved.
Now, I have built another console app (MyApp) that will execute TakeScreenshots.exe. But in this way, only the firefox screenshot is saved and not of the other 2. There are no exceptions. It just says "Process Complete". I guess, MyApp is not waiting for the TakeScreenshots to complete.
How can I fix this.
[TakeScreenshots will later be placed in few remote computers & run by MyApp]
TakeScreenshots code:
private static string[] WebDriversList = ["firefox","chrome","internetexplorer"];
private static void TakeAPic()
{
string url = "http://www.google.com";
string fileNamePrefix = "Test";
string snapSavePath = "D:\\Pics\\";
foreach (string wd in WebDriversList)
{
IWebDriver NewDriver = null;
switch (wd.ToLower())
{
case "firefox":
using (NewDriver = new FirefoxDriver())
{
if (NewDriver != null)
{
CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath);
}
}
break;
case "chrome":
using (NewDriver = new ChromeDriver(WebDriversPath))
{
if (NewDriver != null)
{
CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath);
}
}
break;
case "internetexplorer":
using (NewDriver = new InternetExplorerDriver(WebDriversPath))
{
if (NewDriver != null)
{
CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath);
}
}
break;
}
if (NewDriver != null)
{
NewDriver.Quit();
}
}
}
private static void CaptureScreenshot(IWebDriver driver,string url,string fileNamePrefix,
string snapSavePath)
{
driver.Navigate().GoToUrl(url);
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
ss.SaveAsFile(snapSavePath + fileNamePrefix + "_" + capabilities.BrowserName + ".png",
ImageFormat.Png);
}
MyApp code:
static void Main(string[] args)
{
ExecuteTakeScreenshot();
Console.WriteLine("PROCESS COMPLETE");
Console.ReadKey();
}
private static void ExecuteTakeScreenshot()
{
ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\");
Psi.FileName = "D:\\PsTools\\PsExec.exe";
Psi.Arguments = "/C \\DESK101 D:\\Release\\TakeScreenshots.exe";
Psi.UseShellExecute = false;
Psi.RedirectStandardOutput = true;
Psi.RedirectStandardInput = true;
Process.Start(Psi).WaitForExit();
}
Update:
It was my mistake. Initially WebDriversPath was assigned "WebDrivers/". When I changed it to the actual path "D:\WebDrivers\", it worked. But I still dont understand how it worked when TakeScreenshots.exe was run manually and it doesn't when run from another console
In similar problems I have had success with waiting for input idle first. Like this:
Process process = Process.Start(Psi);
process.WaitForInputIdle();
process.WaitForExit();
You could try this. For me it was needed to print a pdf using Adobe Reader and not close it to early afterwards.
Example:
Process process = new Process();
process.StartInfo.FileName = DestinationFile;
process.StartInfo.Verb = "print";
process.Start();
// In case of Adobe Reader the following statement is needed:
process.WaitForInputIdle();
process.WaitForExit(2000);
process.WaitForInputIdle();
process.Kill();
I'm new to c#.
I'm looking for solution to this (app to check can be downloaded from http://download.eset.com/special/ESETLogCollector.exe
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
namespace LogCollector
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = "C:\\robot\\ESETLogCollector.exe";
proc.Verb = "runas";
Process.Start(proc);
System.Threading.Thread.Sleep(2000);
Console.WriteLine("Ahoj");
AutomationElement desktop = AutomationElement.RootElement;
Condition cond = new PropertyCondition(AutomationElement.NameProperty, "ESET Log Collector");
AutomationElement elc = desktop.FindFirst(TreeScope.Children, cond);
Console.WriteLine(elc.Current.Name);
String save_path = "";
Condition cond1 = new PropertyCondition(AutomationElement.AutomationIdProperty, "1005");
try
{
AutomationElement save_as = elc.FindFirst(TreeScope.Subtree, cond1);
Console.WriteLine(save_as.Current.AutomationId);
save_path = save_as.Current.Name;
}
catch (System.Exception e)
{
Console.WriteLine("EX: {0}", e.Message);
}
if (System.IO.File.Exists(save_path))
{
System.IO.File.Delete(save_path);
Console.WriteLine(save_path);
}
Condition cond2 = new PropertyCondition(AutomationElement.AutomationIdProperty, "1002");
AutomationElement collect = elc.FindFirst(TreeScope.Children, cond2);
Console.WriteLine(collect.Current.Name);
try
{
Object outObject;
collect.TryGetCurrentPattern(InvokePattern.Pattern, out outObject);
InvokePattern pattern = outObject as InvokePattern;
pattern.Invoke();
}
catch (System.Exception e)
{
Console.WriteLine("EX: {0}", e.Message);
}
Console.ReadKey();
}
}
}
All I still got IF I Want to invoke buttonclick is:
Operation is not valid due to the current stat of the object
I really don't know what is happening here. Can anybody help me with this?
Ty
The operation is probably failing because you are not running your automation application as administrator while the ESET Log Collector has been run with elevated permissions.
The UI Automation Security Overview describes the security model and how to communicate with processes that are running at a higher privilege level (i.e. you need a signed application with a manifest file that includes a special attribute).
So, i'm using the PDFSharp lib, and, I whant to print a pdf on both sides....
My printer is a hp2055dn I have tryed all things... btw... my code is that:
using PdfSharp.Pdf.Printing;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace impressao
{
class Program
{
static void Main(string[] args)
{
PdfFilePrinter.AdobeReaderPath = #"C:\Program Files (x86)\Adobe\Acrobat 8.0\Acrobat\Acrobat.exe";
//PdfFilePrinter.AdobeReaderPath = #"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";
PrinterSettings settings = new PrinterSettings();
settings.Collate = false;
settings.Duplex = Duplex.Vertical;
PrintDialog printerDialog = new PrintDialog();
printerDialog.AllowSomePages = false;
printerDialog.ShowHelp = false;
printerDialog.PrinterSettings = settings;
printerDialog.AllowPrintToFile = true;
printerDialog.PrinterSettings.PrintToFile = true;
DialogResult result = printerDialog.ShowDialog();
if (result == DialogResult.OK)
{
PdfFilePrinter printer = new PdfFilePrinter("0.pdf", settings.PrinterName);
try
{
printer.Print();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
}
If have a another way to make this, another lib, or, another settings, or another dll... please tell me
if is possible to make a another program in C or C++ and I just pass the parameters to print... I don't know...
I'm needing a lot of it, and urgently :(
Thanks
Alexandre
I found the solution, I have created a new printer and seted on defaults with print in both sides...