Get name of branch into code - c#

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.

Related

How do I publish a Solution (.sln File) using Process in 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?

Send text from C# .NET application to running Visual Studio Code instance

Is it possible to send text to a Visual Studio Code instance via the command line interface of VSCode in order to display the text in a new unsaved tab?
At the moment i managed to accomplish my task via writing the text to a file and then sending the file.
My solution at the moment is like this example:
var result = "This text wants to be displayed in a new visual studio code tab.";
File.WriteAllText("C:\\Temp\\test.txt", result);
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Microsoft VS Code\\Code.exe", "C:\\Temp\\test.txt");
process.StartInfo = startInfo;
process.Start();
I also tried passing the string itself to the CLI.
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Microsoft VS Code\\Code.exe", result);
The result is that a new Tab for every word in the string is created in the Visual Studio Code instance.
This tells me it may be possible to pass something to the CLI other than a file.
Does anybody know if and how it is doable?
This does not appear to be possible currently (as per their command line documentation: https://code.visualstudio.com/docs/editor/command-line#_opening-files-and-folders). When you pass the string it is interpreting it as mutiple files and opening a tab for each file (which are created if they dont exist).

C# - Executing a exe file which further invokes a msi file

I have a .exe file which checks the system architecture and based on the system architecture it calls the corresponding msi file.
I am trying to run this exe file from C# using the code below
Process process = new Process();
process.StartInfo.FileName = "my.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.WorkingDirectory = "path//to//exe//directory";
Console.WriteLine(process.StartInfo.WorkingDirectory);
process.Start();
process.WaitForExit();
The exe is getting invoked. i can see the application logs and there are no errors in the logs.
But the msi is not getting started or installed.
When I try to run the same exe file manually the msi is installed.
Note: There are other dependency files for this my.exe which are placed in the same directory.
Why am i not able to install the msi from C# program while i am able to do this manually.
I am running the VisualStudios in administrator mode.
You need to execute .exe (and msi) as an administrator.
To ensure that, use:
process.StartInfo.Verb = "runas"
Also, try it removing quiet arguments to see any possible errors.
Is "my.exe" installing your MSI if you call it, isn't it?
I got this resolved after i added Thread.Sleep(). before "process.WaitForExit()"

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.

Accepting EULA for a quiet install of EXE file

I am working on packaging up some installers for internal use. I have the uninstall working fine with a passive switch.
As for installation, the MSI's that do not have an EULA work perfectly with the passive switch showing progress.
The EXE's that contain an EULA are the problem.
I am trying to find a way to accept the EULA without user input - note I do not have access to changing the public properties of the EXE's to set the ACCEPTEULA=1
the base I am working with right now is...
start = new ProcessStartInfo();
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
start.Arguments = s.args; //curent argument /qn
start.FileName = tempDir + "/" + s.executable;
start.CreateNoWindow = true;
While this code works perfectly fine with the msi's it does not work with the exe's as they all contain EULAs.
When using an exe you need to preface /qn with /v making it:
setup.exe /v/qn

Categories