i have two WPF Application say WPFApplication1 and WPFApplication2 , WPFApplication1 has reference of WPFApplication2 , now i am calling one class of WPFApplication2 in WPFApplication1 using reference, and from WPFApplication2 class creating App class instance of WPFApplication2 , so i am facing issue like below
Error
Please help me out with this, THanks in Advance
Look at Process.Start and ProcessStartInfo
Here is an example for Console application. But there are not different how to start other application.
By ProcessStartInfo you can specify many things like Are you has to wait it? Or run without waiting. Is you want to redirect output? And so one.
using System.Diagnostics;
class Program
{
static void Main()
{
LaunchCommandLineApp();
}
/// <summary>
/// Launch the application with some options set.
/// </summary>
static void LaunchCommandLineApp()
{
// For the example
const string ex1 = "C:\\";
const string ex2 = "C:\\Dir";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
}
Related
This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed last year.
I wanted to run CMD commands using C#. Therefore I copied some Code:
using System.IO;
using System.Diagnostics;
namespace Test
{
internal class Program
{
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("#echo off");
sw.WriteLine("title Test");
}
}
}
}
}
So. After I run some code like changing color etc, I want to enter normal cmd commands myself but the Window instantly closes.
Thanks for any Help :D
Use ReadKey()
Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed.
As I aksed in another post, I am trying to automate running processing ide from c#. Finally I found the way to run the processing sketch via cmd, with setting the installed processing folder in the path of evironment variable.
I find it works with inputting command directly in cmd.exe, but when I want to do the same thing through some c# code in Visual Studio, it doesn't run the .pde file.
Here is the code,
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Runprocessing
{
static void Main()
{
Process process = new Process();
ProcessStartInfo stinfo = new ProcessStartInfo();
stinfo.FileName = "cmd.exe";
stinfo.Arguments = "/c"+"processing-java --run --sketch=D:\\pw --output=D:\\pw\\output";
stinfo.CreateNoWindow = true;
stinfo.UseShellExecute = false;
process = Process.Start(stinfo);
process.WaitForExit();
process.Close();
process.Dispose();
}
}
}
My question is, how should I properly use processing-java to activate the sketch. because here I am stating
stinfo.FileName = "cmd.exe";
stinfo.Arguments = "/c"+"processing-java --run --sketch=D:\\pw --output=D:\\pw\\output";
Is this the right way to use processing-java in cmd?
I am testing a C# WPF program that requires elevated privileges, that loads without any delay if I am logged on as an admin, but if I am logged on as a standard user (99% of time) then there is a delay of about 30 seconds before the UI appears.
Using the same elevation code in a C# console app and in a c# Winforms app, there is no delay in loading, so I know that the code works.
So, can anyone explain to me why is there a delay associated with WPF; and is there a workaround?
Here is the code from app.xaml.cs ( the remainder of the project is as genereated by VS2010)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Security.Principal;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel;
using MyNewServiceLib;
using System.Runtime.InteropServices;
namespace WhySoSlow
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (!IsAdmin())
{
StartAsAdmin(e);
Application.Current.Shutdown();
}
else
{
MainWindow = new MainWindow();
MainWindow.SizeToContent = SizeToContent.WidthAndHeight;
MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
MainWindow.Show();
}
}
bool IsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);
return p.IsInRole(WindowsBuiltInRole.Administrator);
}
private void StartAsAdmin(StartupEventArgs e)
{
string[] args = e.Args;
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
Uri uri = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);
startInfo.FileName = uri.LocalPath;
startInfo.Arguments = String.Join(" ", args);
startInfo.Verb = "runas";
Process p = Process.Start(startInfo);
}
catch (Win32Exception exception)
{
MessageBox.Show(exception.Message);
}
}
}
}
Further info
Weirdly, if I start the program from a cmd prompt running under NT Authority\System, there is no delay starting up the UI. After that one successful start, every further start, from whatever prompt, be it standard user prompt, run as administrator, the program starts without delay; UNTIL that is, I log off from the session.
After logging on again to a new (standard user) session, all attempts to start the program result in the 30 second delay before showing the UI.
I can only think that this is some kind of UAC bodge by Microsoft, that is hindering the startup of wpf.
Instead of using this code, have you tried utilizing a manifest file? Running an application as admin is one of the things you can do with it.
this might help https://msdn.microsoft.com/en-us/library/ms742884.aspx
Greetings SO Community
I'm writing a c# program to manage the uwfmgr registry exclusions.
I execute the following command.
uwfmgr registry get-exclusions.
Afterwards, when I want to read the registry, I get an exception with the message:
No more data is available.
This problem persists until the next reboot.
I suspect, that uwfmgr does not release the registry key properly.
I've made a minimal example to demonstrate the error.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName = "C:\\Windows\\Sysnative\\uwfmgr.exe";
proc.StartInfo.Arguments = "registry get-exclusions";
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
string[] subkeys = Registry.LocalMachine.GetSubKeyNames();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
}
}
}
Info:
CLR is ".Net Framework 4.5".
Target is "Any CPU".
The executing system is "Windows Embedded 8.1 Industry Pro".
The architecture is 64bit.
Now, is there any possible way to force uwfmgr to release the registry?
PS: This is my first post. I tried to post according to the rules, but if I forgot something, please kindly remind me. Thanks.
I found this thread very helpful and I would like to ask Ian Norton about his wrapper. HERE is the link to the wrapper I was trying that IanNorton had posted. I'm not sure if this is the right place to ask and I also don't want to create a new thread when it pertains to his response. So I will go ahead and suffer whatever backlash may come my way.
I am currently trying to use your wrapper and i cannot seem to seem to get it to trigger anything when I run it. I do not want to use options as i just want to set this as an .exe that runs on a timer. Quite simply, I would like to use the p4 opened -a cmd and print the out puts to a file. That is it. Any help would be greatly appreciated by this NooB.
Thank you very much!
This is as far as I got with just using the Command Line. Unfortunately I could not output my info to a text file.
using System;
using System.Diagnostics;
using System.IO;
namespace P4OpenCMD
{
class P4Opened
{
// Main begins program execution.
static void Main(string[] args)
{
string temp = string.Empty;
if (temp != string.Empty)
{
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
StreamWriter sw = p.StandardInput;
using (sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("set P4PORT=####");
sw.WriteLine("set P4USER=####");
sw.WriteLine("set P4CLIENT=####");
sw.WriteLine("set P4PASSWD=####");
sw.WriteLine("p4 opened -a //Depot/...");
sw.WriteLine("pause;");
}
Console.WriteLine();
}
sw.Close();
p.WaitForExit();
p.Close();
}
}
}
}
If you do this, you should be in good shape:
info.FileName = "cmd.exe";
info.Arguments = "/c p4 opened > opened.txt";