简体   繁体   中英

Stand-alone C# file

I want to go ask a server for some information and dump it into a CSV file. This should be a very simple operation, nothing fancy.

In PHP I would simply write a .php file, fire up whatever server I'm using, and open that file in a browser to have it run.

Is there any parallel setup using C#?

I tried creating a .cs file and running it in VS but it asks about attaching to something.

I'm fairly new to C# and really don't want to spin up a whole application to run one simple function.

You can do this either by using C# Interactive , or you can use LINQPAD which will run it for you. Outside of this there is no way to run a C# script like you would in PHP.

If you want to execute the code via a web server, you can use the following method. If you want to execute the code from the command line directly on the computer, use the variants proposed by Kevin

You need to have IIS (Internet Information Services, the Microsoft Web Server) on your machine. It comes with all server versions of Windows and can also be installed free of charge on standard Windows.

Then you need to make sure, ASP.net is installed on the server. You can do that via "Add/Remove Windows Features" in the Windows operating system. Then look for "Internet Information Services -> WWW -> Asp.net" (Differs a bit from Windows version to Windows version).

Then in IIS Manager, create a new virtual folder "sample". In there, create a file "sample.aspx" with the following content:

<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Response.Write("Hello World!");
    }
</script>

Then just call the code via http://localhost/sample/sample.aspx

You can edit the text file and immediately use the browser to execute it without any need to manually compile the file.

If you have VS2015 or newer, use the C# Interactive compiler:

C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\csi.exe scriptfile.csx

Also can be used interactively when a script is not specified.

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