Jump to content

How to compile C# files using csc.exe from .NET Framework


Recommended Posts

In order to compile your C# files without using software like SharpDevelop or MonoDevelop on Windows, you can use csc.exe, which stands for C# compiler. It is provided with every installation of .NET Framework.

All you have to do is right click on 'My Computer' -> 'Properties' -> 'Advanced' -> 'Environment Variables' -> find 'Path' in system variables and add ';%systemroot%\Microsoft.NET\Frameworkv3.5' there, without ''. v3.5 is the .NET version, you can change it to any you have.

Now, in order to compile your .cs file(which you can create in notepad for example, remember to save it with .cs extension), click on 'Start'->'Run'->'cmd'. Remember to copy UnityEngine.dll and Assembly-CSharp.dll from KSP's folder(found in \KSP\KSP_Data\Managed) to the folder with your .cs file.

Next, navigate to your .cs file by using 'cd' command(for ex. cd c:\CSharp) and type:

'csc /r:UnityEngine.dll /r:Assembly-CSharp.dll /t:library /out:myfile.dll filename.cs'

'myfile.dll' is the name of your compiled .dll and 'filename.cs' is your C# code.

You'll find your .dll in the same folder as 'filename.cs'.

Enjoy!

Edited by ForumHelper
Link to comment
Share on other sites

  • 7 months later...

I was wondering why csc.exe didnt work on my system, too late though as I just bundled everything into a visual studio solution instead which is the IDE I like to use anyway so did the job fine

Link to comment
Share on other sites

Personally, if I were to be building my stuff on the command line, I would just write a batch file like this:


SETLOCAL

REM -- Naturally, change these paths to match your setup.
SET KSPData=E:\Program Files\KSP\KSP_Data\Managed
SET CompilerDir=C:\Windows\Microsoft.Net\Framework\v4.0.30319

REM -- If the source file has a space in its filename, surround the filename with double quotes.
SET SourceFiles={Source File 1} {Source File 2} {etc.}

REM -- Note the references to System.dll, System.Core.dll and mscorlib.dll that exist in your KSP folder. The /nostdlib
REM -- and /noconfig flags stop the compiler from adding references to these automatically. If you didn't do that, the
REM -- compiler would link in the .Net 4.0 version of DLLs and KSP doesn't like those.
SET References="/r:%KSPData%\Assembly-CSharp.dll" "/r:%KSPData%\UnityEngine.dll" "/r:%KSPData%\mscorlib.dll" "/r:%KSPData%\System.dll" "/r:%KSPData%\System.Core.dll"

REM -- Change MyPlugin.dll to whatever you want your DLL to be named.
"%CompilerDir%\csc" /t:library /out:MyPlugin.dll /o /nostdlib /noconfig %References% %SourceFiles%

Link to comment
Share on other sites

  • 7 months later...

So I'm trying to edit a mod that has a source folder with a bunch of .cs files. I only need to edit little bit of one of those files though. How do I go about compiling a new .dll from this source folder with multiple .cs files?

Link to comment
Share on other sites

Just add the other .cs files to the command line:

csc /r:UnityEngine.dll /r:Assembly-CSharp.dll /t:library /out:myfile.dll filename.cs otherfile.cs someotherfile.cs

But, yes, use visual studio express. It is a lot easier.

*.cs usually works as well

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...