How do I compile specific sourcecode files in Visual Studio? - c#

So in VS, I create a project, right? Let's say I'm following a tutorial that has extra code that I don't need that only serves to show examples for looping, threads, etc. - then moves onto the real code. I take notes of everything so I usually save a copy of my source code with a different name as future notes. Now the only thing with projects in VS or any other C# compiler is that if you save the file AS "..." or make a new file, sometimes I find that the compiler will now compile the new code or the old code (it gets me so mixed up).
I get why it does this though, to link to other sourcefiles right? Well anyway, all I want to know is how do I compile a different C# file within my project folder? I don't want to create a new project, it just makes it harder for me to organize.

Assuming you want to include a lot of files into project bu only compile one you can just change "Build action" to "none" from "compile" for CS files you don't want to build.

Related

Nuget-deployed C# files excluded from Resharper analysis

I'm having trouble understanding a behaviour of Nuget. I've created a basic package that deploys a single Test.cs file into an "HtmlHelpers" folder in a project. I'm following the "convention based working directory" method described on the Nuget site.
The issue I'm having is Resharper file analysis isn't enabled for this file (the file doesn't compile currently, I've left off a semicolon):
But, if I rename the file from "Test.cs" to "Test2.cs" then Resharper analyses the file correctly showing the syntax error:
Has anyone got any idea what's happening here? Is there a list somewhere of Nuget-supplied source files that are then excluded from Resharper analysis? The file properties are the same as for any other C# file, set to Build Action "Compile" etc.
EDIT1
To answer questions from Stephen below, this is Resharper 8, I'm not currently on 9. I've tried closing and re-opening the solution and excluding and re-including the file, neither of which help.
Interestingly, with analysis working correctly on the renamed "Test2.cs", if I then rename it back to "Test.cs" the analysis switches off again :S
EDIT2
Just to add some more info to this, if you Nuget-deploy C# files using the .pp extension, Resharper analysis works correctly on the resulting .cs files. Go figure.
ReSharper excludes source code that has been delivered as part of a NuGet package from analysis - it treats it as third party code that you did not write and do not want to maintain. E.g. it won't show any inspection results for files such as jquery.js or angular.js - you don't own these, don't intend to maintain them as part of your project, and any changes you do make are likely to get overridden the next time you update the project. So, inspections are disabled for these files, but the files are still indexed to allow navigation.
Just in case anyone else has this issue it only happens if Nuget delivers a .cs file. If you get Nuget to deliver a .cs.pp file, Resharper correctly analyses the resulting .cs file in your solution.
I was new to Nuget when I started this work and it turns out I needed to make them .cs.pp files anyway in order to use the correct project namespace when importing. So the problem has gone away.

Visual Studio 2013 only builds HTML changes, not C# changes

I am building a personal website using asp.NET's webforms in visual studio 2013 express for web and am following this tutorial:
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview
My page is structured exactly the same as in the website, I have changed some minor stuff to make it my own but the structure in terms of the C# classes and how the interact with the HTML are exactly the same.
I got to section 5 of the tutorial "Display Data Items and Details" and everything was working fine. I've used git a lot in the past so I decided to create a repository for this project so I can access it at work if I feel like.
Suddenly now when I make changes to the C# classes it won't build. It's even stranger because I if I make a change on an HTML file the change is built. In section 3 of the tutorial we learnt how to make the 'product' classes which are displayed on the products page. If I want to change one of the product names for example, when I build the change is not there. Simultaneously I went and changed some info in the HTML for the contact page, IT CHANGES when I build. Why wont the C# changes take effect when I build any more?
I am relatively new to both asp.NET and visual studio. The HTML changes when I build and the C# does not. When I change either I can see in solution explorer that there is a red tick for pending changes. Why would only the HTML pending change be included in the build and not the C#? How do I ensure that the build is actually building the version I see in my editor window?
EDIT
I do not know if I found the original cause but I found a solution/workaround. I realized that the classes mentioned above were grabbed by the html page from the page's database. The .mdf file for the solution was not being rebuilt whenever I cleaned and built so I physically deleted it and rebuilt the solution and voilla my C# changes occurred. I am still fairly new to this whole thing, can someone explain what the .mdf file does and why it wasn't being rebuilt?
Check your .cs files properties on the properties window in visual studio to make sure their Build Action is set to "Compile", Things that are not set to "Compile" do not get compiled. How MSBuild treats project items depends entirely on their build action. CS files default to "Compile" when you make them, but if you changed them yourself that would be why it doesn't update. Also CS files placed in the App_Start folder default to "Content" and they are compiled by ASP.Net when the Application Starts, so if you changed something in App_Start you need to reset the site.
Not exactly sure what is the problem, but I would do a right mouse click on the solution in Solution Explorer->Clean Solution, then do another build and see if that helps.

Cheap way to wrap .exe for Custom Tool?

I have a custom script language and a compiler (an EXE written in C) that turns that language in to C# code. I'd like to hook up the script compiler as a Custom Tool on script files in the solution, and have it generate C# code behind.
I've seen articles and tutorials online, and they all have you generate COM interfaces and register your custom DLL with the registry and GAC, and I really don't want to deal with all that.
Is there a wrapper or hack or 3rd party plugin somewhere that would make this easier? Like if there was a way to run a batch script as the custom tool, and have the code behind file get generated from the stdout of that, I could pipe the file from my compiler to stdout.
First off, I know you said you don't want to register through the MSFT way, but I suggest you reconsider. Details are here: http://msdn.microsoft.com/en-us/library/vstudio/bb166527.aspx
Now that we got that out of the way, my suggestion to you:
Put your exe in a fixed location (best spot is probably right in the root of your solution or repository). Next, run your tool manually once, this way you have the .cs files or whatever you are generating so you can add them to your solution. That way the C# compiler knows they are there and you just have to hit the build button and it'll have the files to build.
Next, create a pre-build event (under project properties, build events tab) that calls your exe with the appropriate command line arguments to make it do its thing (generate your new cs files). I suggest you edit your exe to take multiple files at a time, to make your pre-build command more simple. (This is where placing your file in the sln or repo directory is helpful, because you can use VS macros to get an absolute path to both your exe and the files to read in.)
What happens now, is before msbuild gets called (but after you hit build) your script (or exe) will run to generate new output files. Since msbuild hasn't started yet you can change any solution files to your hearts content and the changes will be picked up by both msbuild and eventually (probably once the build is complete) VS.
Notes:
I have never been able to get a build event to work on the first try, it usually throws an error that will show up in the VS error/warnings window. I usually copy the whole error into notepad (or scite) and edit it down to the actual command line with arguments. I then open up a command shell and try to execute it. The errors here are usually more helpful and you can tweak until you get it right and copy the changes back in to VS.

Shared AssemblyInfo for uniform versioning across the solution

I've read about this technique: Shared assembly info in VS projects - JJameson's blog
Basically it means to create a SharedAssemblyInfo.cs with versioning information about the assembly, and adding this file as Link to all projects of the solution, so the actual file resides only in 1 location on disk.
My question deals with 2 scenarios:
Existing solution that doesn't use this mechanism: Is there a way to easily add the ShareAssemblyInfo to all projects? (lets say i have a solution with 50 projects).
When creating a new project, by default a new AssemblyInfo.cs is created. However i'd like to link automatically to the SharedAssemblyInfo as well.
Is there any solution for this? what is the common practice?
It is possible to link to a shared assembly info file in VS 2010. Ashish Jain has a good blog post about it: Sharing assembly version across projects in a solution.
After creating the shared assembly info file at the solution level, his instructions for linking to it from a project are:
Right click on the project, in which you wish to add the Shared
assembly file, and select Add -> Existing Item...
Select the file “SharedAssemblyInfo.cs” from the solution folder.
Instead of Add, click on the the arrow next to Add and click “Add as
Link”
Drag down the added linked file alongside AssemblyInfo.cs in the
same folder.
Repeat steps 1 – 4 for all projects for which you wish to add shared
assembly file.
I've tried this and it works.
First point could be solved with simple text editor that could handle several files at once and find/replace. Just open all of your csproj in it and replace string <Compile Include="Properties\AssemblyInfo.cs" /> with
<Compile Include="..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
Alternatively you could write a utility like that:
var files = Directory.GetFiles(yourSolutionDir, "*.csproj", SearchOption.AllDirectories);
foreach (var f in files) {
string contents = File.ReadAllText(f);
string result = contents.Replace("<Compile Include=\"Properties\\AssemblyInfo.cs\" />", putSecondStringHere_ItIsJustTooLong); // :)
File.WriteAllText(f, contents);
}
As for the second question... You could take a look at Visual Studio custom project templates , but I'm not sure it worth the efforts. You should IMO write test that will check this instead. It will be much simpler and outcome is actually almost the same.
UPD: About writing tests for checking solution/project files against some custom rules. Basically, sln/csproj format is simple enough to be parseable without much efforts. So if you want to have SharedAssemblyInfo.cs linked into every project - just parse csproj's and check that. Then put that checker in your build server and run it on each build. We have such system working currently and it costs something about two days to write but saved us many more (we have there more sophisticated rules and multi-solution project, so it was worth the efforts).
I won't write about this checking in detail here right now (it is not that short), but I'm going to write blog post about it soon - most probably till the end of this week. So, if you're interested - just check my blog soon :)
UPD: Here it is.
I have created an application to increment the file version automatically.
Download Applicaiton
add the following line to pre-build event command line
C:\temp\IncrementFileVersion.exe $(SolutionDir)\Properties\AssemblyInfo.cs
Build the project
To keep it simple the app only throws messages if there is an error, to confirm it worked fine you will need to check the file version in 'Assembly Information'
Note : You will have to reload the solution in Visual studio for 'Assembly Information' button to populate the fields, however your output file will have the updated version.
For suggestions and requests please email me at telson_alva#yahoo.com
This does not work for solution that has both C# and F# projects. c# project cannot reference shared f# file and vice versa.
The only option in this case is to make a separate project and refer to it from other projects

compile and publish only one .cs file

i have a Visual studio project. it has .aspx, .aspx.cs, config, dlls and so on...
Regularly what i follow for loading a gui on the server is:
build the solution
publish the solution (so i get the published files of the project excluding .cs files) this makes sense as i dont want to give my backend code.
finally i run the gui on the browser.
This works perfectly fine.
But now i have to make a change in .cs file of one of the pages.
do i have to build the solution and publish all the files?? how can i just give the file i made the changes in.
I ask this because it does not look good to a client...
any suggestions?? thanks
If you make a code change you should republish the site. Bear in mind that you can just publish the newly compiled assembly itself and not all of the aspx files.
However, I don't recommend doing this as the time to deploy an entire site isn't particularly large and you might have made a change to an aspx and forgotten between deployments. It's much better to send the whole thing.
Also, this isn't a normal thing for a client to have a problem with. Tell the client this helps ensure robustness of the deployment. After all, if you made a code change in a language like PHP and deployed that single file then you don't get compile time checking and stupid problems like misnamed variables might not show up for awhile or might crater the whole site.
IMHO, it's much better to get the full compile time checks out of the way BEFORE the site is pushed. Of course, this does little to nothing for runtime problems.. but that's a problem you have no matter the language.
C# doesn't have .obj and linker like c++, so if you change one file and want to integrate it in your project, you should compile it in related project (just changed files going to be compiled), also for file compile option see this: http://msdn.microsoft.com/en-us/library/78f4aasd%28VS.80%29.aspx

Categories