So, with no further ado, I present:
Visual Stu-DIY-o
For the impatient, here is the quick and dirty TL;DR:assoc .cs=CSharpSourceFile ftype CSharpSourceFile=%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe %1 reg add hkcr\CSharpSourceFile\Shell\edit\command /ve /d "%WINDIR%\System32\notepad.exe %1"
But your .NET compiler might not be located there, or your code warnings and errors may zip by and disappear, or your executable may not get built, or you may have wanted to program VB.NET, et cetera. If you care about such things, then go through the exercise of locating your compiler, creating a friendly compiler script, and associating that with your source files. Read on for more.
Find your .NET compiler of choice
You're really just confirming what version you have. You're doing this because you want to be sure the files exist before you copy and paste the script below. Fortunately, this is easy, because it's in your path:- Hit Win+R (to get the run dialog)
- Type Microsoft.NET
- Leap of faith: press Enter
- Browse to the Framework\v4.0.30319 directory
- Note the absolute path to csc.exe (hint: hit ALT+D, hit End, finish off that directory name with a backslash, and start typing csc, and it will let you auto-complete the path in the address bar; copy this address for later).
A little note: I use the Framework (and not Framework64) directory because I want my MSIL to run in 32- and 64-bit environments. For my normal projects, I use .NET version 4.0, and for software that must be deployed to mixed and potentially un-maintained platforms (if I told you why, I'd have to kill you), I target .NET 2.0. Lastly, I use C#, but you don't have to. At this point, if you want to locate vbc.exe instead (That's the VB.NET Compiler), go ahead and replace csc with vbc. I won't judge you. Just remember to actually replace what I put in the script below with the path to vbc.
At this point, we could set the file association directly with the compiler, as in the "TL;DR" section above, but if you double-click your .cs (or .vb) files, and your source code has warnings or errors, a mysterious black box will appear and disappear before you have a chance to review and fix them. That's why you're going to bother with the next step:
Write a quickie script
Choose a place where this script will live. Make a "util" or "bin" or "scripts" directory somewhere in your user profile if you don't already have one. Open Notepad, copy and paste what is below (replace the path to csc if you are targeting a different .NET version, architecture, compiler, etc.), and save it as something. I chose buildcs4.cmd. In the Save As dialog, under "Save as type", don't forget to select "All Files (*.*)" so that it does not "helpfully" append the .TXT file extension for you, thus neutering your command script. Here's my version:@ECHO OFF %WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe %* PAUSE
The PAUSE command is there so you can see the warnings before the script closes. You can't use an IF ERRORLEVEL command to pause for warnings, because the compiler only returns 1 or greater if there are errors, not if there are warnings, so that is why I include an unconditional PAUSE. %* tells the command interpreter to pass along all the arguments from the command line. This is how the filename you double-click will be passed to the C# compiler (or VB.NET compiler, if that's your predilection).
Finally, make the file association
Open an administrative command prompt and invoke these three incantations:assoc .cs=CSharpSourceFile ftype CSharpSourceFile=%USERPROFILE%\path\to\buildcs4.cmd %1 reg add hkcr\CSharpSourceFile\Shell\edit\command /ve /d "%WINDIR%\System32\notepad.exe %1"
These define a filetype, set a file association, and add an "Edit" context menu command, respectively. For more background on all that, type ASSOC /?, FTYPE /?, and REG /? on the command-line.
Taking it for a Spin
If you want a test, here's a nice C# sample you can paste into notepad. I think the traditional hello-world example is pretty boring, so here's something to spice up someone's life:using System; using System.Windows.Forms; class mbox { static public void Main() { MessageBox.Show( "Delete C:\\ - are you sure?", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Warning ); } }
No comments:
Post a Comment