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

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.

Related

How do I choose a local Python interpreter for pythonnet in Visual Studio?

Local Python interpreter is a new Virtualenv environment in PyCharm, for example.
I have had the same issue and can confirm that this method works.
Ensure that you have the Python Extension installed
Go to Settings in VS Code
Type in the search bar, "python: pythonPath"
You may have to scroll a bit before you see the result, but it should have a blue line next to it (on the left)
Copy and paste the path to your python interpreter, including the file itself, in the field provided (e.g. C:/Windows/python/python.exe)
Hope this helps :)

How to create more Macros in C# Post build step

I have an Visual Studio 2012 Solution that includes both C# and C++ projects.
I want to create a postbuild step and I notice that the C++ projects have much more macros than the C# projects. I need to get the WindowsSDKDir which is available in the C++ macros but not in the C# macros.
C++ Macros
C# Macros
Can I see all the Macros that are available to the C++ projects in the C# projects??
I can think about the following workaround. Write this post-build command:
$(ProjectDir)post_build.bat
Create post_build.bat file in the project directory and fill it by this way:
echo %WindowsSdkDir%
echo %WinDir%
Build the project, it prints:
ECHO is on.
C:\Windows
So, C# is not smart enough to see Visual Studio build environment variables (%WindowsSdkDir% is expanded to nothing), but at least general environment variable like WinDir is expanded. So, you can create your own environment variable with the same value as WindowsSDKDir and use it in batch file called from Post-Build step.
To make something more useful then echo, create batch file with parameters (%1%, %2% etc.) and call it from post-build step, passing required parameters. For example:
$(ProjectDir)post_build.bat $(TargetPath)
In the batch file %1% will be expanded as output file name.
Simply call the batch file that sets up environment variables for a Visual Studio command prompt as the first line of your build event. This will set up all the environment variables for use in later lines of your build event. For example...
call "$(DevEnvDir)..\..\VC\vcvarsall.bat"
"%WindowsSdkDir%bin\x86\rc.exe" <parameters>
Note that the environment variables are Windows environment variables and need to be accessed via the %VAR_NAME% syntax rather than the $(VAR_NAME) syntax.
The location of the vcvarsall.bat file is consistent over at least VS2012, 2013 and 2015, meaning that this solution is generic and doesn't need tweaking for different VS versions.
If you use msbuild you will find that $(DevEnvDir) is not set correctly when building in msbuild. In that case, this alternative should work for both msbuild and visual studio build providing you have not customized the visual studio install location.
call "%ProgramFiles(x86)%\Microsoft Visual Studio $(VisualStudioVersion)\VC\vcvarsall.bat"

Compiling an executable file using Notepad++ and Csscript

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

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.

How do I get started using Cygwin for C# development?

I'm coming from Linux, and I want to try to learn some C# development.
But I'm partial to the command line and using Vim. Is it easy to set up Cygwin to run the csc compiler from the command line and to edit C# files directly in the console with Vim?
I downloaded SharpDevelop, but I would really like to give console-style C# development a try.
Specifically, how do I set up the PATH (is there an equivalent to .bash_profile?) so that I can run csc and vim from the Windows Console?
I'm running Windows 7.
You should give VsVim a try. With C#, you'll thank yourself for sticking with Visual Studio.
Specifically, how do I set up the PATH (is there an equivalent to .bash_profile?) so that I can run csc and vim from the Windows Console?
Exactly like in linux. The only difference, IIRC, is that you use .bashrc instead of .bash_profile. Edit (or create) the file in your cygwin user's home directory (~/) and write the usual export PATH=$PATH:/path/to/other/bin lines.
http://www.cygwin.com/cygwin-ug-net/setup-env.html
I love VsVim, as suggested by Joel. But you said you want to try without Visual Studio, so here:
vim integrates well with gnu make, and writing makefiles for C# programs is quite simple.
Just be careful about tabs vs spaces, same as on Linux (and like I'm not doing on SO, since getting tabs in HTML code blocks is hard). You'll just need a rule along the lines of:
myapp.exe : mymain.cs myhelpers.cs mydialog.cs
csc.exe /o /out:$# $^
Then from inside vim, you can just use the :make command.
ProcessStartInfo pInfo = new ProcessStartInfo("C:\\Cygwin\\bin\\mintty.exe");
pInfo.Arguments = "-i /Cygwin-Terminal.ico -";
if cygwin is installed on C:\

Categories