Related
How do I build sample code, split into folders in a repo, from a class or tutorial, in Visual Studio?
So - I'm pretty much a noob at C#, I've gone through a lot of tutorials and browsed through some large C# projects from work and built them, and done some other minor things. I'm going through a course on writing testable code on Pluralsight. He has a public Github repo for the code examples, writing-testable-code. I connected to the repo and downloaded it okay into a local Git repo. I was able to download all the packages from NuGet and they are all showing as the version he used (a few have updates, but I figured updating might break things).
I can't figure out how to run this code, build it, or run the tests in it.
What I tried so far
My issue is - I open the solution, and there are a bunch of files and folders - each module/chapter is split into folders (i.e. Module1/Easy, Module1/Hard, Module2/Easy, etc.). I want to build the Module1/Easy folder, including unit test examples, and run the tests.
When reviewing Module1/Easy, it has 3 files that should build okay - the program.cs has a main() and looks like a console app, the Calculator.cs has a simple class, and the CalculatorTests.cs has unit tests built for Nunit. The solution has NUnit, Castle.core, and things from later modules (Moq, AutoMoq, Unity, Ninject, etc.). It didn't seem to have a VS runner, so I added Nunit3TestAdapter - the guy in the course has resharper installed, which I don't, and he was using the Resharper test runner, which would explain why he didn't include it.
I tried setting the "Module1/Easy/Project.cs" file the "Set as Startup Item", since it has a main and looks setup as a console app. However, running it (the "Start" button turned into a "Program.cs" button), it fails saying it can't run a dll. The tests aren't showing up in the Test Explorer like some other small projects I've built from examples.
What's the right way to do this?
I'm not sure where to go from here. On the Build menu is only a "Build Solution" and one about Code Analysis - I'm used to a lot more options here. It feels like I have to turn this folder into a project, maybe? I can always reinstall the packages - but what is the best solution here?
I've run into this before on other book, tutorial, or class repos, but finally decided to figure out how to get this one working. I appreciate any help!
Notes
I'm running Visual Studio Community 2017 at the moment.
I can post some of the files, but the repo is publically available, and not sure exactly what to post to help.
Progress from comments and answers
Per Biker-Dude's answer, I switched the project to build a console app rather than a dll, and now I get a compile-time error for having multiple entry points (i.e. every module and sub folder has its own Main() function and should probably be a separate project).
After #1, I removed all folders but one from the solution, it will then compile, run the tests, etc. - but I eventually want to be able to at least separately compile every sub-folder - what's the best way?
The problem must be that the project must have the output type set to class libraries. Browse through the solution tree and:
Select your class's project> right click > Properties > Application >
Output Type > Console Application/ Windows Application.
This should fix it, if the other things are set up properly.
With the help of BikerDude's answer and stijn's comments, I was eventually able to play around with this and get some things working.
First of all, don't try to exclude any folders in this situation, that will just make things worse! They will still be in your underlying folder/repo, just won't be showing up in your solution anywhere, and you won't be able to create a new folder with the same name (weird decision...). And you'll have to add them back in as individual files - I think.
The best solution (so far)
The best solution seems to be:
Create a new project for each buildable set of files in the solution (in my case, at least one project per "module" folder). I used the ".Net framework console app" project type (right click on the solution, use Add/New Project) to get things to work, but this would depend on the particular course or tutorial repo you downloaded.
Move the folder or sub-folder that has the files you want to build out of the main solution and into the new project - you can click and drag to move it.
Visual Studio will make an empty, pre-formatted file in your project that you likely have to delete - for .Net apps, this is the "Program.cs" file in C#. For one of my folders this file already existed, and I had to delete the new one in order to build. Another folder from a different module was setup more like a library and couldn't build standalone, but this procedure did get me to being able to build the files and that allowed the unit tests to show up in the test explorer and run the tests successfully (which was the main point of that module).
Go to the solution and right-click and choose "Manage Nuget Packages for Solution". As long as all the packages are installed for the main solution, then they will all show up in the list of Installed Packages (you might need to click on the "Installed" tab). You can click on each package in turn, then on the right you can checkmark the new project, and the "Install" button should be available - click it. Repeat for all the packages to install them all. Note that you can cut out some repetition here if you create all the projects you need first, then you can install all of them at the same time in this step (i.e. checkmark all the new projects at once instead of reopening the package manager each time).
You might have to fix the NameSpace - it should be consistent within the files/folders you transferred from the original solution, but if you add any new files to play with things, the Namespace for it will likely not match, and to see classes, etc. in the original files, you'll have to update your Namespace on the new files.
Per BikerDude's answer - After transferring everything to new projects, if you keep anything in the original project that came with the solution, it might not be trying to build the right type of item. You may be able to fix that by right-clicking the project, selecting properties, and adjusting the "output type", but it may not have the options you need. If it doesn't, just create a new project with the right type and transfer the files as above.
After following the above steps, I was able to build each new project I created, using the original files from folders that I moved. Mainly I just needed to build, which enabled all the unit tests that this tutorial/class was focused on, but this allowed me to build the console apps as well, when present.
Thanks for the help from all in pointing me in the right direction!
Having just installed VS2015 Update 1, I discovered the C# Interactive window.
According to the second post in that series, you should be able to import a project from your solution.
When you want to fire up the C# Interactive Window you can just right click your solution and select "Reset Interactive from Project"
However, this option is not available on my project. I'm using a Console Application to test, and have pushed the framework up to 4.6.1 from 4.5. However, this has not worked.
Additionally, I have attempted to manually import the project. Looking at the screenshots on the website, I should be able to do this like this: #r "ConsoleApplication7.exe" but when I do, I get the following exception:
(1,1): error CS0006: Metadata file 'ConsoleApplication7.exe' could not be found
It will pull in via the filepath, e.g:
#r "bin\Debug\ConsoleApplication7.exe"
However, this is a little unwieldly. Especially since whenever you make changes to the classes, it requires a #reset and re-import.
Is there a better way to import Projects into the Immediate instance?
According to the Comment from Jason Malinowski
It doesn't really answer your question, but unfortunately we had to cut the "Reset from Project" command in Visual Studio 2015 Update 1 just because we were crunched getting everything else done. We don't like that it's not there either.
Jason Malinowski
Therefore, at this time (2016-02-04), the "Reset from Project" option is not available.
Projects can be imported manually, by using the following command:
#r "bin\Debug\ConsoleApplication7.exe"
This should work when you are running the REPL with the solution open in the same instance, and when you have only one project.
At other times, you will need to use the full file path
#r "C:\MySolution\ConsoleApplication7\bin\Debug\ConsoleApplication7.exe"
I am fairly new to C#..
I am using Visual Studio 12, the source I am using was last edited in VS 12.. But my problem is that it's throwing me this error:
First of all, my computer username isn't Martin, it is Administratoring - The creator of this project is Martin.. So that's where I guess it's coming from, but I don't know how to fix this.
I have tried editing in Project > Properties > Build > Output Path - And it still doesn't work. I am not too familiar with C#, and I've spent some time searching up for a solution but can't find it anywhere.. Probably because I don't know what I should be searching up (I've tried searching keywords and quotes from the error, but still nothing)
Try these:
Make sure that output path of project is correct (Project > Properties > Build > Output path)
Go in menu to Build > Configuration Manager, and check if your main/entry project has checked Build. If not, check it.
For those with this kind of problem - another solution:
Pay attention also to Warnings when you build solution. For example, I had referenced a dll built with higher version of .NET (4.5.2) than my main project (4.5)
After I referenced a dll built with 4.0 build process was successful.
Please try with the steps below:
Right click on the Visual Studio Project - Properties - Debug - (Start Action section) - select "Start project" radio button.
Right click on the Visual Studio Project - Properties - Debug - (Enable Debuggers section) - mark "Enable the Visual Studio hosting process"
Save changes Ctrl + Shift + S) and run the project again.
P.S. I experienced the same problem when I was playing with the options to redirect the console input / output to text file and have selected the option Properties - Debug - (Start Action section) - Start external program. When I moved the Solution to another location on my computer the problem occurred because it was searching for an absolute path to the executable file. Restoring the Visual Studio Project default settings (see above) fixed the problem. For your reference I am using Visual Studio 2013 Professional.
I had the same problem and unfortunately non of above answers worked for me . the solution that worked for me is :
right click on your startup project and select Properties - Debug and change "start external program: " to the correct path
Done!
Switch Target framework to 4.5.2 or something higher and bring it back to your original version (example: 4.5) then when you build, it will work.
You are not set the startup project so only this error occur. Mostly this problem occur when your working with more project in the single solution.
First right click on your project and "Set as Start Up Project" and/or right click on the start up file inside the selected project and click "Set StartUp File".
I also get this error quite often.
I solve this by modifying the code (doing a very small change), saving it, then building the solution again.
In my case I had added a project to a solution manually, where that project was targeting a higher .NET version than the rest of the projects that were referencing it. Strange... there would normally be a somewhat more verbose, literal and descriptive error in such cases.
There wasn't a real error but there was a warning that said as much.
Go to Project > properties > Debug Tab and set the Launch to "Project"
I had the same problems. I had to change file rights. Unmark "read only" in their properties.
So... it’s mid 2021 and I’m using visual Studio 2019 (version 16.10.2) which is the current version available, on a windows 10 pc.
I had to start a new project and following this steps solved the issue;
When at the menu that says “Create new project”
After you’ve selected your project template it takes you to another menu that says “Configure your new project”
On this menu there’s an option that says “Place solution and project in the same directory”.
By default this option was not checked, so I checked it and it solved the issue.
I had the same problem with visual studio 2015 , and I found that there is reference is marked so I just deleted it , maybe you can delete this reference or reinstall it again
What solved it for me was deleting the line
<ImplicitUsings>enable</ImplicitUsings>
from the project property file. It caused Visual Studio to generate a (useless) file with multiple global using directive.
I faced the same problem , but in my solution i had many projects so in the solution configuration the start up project was by mistake a class library i changed the startup project and then i worked like a charm
right click on the sln => common proprties => choose right startup project .
Please can someone could explain why I get this error and what to do to fix it (or what I'm doing wrong!). The steps I have taken are
Download Source code from http://www.codeproject.com/Articles/16859/AForge-NET-open-source-framework
Opening in VS2010 shows the references cannot be found
Re-Add all 3 references from PlayingCardRecognition\bin\Release so no further warnings
When I try and build or Run I get the following message
To fix this issue, do these steps:
Right click the Project name in Solution Explorer of Visual Studio
Select Set as StartUp Project from the menu
Re-run your project
It should work!
If it did not work, be sure that you have set your start page. If your project is C# Windows Application or C# Console Application, try this:
Right click the Project name in Solution Explorer of Visual Studio
Select Properties
Select the Application tab
In the Output Type drop box
Select the correct application type of your project
Re-run your project and let me know if it won’t work.
The project type set as the Start-up project in that solution is of type ClassLibrary. DUe to that, the output is a dll not an executable and so, you cannot start it.
If this is an error then you can do this:
A quick and dirty fix for this, if that is the only csproj in the solution is to open the .csproj file in a text editor and change the value of the node <ProjectGuid> to the Guid corresponding to a WinForms C# project. (That you may obtain from a google search or by creating a new project and opening the .csproj file generated by Visual Studio to find out what the GUID for that type is). (Enjoy - not many people know about this sneaky trick)
BUT: the project might be a class library rightfully and then you should reference it in another project and use it that way.
.Exe's and .dll's are both assemblies. The key difference is that executeables define an entry point Main which can be invoked by the runtime. The error
"Class library cannot be started directly"
is due to the fact that said .dll's do not have a Main. To fix this issue, change the project type to a Windows application/Console application and define an entry point. Or, add a new project that is of type Windows application/Console application and reference said .dll.
The project is a class library. It cannot be run or debugged without an executable project (F5 doesn't work!!!). You can only build the project (Ctrl+Shift+B).
If you want to debug the code add a console application project (set it as the start up project) to the solution and add the reference to the library.
The project you downloaded is a class library. Which can't be started.
Add a new project which can be started (console app, win forms, what ever you want) and add a reference to the class library project to be able to "play with it".
And set this new project as "Startup project"
The project you've downloaded is a class library, not an executable assembly. This means you need to import that library into your own project instead of trying to run it directly.
Your project type is a class library one would suspect, add a ConsoleApplication or WindowsApplication and use that as your startup object. Reference this project and then access the code.
If you convert the WPF application to Class library for get the projects .dll file.After that convert the same project to the WPF application you get the following error.
Error:".exe does not contain a static main method suitable for an entry point".
Steps to troubleshoot:
1.Include the App.xaml file in the respective project.
2.Right Click on App.xaml file change the build action to Application Definition
3.Now Build your project
Goto the Solution properties -> on Build right side you see the startup project type. here you need to select the console appication/windows appication.
If you got this issue (got it in Visual Studio 2017 RC), and you don't get any of the things listed by Mak post from step 3 onward "4 In the Output Type drop box....", it is because you made a Class Library app when you want to create a cross platform app, so here is the solution :
1 Start a new project
2 select Visual C# and cross-platform app.
3 select cross-platform app (Xamarin and native app)
4 select blank form.
From then , right click, select as startup project and build as mentioned by Mak, and it should work.
If you can afford to start from scratch, it could do the trick as it did for me.
This could do the trick for the main issue as well, but must be adapted to your current version of Visual Studio ("Xamarin.forms portable" for visual studio 2015 for example).
Bye!
VS -> Debug -> Attach unity debugger -> double click project
Set your api project to a startup project:
Right click the api Project than choose Set as startup Project.
Just right click on the Project Solution A window pops up. Expand the common Properties. Select Start Up Project
In there on right hand side Select radio button with Single Startup Project Select your Project in there and apply.
That's it. Now save and build your project. Run the project to see the output.
_Sarath#F1
To fix this issue, do these steps:
Right click the Project name in Solution Explorer of Visual Studio
Select Set as StartUp Project from the menu
Re-run your project It should work!
I have created a new VS 2010 extensibility package. So far, all I want to do is have the user press a button and fill a listview with the entire contents of the solution. I have the following code:
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.10.0");
foreach (Project project in dte.Solution.Projects)
{
foreach(ProjectItem pi in project.ProjectItems)
{
listView1.Items.Add(pi.Name.ToString());
}
}
This does seem to work, however, it populates the list with the contents of the solution with the package in it and not the experimental instance that is launched when this is run. Am I instantiating the reference wrongly?
GetActiveObject method returns first process instance of DTE, not
caller DTE. (in Visual Studio SDK 2010 project on Visual Studio 2010,
type F5 to execure experimental hive may fail)
Look at here and here for more details...
No - you need to use ProjectItem.SubProject to get to what you want... depending on the solution structure some recursion could be needed... for some sample code doing nicely all this see http://www.wwwlicious.com/2011/03/envdte-getting-all-projects.html