## Run SLN file (visual studio solution) from win form application - c#

### SSIS script task add existing file to project as link causes binary not to be found

I thought this would be simple, but I guess not.
Here is what is happening.
I create a new package and add a script task to it.
I open up the script task and edit the script (C# 2010).
In the solution explorer, I right click the project name (ST_xxxxxx).
I select "Add->Existing Item".
In the file explorer, I navigate to my documents folder and select (not double click) my "test.cs" which is just an empty class. At the bottom right of the file explorer window, next to the Add button, I click the down arrow and select "Add as Link". To test, I select "Build->Build ST_xxxxx". Build succeeds.
I save, then close the script task VS editor. Then I click ok on the script task editor window.
A "Script Error" window appears. "Scripts contained in the package have compilation errors".
If I do save it, The error on the task says "Binary code for the script is not found"
Any ideas how to get around this?
I can go through that same process and select "Add" instead of "Add as Link" and it works fine. But that creates a copy of the file and has to be updated manually. I want to link to my common cs files that I use all over and not have to update them.
Any help would be appreciated.

Found the solution. Just drop the dlls into a sharedrive accessible by the dev environment and production servers. Then add this code to the script task before (or after) the Main (entry point) function. Then add the same dll(s) as a reference to the project. Works like a charm.
static ScriptMain()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("ssisHelper"))
{
string path = #"c:\temp\";
}
return null;
}
https://blogs.msdn.microsoft.com/dbrowne/2014/06/25/how-to-load-an-assembly-in-a-ssis-script-task-that-isnt-in-the-gac/

### Launching a Console Applications from a MenuStrip (Visual C#)

I want to have a Windows Form Application use a menustrip with three options to launch a console application. The console application is a .exe file built in C# in Visual Studio with some basic code for as school project. The console application does not need to return any values, it only needs to run and allow the user to use it. This is what the form will look like: Menu Application
I have tried importing the System.Diagnostics.Process.Start namespace with Process.Start#("Path of file") in my menu item click event method to launch my C# console application but have not been successful. I am getting a "Win32Exception was unhandled: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll. Additional information: The system cannot find the file specified"
Here is the code in the menu item click event:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lesson13LabCToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
Any ideas on what I am doing wrong?

The error is clear The system cannot find the file specified. Check the path of file.
Also remove the starting \ in the path

Remove the backslash at the beginning of your path (before the drive letter).

### How to make a dialog appear when Visual Studio is started. - VS Extension

I am building a Visual Studio extension and I am stumped on how I would show a dialog when visual studio is started.
The main use for it is going to be when Visual studio starts my extension will check for updates if an update is found a dialog appears.
Information on extensions is very scarce so I have no idea how to do this. I am using C#.
Edit: I have tried adding the code in the package file that has all of the command code/callbacks into it's initialize event and it shows the dialog before visual studio appears to have even loaded and does not continue to load until I close it. I feel like I am getting closer though.
Is their an extension start up command I can create in VSCT file, kind of like they have for menu items?

I was able to figure out my problem. It took alot of trial and error due to the lack of info. I had originally tried the OnStartupcomplete() event but it was not working for me, hence I came here. The reason why it was not working was because the DTE object wasn't initialized at that point. So I was able to create the object and add the handler.
protected override void Initialize()
{
//DTE gets called
var dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
_EventsObj = dte.Events.DTEEvents;
_EventsObj.OnStartupComplete += OnStartupComplete;
}
public void OnStartupComplete()
{
//This is the code to launch the dialog.
EvaluationDialog EvalForm = new EvaluationDialog();
EvalForm.ShowDialog();
}

I'm assuming you are using a Visual Studio Add-in project. If you want just a message box, in the Connect.cs file, add a reference to System.Windows.Forms and a using statement:
using System.Windows.Forms;
In the OnConnection method:
public void OnConnection(object application,
ext_ConnectMode connectMode,
object addInInst, ref Array custom)
{
MessageBox.Show("message box");
// or you could use your on dialog class
var myDialog=new MyDialog();
myDialog.Show();
// ...
}

We are using OnAfterOpenProject. You can check for updates and bring up a dialog if found.

### How to Detect if Visual Studio IDE is closing using VSPackage?

I'm writing a VS Package where I need to store the time when the user start my package, and when the user close the Visual Studio.
The problem is, I don't know how to get the closing event for the Visual Studio. Can anyone give me any information about how to detect if the VS is closing?
Note: When I search from the internet, I got the following similar problem to mine: How do you cancel a ToolWindowPane or Visual Studio IDE close operation via a VSPackage?, but when I try it, this solution is to detect and do something when the Package window is closed, and cannot detecting when the Visual Studio is closed.
Any help is really appreciated.
Thanks

Just to make it explicit and close this problem.
This is the snapshot of code to check if VS is closing:
// Create 2 variables below
private DTE2 m_applicationObject = null;
DTEEvents m_packageDTEEvents = null;
Then in Initialize add this:
// Link the Event when VS CLOSING
m_packageDTEEvents = ApplicationObject.Events.DTEEvents;
m_packageDTEEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(HandleVisualStudioShutDown);
Two other methods that you need:
public DTE2 ApplicationObject
{
get
{
if (m_applicationObject == null)
{
// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));
m_applicationObject = dte as DTE2;
}
return m_applicationObject;
}
}
And
public void HandleVisualStudioShutDown()
{
MessageBox.Show("Exiting Visual Studio. Bye");
}

### Console.WriteLine does not show up in Output window

I have put some Console.WriteLine calls in to test, but they aren't appearing in the output box?
public static ArrayList myDeliveries = new ArrayList();
public mainForm(){
InitializeComponent();
}
private void mainForm_Load(object sender, EventArgs e){
if (!File.Exists("../../MealDeliveries.txt")){
return;
}
//first line is delivery name
string strDeliveryName = sr.ReadLine();
Console.WriteLine("Test content");
while (strDeliveryName != null){
//other lines
Delivery d = new Delivery(
strDeliveryName,
);
//check for further values
}
}
displayDeliveries();
}
private void displayDeliveries(){
lstDeliveryDetails.Items.Clear();
Console.WriteLine("Test content");
Console.WriteLine(mainForm.myDeliveries.Count);
foreach (Delivery d in mainForm.myDeliveries){
}
}
Can anyone help??

If you intend to use this output in production, then use the Trace class members. This makes the code portable, you can wire up different types of listeners and output to the console window, debug window, log file, or whatever else you like.
If this is just some temporary debugging code that you're using to verify that certain code is being executed or has the correct values, then use the Debug class as Zach suggests.
If you absolutely must use the console, then you can attach a console in the program's Main method.

Console outputs to the console window and Winforms applications do not show the console window. You should be able to use System.Diagnostics.Debug.WriteLine to send output to the output window in your IDE.
Edit: In regards to the problem, have you verified your mainForm_Load is actually being called? You could place a breakpoint at the beginning of mainForm_Load to see. If it is not being called, I suspect that mainForm_Load is not hooked up to the Load event.
Also, it is more efficient and generally better to override On{EventName} instead of subscribing to {EventName} from within derived classes (in your case overriding OnLoad instead of Load).

If you want Console.WriteLine("example text") output to show up in the Debug Output window, temporarily change the Output type of your Application from Console Application to Windows Application.
From menus choose Project + Properties, and navigate to Output type: drop down, change to Windows Application then run your application
Of course you should change it back for building a console application intended to run outside of the IDE.
(tested with Visual Studio 2008 and 2010, expect it should work in latter versions too)

If you are developing a command line application, you can also use Console.ReadLine() at the end of your code to wait for the 'Enter' keypress before closing the console window so that you can read your output. However, both the Trace and Debug answers posted above are better options.

When issue happening on Mac VS 2017 (Which I faced).
Go to Project >> "Your Project name" options.
An option window will pop up
Go to RUN >> Default menu option
Tick the "Run on external console" option TRUE and say OK
Run your application code now.

Using Console.WriteLine( "Test" ); is able to write log messages to the Output Window (View Menu --> Output) in Visual Studio for a Windows Forms/WPF project.
However, I encountered a case where it was not working and only System.Diagnostics.Debug.WriteLine( "Test" ); was working. I restarted Visual Studio and Console.WriteLine() started working again. Seems to be a Visual Studio bug.

Old Thread, But in VS 2015 Console.WriteLine does not Write to Output Window If "Enable the Visual Studio Hosting Process" does not Checked or its Disabled in Project Properties -> Debug tab

Select view>>Output to open output window.
In the output window, you can see the result