Skip to footer content
Iron Academy Logo
C# Scripting and .NET CLI

How to Run C# Code With Just a Double Click (Tim Corey Explains)

Tim Corey
4m 53s

Running C# code usually means opening Visual Studio, creating a new project, working inside a solution, and running a console application through the IDE or the command line. In a recent video, however, Tim Corey shows how C# can be used in a much lighter way—closer to a scripting language—by running a single .cs file directly.

In this video, Tim takes the idea one step further. He explains how to run C# code with just a double click on Windows, without opening an editor or typing commands manually. This article walks through Tim Corey’s explanation in detail, following the video step by step so you can clearly understand how this works and why it works.

From Command Line Execution to Double-Click Execution

At 0:00, Tim Corey starts by referring to an earlier demonstration where he showed how to run C# files directly using the dotnet CLI. In that approach, a .cs file could be executed from the terminal window using dotnet run, making C# feel closer to a script file rather than a full project.

However, Tim points out at 0:11 that one limitation remained: you still couldn’t just double-click a C# file and have it execute. You had to use the command line, which meant opening a console, navigating to a folder, and typing a command.

At 0:20, Tim explains that the goal of this video is to solve that problem by showing how to double-click a .cs file and execute it directly on Windows.

A Simple Hello World Example in C#

At 0:42, Tim introduces two files: a C# file named hello.cs and a batch file that will make everything work.

Tim first focuses on the C# file itself. At 0:53, he runs it using dotnet run hello.cs, and the output is a simple Hello World message. As Tim explains at 1:01, this is all the program does.

Although Tim doesn’t open Visual Studio or create a full project file, the C# code still follows the same core structure developers recognize—using System, defining a public class Program, and including a static void Main method. This shows that even without a traditional console app project, the C# compiler can still compile and execute valid C# code.

Tim explains at 1:05 that this approach treats a single .cs file almost like a script, avoiding the need for a full .NET Framework or SDK-style project setup.

Why This Isn’t a Full Project Setup

At 1:08, Tim briefly reminds viewers that this is not the same as creating a full console application with a solution, project references, or NuGet packages. Those concepts still matter when building larger apps.

However, for quick utilities, learning examples, or lightweight automation, Tim shows that you can start coding immediately with a single .cs file and the dotnet CLI, without worrying about folders, extensions, or project configuration.

Introducing the Batch File Solution

At 1:19, Tim explains that double-click execution requires help from a Windows tool—a batch file.

At 1:24, he states clearly that the batch file is what connects the double-click action to the dotnet run command. Without it, Windows doesn’t know how to execute a .cs file as a program.

Tim mentions that the batch file is available in the video description, but he walks through it line by line so developers can understand exactly what’s happening.

How the Batch File Works

At 1:34, Tim shows the entire batch file and begins explaining each line.

He first disables command echoing to keep the output clean. At 1:39, he explains that the batch file changes the current directory to the folder where the clicked .cs file exists. This ensures the correct files are accessed during execution.

At 1:46, Tim highlights the most important line: the dotnet run command. The file that was double-clicked is passed in as an argument, allowing the batch file to execute the correct .cs file automatically.

This approach avoids compiling a separate exe file manually and instead relies on the .NET compiler to compile and run the code on demand.

Keeping the Terminal Window Open

At 1:52, Tim explains why the batch file includes a pause command.

Because the example program does not wait for user input—such as Console.ReadLine()—the terminal window would close immediately after execution. This would result in a quick flash where the user never sees the output.

By pausing the terminal, Tim ensures the output remains visible until the user presses a key. As he explains at 2:15, if you don’t want that behavior, you can remove the pause line so the program runs silently in the background.

Associating .cs Files With the Batch File

At 2:24, Tim explains that the batch file itself isn’t designed to be double-clicked. It expects a file name as input.

The solution, as Tim shows at 2:32, is to associate .cs files with the batch file using Open With → Choose Another App. If the batch file doesn’t appear in the list, Tim explains at 2:42 that you can manually browse your system to find it.

Once selected, Tim chooses Always, making this batch file the default handler for .cs files on Windows.

Running C# Code With a Double Click

At 3:06, Tim demonstrates the result. When he double-clicks hello.cs, a console window opens, the C# code is compiled and executed, and the Hello World output appears.

This creates an experience similar to running a script in languages like Python, where a single file can be executed directly by the user.

Switching Back to Visual Studio or an Editor

At 3:16, Tim addresses what happens if you want to open the file in Visual Studio, VS Code, or another editor instead.

He explains that only one default action can be assigned to double-click. However, users can right-click and choose Open With to open the file in an editor. At 3:37, Tim shows how to reset the default behavior back to an editor by selecting it and choosing Always.

Windows-Only, Simple, and Practical

At 3:54, Tim clarifies that this method works on Windows, though similar ideas may exist for other platforms.

He concludes at 4:00 that as long as a .cs file is set up correctly, developers can now double-click it to execute C# code, view output, press a key, and close the window—or let it run silently without pausing.

Final Thoughts

Through this video, Tim Corey demonstrates a practical way to run C# code without a full project, IDE, or manual command-line steps. By combining a simple C# file with a batch file, C# becomes far more accessible for quick scripts, experiments, and utilities—while still using familiar concepts like public class Program, static void Main, and the dotnet CLI.

As Tim emphasizes, this isn’t about replacing best practices—it’s about having the right tool for the job.

Hero Worlddot related to How to Run C# Code With Just a Double Click (Tim Corey Explains)
Hero Affiliate related to How to Run C# Code With Just a Double Click (Tim Corey Explains)

Earn More by Sharing What You Love

Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me