How to compile and run a C # program on a machine that does not have a vs (Visual Studio)

Source: Internet
Author: User

Will soon be New Year, every year to go back home, the use of computers and network is not so convenient here. If I wanted to write a program, I didn't have a machine with Visual Studio, and I couldn't install a Visual Studio for every machine.

Online search, not to say that the need for Visual Studio is required. NET Framework sdk,visual Studio Several G, this SDK is not small, 1.3G, my USB flash drive can not be installed (USB drive size only 1G L)

SDK actually has a lot of things I do not need, such as documents, in fact, I need only a compiler, so I try to csc.exe to their own USB flash drive, and then copy to the dad's machine, with the console of a implementation, simply can not use!! A check that turns out his machine didn't have the. NET Framework installed at all

So I went to the Microsoft home page to find the download link to the. NET Framework 3.5 SP1 (not the SDK) (http://download.microsoft.com/download/2/0/e/ 20e90413-712f-438c-988e-fdaa79a8ac3d/dotnetfx35.exe), this is also relatively good, only 231M, download installation completed, found that the original did not have to go to the cuff csc.exe, in c:/windows/ There are already csc.exe under the microsoft.net/framework/v3.5. Ha ha! So everything is ready: There are editors (Windows comes with Notepad), there are compilers (csc.exe), and there is a running environment (. NET Framework)

So how do you use csc.exe to compile and write a good program?

We can write a program App.cs

Using System;

Using System.Windows.Forms;

Namespace MyApplication

{

Class APP

{

public static void Main ()

{

Application.Run (New Form ());

}

}

}

Save it under the C drive. Then bring up the console (Start menu, run->cmd, enter), in the case of the current position is C drive, enter the CD c:/windows/microsoft.net/framework/v3.5, enter the folder

Input Csc/t:winexe/r:system.dll/r:system.windows.forms.dll/out:c:/app.exe C:/app.cs Carriage return

will be able to see in the C-disk app.exe files, click to run, you can see the form.

The meaning of/t here is the compiled file type, there can be 4 kinds: exe,winexe,library and module. EXE means that the console runs the program, winexe indicates that Windows runs the program, and Librar is a module file for Dll,module.

/R is a DLL referenced by the program, here I need to refer to two Dll:System.dll and System.Windows.Forms.dll, because these two DLLs are. NET provided, so there is no need to specify a path, if the DLL is not registered with the system, you need to specify the path.

/out is the file that specifies the output, and if not specified, the compiled generated file is saved by default to the folder where Csc.exe is located

Let's write two more files and compile them into a DLL for App.exe to call.

Test1.cs

Using System;

Namespace Testdll

{

public class Test1

{

Public String Text

{

get {return m_text;}

set {M_text = value;}

}

Private String M_text;

}

}

Test2.cs

Using System;

Namespace Testdll

{

public class Test2

{

Public Int32 Value

{

get {return m_value;}

set {m_value = Value;}

}

Private Int32 m_value;

}

}

Compile into Test.dll using CSC

Csc/t:library/r:system.dll/out:c:/test.dll C:/test1.cs C:/test2.cs

Then modify the App.cs to

Using System;

Using System.Windows.Forms;

Using Testdll;

Namespace MyApplication

{

Class APP

{

public static void Main ()

{

Test1 test1 = new Test1 ();

Test1. Text = "This is my Test form";

Test2 test2 = new Test2 ();

Test2. Value = 100;

Form f = new form ();

F.text = Test1. Text;

F.height = Test2. Value;

Application.Run (f);

}

}

}

Use CSC to compile to App.exe

Csc/t:winexe/r:system.dll/r:system.windows.forms.dll/r:c:/test.dll/out:c:/app.exe C:/App.cs

Run the generated App.exe file and get the following results

How to compile and run a C # program on a machine that does not have a vs (Visual Studio)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.