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

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).

Related

.net Process.Start Method not Launching Excel File in Excel 2013 only when launched from whithin excel com addin

System.Diagnostics.Process.Start() not launching an excel file In Excel 2013 and that too launched from inside excel comaddin.
string filename = #"C:\Users\centraluser\AppData\Roaming\STUDIO\CENTRAL\d7c98719-7aa9-4e7e-8fb6-
bd5a5b23f560\New Microsoft Excel Worksheet.xlsx";
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = filename;
var process = Process.Start(processStartInfo);
It hangs after Process.start and waits for some time and then returns with a null process value.
While the same file gets launched if I use the same code and try to open the file from a console application.
And all this behavior is only occurring with Excel 2013 only.
Kindly help on the same.
From MSDN:
Set the UseShellExecute property to specify whether to start the
process by using the operating system shell.
Set the processStartInfo.UseShellExecute to true value.

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.

How to run coded UI test with MSTest.ese without installing Visual Studio 2010

I am working with Coded UI automation. The issue is to customize test case execution. I cannot use TFS or Lab agent or any other tool. The test components (DLL) are executed through a customized UI developed using C# on a 64bit machine with Win7.
I am able to run test case now through the code below:
string str = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\MSTest.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(str);
startInfo.Arguments = " /testcontainer:TestProject1.dll";
Process.Start(startInfo);
But when I want to install this application to another machine I need to install VS2010. This is what I don't want. I have gone through several doc on the internet, but none of them have a clear picture.If any one can help me with a solution.How to make it work.
I am using Visual Studio Agents 2012 to execute coded UI test without visual studio installed, It works fine for me. you can specify test container,test method, result file name and even test setting file by a bat file and call the MStest.exe in test agents.
Please refer to this
Link
I have completed your requirement.See bellow
public void test()
{
string testcase = "/testcontainer:\"D:\\testcase\\s\\CodedUITest.dll\"";
string Path = "C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\mstest.exe";
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(Path, testcase);
myProcessStartInfo.UseShellExecute = false;
try
{
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Trying to run slui.exe from within a method using ProcessStartInfo and Process

I'm having an issue with running slui.exe from a method in c#. I'm using the code:
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Windows\System32\slui.exe");
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
but I keep getting a Win32Exception: 'The system cannot find the file specified'.
If I change the ProcessStartInfo to: (#"C:\Windows\System32\cmd.exe") it will launch just fine.
Is there something with running slui.exe in this context that is breaking?
I'm certain that the file is in the directory specified, so I'm stumped as to what may be going wrong here.
Any ideas how to call slui.exe from a c# method?
Slui.exe is only available as a 64-bit program on Windows x64. Your hard-coded path c:\windows\system32 will get re-directed to c:\windows\syswow64 when you run as a 32-bit process. And thus won't find the file.
Project + Properties, Compile tab, change the Platform target setting to "AnyCPU". Repeat for the Release configuration. And use Environment.GetFolderPath() to ensure it still works when Windows isn't installed to c:\windows.

The filename, directory name, or volume label syntax is incorrect, c#

i've written a console application deploy.exe which runs a batch script.
Process p1 = new Process();
p1.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "installer.bat";
p1.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p1.Start();
p1.WaitForExit();
p1.Close();
the installer.bat conatins the following command.
\shared1\lists\list1.cmd
If i run the executable byitself it runs successfully.
However i needed it to run in a windows installer project. So i made a setup and deployment project and added the deploy.exe successfully as custom action upon install.
It runs fine but when it starts to execute the command i get this error
"The filename, directory name, or volume label syntax is incorrect".
any help?
Try printing out what the value of AppDomain.CurrentDomain.BaseDirectory is. It may not be where installer.bat is when you are installing it.
Also, you tried adding the bat file to a custom action (if that is even possible)?
And, would it possible to move what is in the bat to the exe?
Is it a problem in your batch file?
Check this:
\\shared1\\lists\\list1.cmd
should probably be
\\shared1\lists\list1.cmd
Note the extra \ chars in your original command. That would cause the batch file to give that error.
the error seems to be inside the script which was being executed. It contained environment variables %kind%, which were not acceptable by the installer for some reason. So it was working properly outside the installer and not properly when the installer was calling it.

Categories