Sublime Forum

How to run(execute)c# code

#1

hi i tried to execute a basic c# code to see if it works or not and it didn t i triend to use terminus but i think i have to do something like creating a new build system(i tried but failed)
and i tried to use a plugin called c# compile and run and i get an error but it was another error idk if it is fixed or not but the error now is: ‘csc.exe’ is not recognized as an internal or external command,
and when i tried to create a new build system i got win error 2 and 5
so i don t know what to do i just wanna execute c# code thats it
btw im a beginner

0 Likes

#2

ok there is something weird
i pasted this in a new build system and named is c#.sublime-build:

{
   "cmd": ["csc", "$file_name", "&&", "start","cmd", "/k", "$file_base_name.exe"],
   "file_regex": "^(...?):([0-9]):?([0-9]*)",
   "working_dir": "$file_path",
   "selector": "source.cs",
   "shell": true,
   "quiet": true
}

and then it worked BUT the c# compiler plugin worked and its faster than the c#.sublime-build
why did it worked now even when i deleted it?
and i wanna compile it on both cmd and in sublime(at the bottom srry idk what it is called panel i think)

and i got this code from:https://stackoverflow.com/questions/33906312/how-do-i-make-a-build-system-for-sublime-3-to-compile-the-current-cs-file-c

0 Likes

#3

The build system is starting the external program you specify there with arguments and capturing what it does. Presumably the plugin does a similar thing, but perhaps executes a different tool or uses different arguments.

The build you have outlined there is (via the start cmd /k part) opening an external command prompt and running your program inside of it.

If that part wasn’t there, the program would execute in the build panel at the bottom of the window instead. In doing so your program would however not be able to accept input because the output panel, as its name suggests, is output only.

If you have Terminus installed, you can add:

"target": "terminus_exec",
"cancel":  "terminus_cancel_build",

To your build to get Terminus to execute it instead.

0 Likes

#4

where do i paste this “target”: “terminus_exec”,
“cancel”: “terminus_cancel_build”,

im srry if i don t know im complete beginner

and i have another issue when i created a new .cs file and then i compile it using c# compile plugin i get an error:‘C:\Users\Admin\Documents/yea.exe’ is not recognized as an internal or external command,
operable program or batch file.

But when i compile it with the c#.sublime-build that i made it works and when i do it again with the c# compiler plugin it works thats weird for me

0 Likes

#5

I’m unfamiliar with the plugin that you mention, but assuming your sublime-build file above works, the following is a modified version with some description of what’s happening that outlines things.

In a nutshell:

  1. It’s recommended to use shell_cmd and not cmd for this, which makes things easier
  2. Your build is opening a new command prompt window and running the application there. There is no benefit to using Terminus in such a case because Terminus is for making the build interactive, and it will already be interactive if it’s running in a command prompt.

If you use this build directly, it will run in Sublime in Terminus (but you may need to adjust or remove the env at the end)

{
    // The original cmd version; this requires that csc.exe be on the path in
    // order to launch. It compiles the program to an executable with the same
    // name as the source file, and then launches a command prompt to run it.
    //
    // "cmd": ["csc", "$file_name", "&&", "start","cmd", "/k", "$file_base_name.exe"],
    // "shell": true,

    // This modified version just runs the build and lets Sublime capture the
    // output, so that you can see it at the bottom of the screen. The added
    // /nologo here tells the compiler not to tell you about itself, so that you
    // see only the output of the program without the compiler copyright notice.
    //
    // This will not/ work if your program is interactive (asks the user for
    // data) because Sublime doesn't natively support input gathering from
    // output panels.
    //
    // "cmd": ["csc", "/nologo", $file_name", "&&", ""$file_base_name.exe"],
    // "shell": true,

    // This is the same as the first cmd example above, but expressed more
    // clearly; // this does not need the "shell": true line because it's
    // inferred.
    // "shell_cmd": "csc.exe ${file_name} && start cmd /k ${file_base_name}.exe",

    // This is the same as the second cmd example above.
    "shell_cmd": "csc.exe /nologo ${file_name} && ${file_base_name}.exe",

    // If you want to use Terminus, these two lines tell the build to use
    // Terminus rather than the normal build. The change is that the Terminus
    // panel will allow your program to be interactive in a way standard builds
    // do not.
    //
    // If you are using the version of things that opens an external command
    // prompt, you're not going to gain anything from using Terminus since it
    // is only going to display the compiler output, which is not interactive.
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",

    // This line is optional and only allowed when using Terminus; it tells
    // Terminus to give the output panel the focus when it launches the build
    // so that you can interact with the program that's running without having
    // to manually click in the panel.
    "focus": true,

    // These parts are common no matter what
    "file_regex": "^(...?):([0-9]):?([0-9]*)",
    "working_dir": "$file_path",
    "selector": "source.cs",
    "quiet": true,

    // IF:
    //    1. You are using shell_cmd and not cmd
    //    2. csc is on the path
    // THEN:
    //    You don't need this key; it is telling Windows where to find the
    //    compiler by temporarily adjusting the path for this launch.
    //
    //    If you're using this, make sure the locaton specified here is
    //    correct (in particular, that it has the correct .NET version number).
    "env": {
        "PATH": "$PATH;C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319"
    }
}
1 Like

#6

hi
it worked

there is a problem when i changed it from shell_cmd to cmd and tried it it didn t work and when i revert it to shell_cmd it didn t work and i created a new build(with the same name)and it didnt work

0 Likes

#7

The build as outlined above works for me (I tested it on my Windows machine before posting it) so you may want to investigate what yours is doing (or not doing) that the example above is doing (or not doing).

If you can say what the problem you’re seeing here (if any), we can troubleshoot it a bit.

0 Likes

#8

when i click cntr+b nothing happened and it doesn t say no build

0 Likes

#9

See any errors in the Sublime Console (View > Show Console)?

In particular, the example build above does not have some of the keys that you specified in your original build because terminus_exec doesn’t like them, which will cause it to fail.

0 Likes

#10

ok nvm i just reopened sublime and it worked
and thank you for helping me
and i got last question why shell is better than cmd?

0 Likes

#11

In my experience, once you make Terminus angry by providing it an argument it didnt’ expect, it does not always recover without a restart, even when you fix the problem in the build itself.

shell_cmd is a single string that you pass that is the command line as you would pass it directly in a terminal window, so it is a 1:1 mapping of what you would manually type versus what you tell Sublime to do.

cmd is an array of strings that tells Sublime to directly execute the first thing in the array, passing the remainder as arguments, which bypasses all of the normal terminal handling.

The two both have their place, but when you want to use && or || to chain commands, or use input and output redirection, you must use the shell to do that.

There are two ways to do that:

  1. Just use shell_cmd, which makes it clear exactly what you intend to do

  2. Use cmd to form the same command line in a longer and more verbose way, and then ALSO add "shell": true to tell the system that behind the scenes it should just go ahead and try to turn it into a shell_cmd on its own.

Technically speaking, they both do the same thing; you have more control over what happens in that case if you manually create the shell_cmd yourself though (and it’s less work and easier to read, arguably).

0 Likes

#12

ok thank you

0 Likes

#13

It is worth nothing csc.exe is part of the legacy .NET framework. Microsoft says they will continue support but there are no plans to add new language features. For net8.0 onward which does support the latest version of C# (C# 12), it is recommended to run the dotnet CLI via shell_cmd.

2 Likes

#14

oh ok thank you for the info.

0 Likes