How do I publish a Solution (.sln File) using Process in C# - c#

I am trying to publish a .NET Core 3.1 Solution using the code below. It works fine with the variables given here.
(It is a Console Application that is being debugged and a WinForm that is running this code).
string batchFilePath = #"D:\Program Files (x86)\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat";
string solutionFilePath = #"D:\Documents\Visual Studio\Visual Studio 2022\Projects\Selfmade\Selfmade.sln";
I run the following code and it will debug the solution in the default debug folder.
Process process = new Process();
var command = Environment.GetEnvironmentVariable("ComSpec");
command = #"" + command + #"";
var args = string.Format($"/S/K \" \"{batchFilePath}\" && devenv /build Debug \"{solutionFilePath}\" && exit \"");
process.StartInfo.FileName = command;
process.StartInfo.Arguments = args;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
However this code debugs the solution. I am looking for a simulair way but then to publish this solution. The only issue is that I tried using the dotnet.exe but I cannot seem to figure out any way to publish this solution using a process.
My question is, Is there a way to use a code simulair to this but then for publishing the solution and how do you do it?

Related

Use powershell to run an installation from C#

The problem is installation shutdown and uninstall the windows service, but stopped and hang in there in the background. If I run the script outside the code, everything works. just wondering if it is possible to call installation this way in the code and how?
The application is running as Windows Service. This service will call a powershell script to run an installation. In the installation, it will shutdown the Windows service and re-install. the code is below:
string filepath = Path.Combine(requiredFiles.First().Value.Path);
script = $"Start-Process -FilePath {filepath} -ArgumentList \"/S
/Q localhost\"";
_Log.Info($"powershell running: {script}");
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = #"C:\windows\system32\windowspowershell\v1.0\powershell.exe";
process.StartInfo.Arguments = script;
process.Start();
process.WaitForExit();
Thank you very much for your help.
I am pretty sure RunspaceFactory will not work.

Run .net core 2 console app in Pre-build event

I have a .net core 2 project where I want to run another .net core 2 project in the pre-build events.
All my previous projects were developed using framework 4.x, which upon building generated .exe files. This .net core does not. How can I achieve the same result as the old code from my previous projects:
$(MSBuildProjectDirectory)\..\MyProjectName\bin\$(Configuration)\MyProjectName.exe
Thank you in advance !
Actually i had the same issue before then i tried another solution, run the 2nd .net
core app during the startup of the 1st one.
as we can run .net core app from command prompt if we navigated to the directory that contains (.csproj) and opened new command prompt window then run command
dotnet run
here is the code
public static void ExecuteCommand(string command, string workingDirectory)
{
var processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = workingDirectory;
processStartInfo.FileName = "cmd.exe";
processStartInfo.Arguments = "/k " + command;
processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = true;
Process proc = Process.Start(processStartInfo);
}
then you can call it like that
// the directory that contains (.csproj)
var appWorkingDirectory = #"D:\\Examples\\MyApp";
ExecuteCommand("dotnet run", appWorkingDirectory);
notice that will open a show the command prompt window and if you want
to hide it just set
processStartInfo.CreateNoWindow = true;
hope this can help you

Get name of branch into code

I have a question about passing the branch name to my code as a string.
So we are using a git repository and the branch number also refers to the staging environment where the build is placed. Meaning, if my branch is 001, my url is 001.test.myapplication.com.
I am writing automated tests which are executed on the staging environment of the branch. My question is, is it possible to pass the branch number to my code so I can make it part of the URL I want to test on?
I am using visual studio 2017, selenium and specflow.
I actually found a great solution which perfectly works. Sharing so in the future, others can use it too if they need to.
ProcessStartInfo startInfo = new ProcessStartInfo("git.exe");
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = "dir Here";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = "rev-parse --abbrev-ref HEAD";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string branchname = process.StandardOutput.ReadLine();
The usual way to do this is to generate a C# file containing this information as part of your build step.
There are already several good answers here:
Embed git commit hash in a .Net dll
The git command in the earlier answer didn't work for me anymore in 2022. I use the following pre-build event in Visual Studio project properties:
git.exe branch --show-current > "$(ProjectDir)\Properties\BuildDate.txt"
echo %date% %time% >> "$(ProjectDir)\Properties\BuildDate.txt"
Define BuildDate.txt as an imported resource in the project's resource file, then use it like a regular resource string at runtime.

run bundled python script in clickonce application

I'm trying to run a Python script from a WPF application, but I can't figure out how to find the file on the client machine. I know ClickOnce installs the files to AppData\Local\, but that seems like a poor way to search for the script.
In my code, I'm starting a shell as follows:
var p = new Process();
p.StartInfo.FileName = #"C:\Python27\python.exe";
p.StartInfo.Arguments = String.Format("{0} {1}", ScriptName, args);
//p.StartInfo.WorkingDirectory = #"<path\to\project>";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
On my development machine, the WorkingDirectory parameter makes it all work if I set it to the project path because that's where the Python script resides.
I included the script in the ClickOnce file manifest and I've verified that it ends up in each version of the application deployment. I've also verified that the file ends up on the client machine in the obfuscated hierarchy, so everything is in place except I can't get my WPF application to actually run it. I could always copy the file to a known place on the machine, but that completely defeats the point of ClickOnce.
Any ideas would be greatly appreciated.
I got this to work, but I'm not 100% sure how. Regardless, here's what I did in case it helps someone else.
Don't set the process working directory (already commented out in my question)
Set the Python script properties as follows:
Build Action = Content
Copy to Output Directory = Copy Always
In the Publish tab under Project Properties, set the script to Include in Application Files.
With the above changes, my application deploys correctly on client machines.

C# code to run my installer.exe file in silent mode, in the background, [duplicate]

This question already has answers here:
How to run silent installer in C#
(2 answers)
Closed 9 years ago.
I have this C# code:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "–s –v –qn";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = desktopPath + "\\" + "MyInstaller_7.1.51.14.exe";
Process.Start(psi);
The first line simply grabs the path of my desktop and the rest attempts to run an installer exe file in silent mode. By silent mode I mean, in the background, without the install wizard, or any UI of any sort during installation. The –s –v –qn arguments are there so that that the installation runs in silent mode.
The problem is that when I run the command equivalent of the above in the command prompt, which is this:
C:\Users\ME\Desktop>MyInstaller_7.1.51.14.exe -s -v -qn
The installer runs as wanted, in silent mode.
Unfortunately, the problem is that trying the same thing in C# with the above code does NOT run the installer in silent mode. The installation wizard DOES appear, which is BAD for by purposes.
I'm thinking maybe I need to run this like a service via C# or under the 0 id of the users. Or with an -i switch. I'm not really sure. Can anyone help??
Just for clarification, my question is, how do I write C# code to run my installer.exe file in silent mode, in the background, with no visible UI?
Please help.
This is the correct answer:
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = newRenamedFile;
psi.UseShellExecute = false;
Process.Start(psi);
The issue was the switches were missing the forward slashes.

Categories