p4 opened -a in C# Perforce Wrapper or P4 API - c#

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";

Related

Allow user input in CMD using C# [duplicate]

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.

How to run multiple cmd commands from c# console app

I'm looking to automate nupkg creation in a c# app. I'm aiming to include nuget.exe in my project and use System.Diagnostics to launch cmd.exe as a process and then pass the required commands, which would be 'cd project\path\here', 'nuget spec something.dll' and 'nuget pack something.nuspec'.
The code I have so far is:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\System32\cmd.exe", #"mkdir testdir");
p.StartInfo = info;
p.Start();
Console.ReadLine();
However, it doesn't even create the testdir, and I've got no idea how to chain those commands. There is a method called WaitForInputIdle on my p Process, but it raises events and I've got no idea how to handle those to be honest.
A perfect solution would also let me read output and input. I've tried using StreamWriter p.StandardInput, but then there's the problem of checking whether a command is finnished and what was the result.
Any help would be much appreciated.
Edit: Success! I've managed to create a directory :)
Here's my code now:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\System32\cmd.exe");
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
sw.WriteLine("mkdir lulz");
}
Still no idea how to await for input and follow up with more commands, though.
You can do this by three ways
1- The easiest option is to combine the two commands with the '&' symbol.
var processInfo = new ProcessStartInfo("cmd.exe", #"command1 & command2");
2- Set the working directory of the process through ProcessStartInfo.
var processInfo = new ProcessStartInfo("cmd.exe", #"your commands here ");
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = path;
3- Redirecting the input and output of the process. (Also done through the ProcessStartInfo).This is required when you like to send more input to the process, or when you want to get the output of the process
Also see this answer

Multiple process commands fail to get back any response

Having a challenge being able to send commands to cmd.exe from via the C# Process class.
Basically I want to call R.exe and then send it several commands to stage the data before I run some R functions and then pull out the result.
But I can't get the result back from a simple 'dir' statemenet :(
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
string pathToR = #"C:\Program Files\R\R-3.3.1\bin";
p.StartInfo = info;
p.Start();
List<string> output = new List<string>();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("cd " + pathToR);
sw.WriteLine("dir");
while (p.StandardOutput.Peek() > -1)
{
var peekVal = p.StandardOutput.Peek();
output.Add(p.StandardOutput.ReadLine());
}
}
}
foreach (var line in output)
{
Console.WriteLine(line);
}
p.StandardInput.Close();
p.StandardOutput.Close();
p.WaitForExit();
p.Close();
Console.ReadLine();
Output:
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
c:\users\micah_000\documents\visual studio 2015\Projects\RStagingTestApp\RStagingTestApp\bin\Debug>cd C:\Program Files\R\R-3.3.1\bin
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
I've seen several variations on this result, but I've never seen any kind of response from my commands :(
A lot of places say the async reads work best. I've seen some persuasive, comprehensive explanations for this, but I haven't been able to get it to work for me.
This one seemed to work. It might have been setting AutoFlush to true. Or just reading once at the end.

How to pass arguments to an already open terminal via System.Diagnostics.Process()

I have been messing around with triggering a bash script via C#. This all works fine when I first call the "open" command with arguments which in turn opens my .command script via Terminal.
Once the "open" command is used once Terminal or iTerm will remain open in the background, at which point calling the "open" command with arguments then has no further effect. I sadly have to manually quit the application to trigger my script again.
How can I pass arguments to an already open terminal application to restart my script without quitting?
I've searched online ad can't seem to work it out, it already took a good amount of time solve the opening code. Your help is much appreciated.
Here is the C# code I'm using to start the process:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "open";
p.StartInfo.WorkingDirectory = installFolder;
p.StartInfo.Arguments = "/bin/bash --args \"open \"SomePath/Commands/myscript.command\"\"";
p.Start();
Thanks
EDIT:
Both answers were correct, this might help others:
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
startInfo.WorkingDirectory = installFolder;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("echo helloworld");
process.StandardInput.WriteLine("exit"); // if no exit then WaitForExit will lockup your program
process.StandardInput.Flush();
string line = process.StandardOutput.ReadLine();
while (line != null)
{
Debug.Log("line:" + line);
line = process.StandardOutput.ReadLine();
}
process.WaitForExit();
//process.Kill(); // already killed my console told me with an error
You can try:
before calling p.Start():
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
// for the process to take commands from you, not from the keyboard
and after:
if (p != null)
{
p.StandardInput.WriteLine("echo helloworld");
p.StandardInput.WriteLine("executable.exe arg1 arg2");
}
(taken from here)
This is what you may be looking for :
Gets a stream used to write the input of the application.
MSDN | Process.StandardInput Property
// This could do the trick
process.StandardInput.WriteLine("..");

externally access processing via cmd

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?

Categories