简体   繁体   中英

Setup environment using batch file for DotNet Framework Version Command line for easy way around

I have .net framework 4.0 (and older versions, ie multiple versions) installed, want to use the command line compiler for C#

C:\>csc.exe

Now I found across few forums using the direct path I can use the compiler, also using batch file one can do that,

I need to set environment variables LINK but that is part of visual studio(if VS is installed then only it works) I want to use the compiler from the redistributable .net framework(using command line only) which is free to use and distribute.

Do not want to install Visual Studio in the system where I run the code.

my Current CSC.exe file location PATH is

C:\Windows\Microsoft.NET\Framework\v4.0.30319

This path and compiler file works fine but every time I have written like this and some times I have seen an error related to Library.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe sample.cs

How to avoid writing long path each time while testing the sample codes?

Please provide best available alternate(Batch file or something)

Edit 1: Setting Path:

Paul's answer is works fine with simple program, But the issue with that is if there are any Library files used/added, were not found in this case.

Note: Version is not a constraint actually, we may use any .net redistributable version. Just CSC.exe should accessible from any path without any issue(example library files used).

if you are using a batch file add to the start

set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319\;%PATH%

This will add the .net framework to the start of your path for the current run of the batch file only.

now your batch file can just use

csc.exe sample.cs

Got the working answer from one of the forums, for the complete solution to build with reference added.

We need to use /r option with compilation.

First add path using command below. Example:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /t:exe /out:sample.exe sample.cs /r:ReferenceName.dll

After adding Path:

C:\test>csc /t:exe /out:sample.exe sample.cs /r:ReferenceName.dll

This solved my problem with reference.

Note: if reference is not in same directory as CS file it need to be added with its path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM