Compiling an executable file using Notepad++ and Csscript - c#

I am very new to C# and am wanting to write my code using a text editor like Notepad++ and compile using csscript. I have the two working and I am getting results from my code.
However, so far, I have only been able to run my code as interpreted, but I will eventually want to compile exe or dll files.
Am I able to compile my code into a standalone exe or dll using notepad++ and csscript, please?

Just an update for your original question...
CS-Script plugin for Notepad++ actually allows building normal executables that can be executed as any other managed exe.

Little too late, but here's the one that worked for me: I called this batch script bnr.bat (Build and Run)
echo Building project..
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /out:"%1\%2.exe" %3
echo Running project
%1\%2.exe
pause
Save this bnr.bat file and and then use NPP's Run and set The Program to Run as follows and before you press Run button, press Save and provide your custom shortcut keys:
<directory_where_you_saved_bnr.bat>\bnr.bat $(CURRENT_DIRECTORY) $(NAME_PART) $(FULL_CURRENT_PATH)
the $ constants are defined internally in NPP:
$(CURRENT_DIRECTORY) is the full path of the directory containing your C# file.
$(NAME_PART) is the name of your C# file minus extension (.cs).
$(FULL_CURRENT_PATH) is the full path for your C# file.
This does not have any error checking, but pause in batch script will allow you to see the errors and exceptions within the console before you exit the script.
I had set the PATH environment variabile, but somehow this batch script did not find csc.exe, because it was looking at the npp bin directory.

For .NET 5+ (and .NET Core), you can compile your project using the .NET Command Line Interface (CLI)
The command
dotnet publish
creates the files you need to run your program.

No, you will need a compiler (Microsoft´s from VS or Mono)

csc.exe is what you need. It should be at C:\Windows\Microsoft.NET\Framework\v4.0.30319.
Here is a link to a reference. http://msdn.microsoft.com/en-us/library/2fdbz5xd.aspx
And check out Visual Studio express, it makes life easier.

Thanks for the reply.
I have Visual studio but the license expires in 7-days.
I am using Notepad++ with an add-in called cs-script. The add-in checks and runs code in a similar manner to Visual studio but it will not compile an exe or dll file.
However, to answer my own question and as suggested by quarksoup, the answer lies within the csc comiler. By using the /flags, I am able to compile my programs from the command-line. I shall write a batch file that will do the work for me.
Regards

Related

How to embed the version information in the executable file name when building C# application in Visual Studio?

This question is a complement for the post How to change the output name of an executable built by Visual Studio.
After reading this post I did the following:
Firstly, I followed the answer for this post and I could define the executable file name successfully.
Now, I would like to know if instead of only define the name as "Demo.exe" as mentioned in the example post above, it would be possible to embed the version defined in AssemblyInformationalVersionAttribute or in AssemblyVersionAttribute in the built file, resulting in something like "Demo_v1.0.0.0.exe"?
I'm developing my application in C# WinForms, using Visual Studio Express 2017.
Why would you want to change the name of the executable? Whenever you try building a Setup for your application, you need to change the Setup to include the new file. And when you install an update, your Setup needs to know all versions of your executable in order to delete the old version. That's just not what you want to do.
If you want to keep all versions of the software for yourself, come up with a different solution, e.g. moving the executable into a folder which has the version number.
That said, I have done this for Setups, so customers can download different versions of the Setup. I did that using a commercial tool called Visual Build, but there are other build automation tools available. So, my answer is: set up a continuous integration / continuous delivery pipeline (CI/CD) and automate the step there, not in Visual Studio.
From the project properties, you can add Post build event command line to rename your exe
pseudo
Maybe you can create another console renamer.exe which reads version defined in AssemblyInformationalVersionAttribute or in AssemblyVersionAttribute of your app and renames it and then call that renamer.exe from Post build event command line
write a powershell script to rename the newly built exe and call that script from Post build event command line

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.

Slow compile times when Visual Studio 2012 is open

Afternoon all,
I have a very strange problem. When VS 2012 is open, compile times are very slow. This slow compile time is present when building via VS and/or directly via csc.exe from the command line.
To test: Create a folder with the following items:
A batch file (compile.bat) containing:
echo %time%
csc /target:library class1.cs
echo %time%
and a class1.cs containing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
}
}
Now open a visual studio command prompt. Navigate to the above folder and run the batch command, without VS open. On my machine, this takes approximately 10ms, perfect.
I now open VS 2012, open no solution, do nothing other than open the the application so that devenv is running.
Now repeat the test by running the batch file, compile time is now 10000ms (10 seconds).
I have checked the event viewer for anything that is happening when VS is open but not when it's closed, used procmon and filemon to look for file access and checked to see if VS is enabling any services when it is open, all without success/impact.
I have even tried uninstalling and reinstalling VS, this solved the problem for the first few builds but it then reoccurred. Restarting the machine has no effect. I have no plugins installed in VS.
My colleagues machines do not display this problem and they have the same setup. This is all running on a machine with 16gb ram, 64-bit win 7 and SSD.
Anyone got any clues?
I've figured this out. I believe it was the result of some malware. I diagnosed the problem with the following steps.
Download ProcMon (http://technet.microsoft.com/en-gb/sysinternals/bb896645.aspx)
Add a filter to ProcMon on process name: csc.exe
I then ran a compile from the command line, with visual studio open. It took around 10 seconds, way too long! Looking at the output in the ProcMon window, I noticed what appeared to be csc.exe pausing for 5 secs, once towards the beginning of the trace, and once towards the end. See the following screens:
Start:
End:
It appeared that a RegCloseKey to HKLM\SOFTWARE\Wow6432Node\5c28f8fbc6fe942 was causing csc.exe to wait for 5 seconds, twice.
RegKey:
I then decided to rename this entry (added _old to the end), I then recompiled....BINGO, it compiled in less than 30ms!
After studying the entries contained in this key and some googleing it turned out that this reg key was the result of some malware. I used the following guide to remove thte malware and now the problem is completely solved.
http://www.explosiveknowledge.net/main/2012/08/19/browsemngr/
Please be aware that the guide above doesn't contain the correct reg entries, I think the virus must have been tweaked at somepoint, I couldn't find the reg entries mentioned in the guide but simply deleted the ones I'd found.
Please note that the 5c28f8fbc6fe942 part of the reg key seems to be randomly generated. If you have this problem is might be different but the values contain within will still talk about "Browser Manager".
Hope this helps someone!
To understand your query we need to look into the concept of response file.
A response file is a text file that contains a set of compiler commandline
switches. When you execute CSC.exe, the compiler opens response files and uses any
switches that are specified in them as though the switches were passed to CSC.exe on the
command line. You instruct the compiler to use a response file by specifying its name on the command line prepended by an # sign. For example, you could have a response file called
MyProject.rsp that contains the following text:
/out:MyProject.exe
/target:winexe
To cause CSC.exe to use these settings, you’d invoke it as follows:
csc.exe #MyProject.rsp CodeFile1.cs CodeFile2.cs
This tells the C# compiler what to name the output file and what kind of target to create. As you can see, response files are very convenient because you don’t have to manually express the desired command-line arguments each time you want to compile your project.
When you install the .NET Framework, it installs a default global CSC.rsp file in the
%SystemRoot%\Microsoft.NET\Framework\vX.X.Xdirectory (where X.X.X is the version of
the .NET Framework you have installed).
The Visual studio 2012 uses this default response file for compiling the code.
Because the global CSC.rsp file references all of the assemblies.Referencing all of these assemblies could slow the compiler down a bit.

Trying to launch a Visual Studio C# program from inside another one

I am having great difficulty getting a Windows program, written in VS 2008 C#, to launch another type program. I've put a main program to offer you the ability to launch some other VS 2008 C# programs. When one of them is selected the following code is intended to do the launch:
System.Diagnostics.Process.Start(#"C:\Documents and Settings\rat\My Documents\Visual Studio 2008\Projects\PV_002_082708\PV_001\PV_001\bin\Debug\PV.exe");
It works, but each user will have a different path. The path shown is for my computer. The code would have to know where each persons program was installed! In the past, I could easily call a program that was in the C:\Program Files location because that's where the MS Package & Deployment program put the programs. VS 2008/2010 doesn't put them there!
I'm sure I am not knowledgeable about Visual Studio 2008/2010 to know if there is a way around this, so any help would be greatly appreciated.
System.Diagnostics.Process.Start(System.Io.Path.Combine(System.Windows.Forms.Application.StartupPath, "PV.exe"));
assuming that the program is in the launched application's path.
or...
System.Diagnostics.Process.Start(System.Io.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), #"Visual Studio 2008\Projects\PV_002_082708\PV_001\PV_001\bin\Debug\PV.exe"));
assuming you want to open the project from it's place.
Once you create the installation package you can set the install path to some fixed path that users will not be able to change and than use that from your code (not very user friendly but it would work) or insert the user chosen path value into the registry and get it from there instead.
May want to have a look at this thread. How-to as well as lots of pro's and cons.
Embedded a *.exe into a dll
If you see the first answer to his question, I think this would work for you.
Simply he is saying, you add a resource file to your project, then when the user runes your program, it extracts your exe file that you added as a resource file to somewhere you know on the end users maching.

Compiling C# with csc.exe from within Notepad++ using NPPExec script

I'm having trouble writing a NPPExec script (for Notepad++) to compile C# source using Microsoft's csc.exe. I had no problem getting MinGW up and running for my c++ stuff, but the same script structure doesn't seem to work for C#. Does anybody have a working C# NPPExec script?
Three quick notes:
While my mingw installed to a path with no spaces (\MinGW\bin), it appears that my C# compiler is buried in \Program Files\, a path with multiple spaces. Does this matter?
I was able to compile using the Microsoft SDK-installed command prompt (which I think sets some environment variables), so I know the compiler works.
In the NPPExec option box, I have selected FOLLOW $(CURRENT_DIRECTORY). Does this matter?
Thanks.
Here is my nppexec script for C# (.NET 3.5) to compile the currently opened file and run:
"c:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe" /out:"$(FULL_CURRENT_PATH).exe" "$(FULL_CURRENT_PATH)"
"$(FULL_CURRENT_PATH).exe"
In the "Plugins -> NppExec" menu:
Select "Save all files on execute"
Select "Follow $(CURRENT_DIRECTORY)".
Answers for your questions:
1) Use double quotes (") for full path of csc like my configuration.
2) At least for .NET 3.5, only full path to csc is enough, no need to load VS environment variables.
3) It is better to select "FOLLOW $(CURRENT_DIRECTORY)" so that the compiled EXE is under the same directory.

Categories