Package for compiling & executing C# files?


#1

Hello,
I very recently began coding C# console applications (the only type of C# coding that I’m familiar with so far), and wish to use Atom for that purpose (though I’ve been using Atom for quite some time).
I started looking into ways to run C# code. At first, I tried the omnisharp package but it produced nothing but errors (should I try harder to make it work?). Second, I used the Script package which supports C# using ‘csc.exe’ (which needs to be added to PATH) and it does execute C# files. However, Script presumably cannot handle user inputs, which is necessary for my uses, so Script isn’t a solution for me. Another way is to compile files manually using csc.exe (via cmd). This makes an .exe in the same directory as the C# file, that once opened works well. This process does achieve the goal but is a bit tedious.
Therefore, what I’m looking for is a package that performs the process of compiling the focused C# file and opening the resulting .exe file in a separate console window; much like the functionality of the gcc-make-run package with C for example.
I would be very grateful for ideas and solutions.
Thanks a lot,
Daniel


#2

Please provide details on how compiling C# files works with csc.exe. I might be able to provide some guidance on how you could automate it, but I need to know the step-by-step process.


#3

Setup: Required to have csc.exe on PATH (C:\Windows\Microsoft.NET\Framework**\v*.*.***** on windows).

To compile files:

  1. Navigate to the file’s directory in cmd
  2. Enter in cmd: csc.exe FILENAME

#4

Here’s how I know to do it.

  1. Install the package process-palette and open the Edit Configuration view (available through the Packages menu and the Process Palette: Edit Configuration command).
  2. Create a new command with the following settings:

  1. Scroll down to Output and select Void.

  1. Switch to the file that you want to compile and run Process Palette: Toggle (also in the menu item) to pull up a docked view with your new command. Press the play button to run your command.
  2. Since the output is Void, the only visible result is a new command line window being created with the results of your script.

#5

cmd popped up like

error CS2001: Source file 'HelloUser.cs' could not be found
warning CS2008: No source files specified

C:\Users\user\AppData\Local\atom\app-1.31.1

and no .exe was created.

That error also happens when the argument doesn’t exist directly inside the current directory.
So Process-Palette seems to call csc.exe from app-1.31.1 instead of the directory of the current file.
Any ideas on how to solve this?


#6

That’s the Working Directory setting, at the bottom of my first image. {fileProjectPath} is a variable that inserts the absolute path of the project folder for the file you have open. Without that setting, anything you run is run with a cwd of wherever Atom is running from.


#7

I see. I got it to work now, but there are 3 issues visible.

  1. The open project folder must be directly above the C# file, so if the file is 2 layers or more bellow the open project folder, csc.exe won’t recognize it and the same error will be thrown.
  2. After compiling, the resulting .exe file should also be be opened.
  3. A process-palette.json file is generated in the directory that csc.exe is called from.

So:

  1. Is there a way to call csc.exe with the file name from the folder directly above the file itself and not from the open project? This would both fix the problem mentioned above and also will allow for running a file without it being inside the open project folder.
  2. Is there a way to open the resulting .exe file (which might take a few seconds to be generated) after the compilation?
  3. Is there a way not to generate the .json file or just delete it after running the file?

#8

You’re right. I should have used the {filePath} variable in the command instead of {fileNameExt}.

After compiling, the resulting .exe file should also be be opened.

How does the executable get named?

A process-palette.json file is generated in the directory that csc.exe is called from.

You created local commands for your current project. If you edit the global commands, the package will use a process-palette.json file in your .atom/ folder.


#9

That doesn’t seem to work when the file is 2 layers or more below the open project folder; no error is thrown, but no .exe is generated either. It does work however when the file is directly below the open project folder.
Can you also elaborate on what {filePath}, {fileNameExt}, and {fileProjectPath} mean in this context?

The same way as the C# file but with an .exe suffix instead of .cs.


#10

That’s interesting. It should at least give you an error. Well, you can always try {fileAbsPath}. To be honest, there’s probably no reason to not use that for cases like this, but my habit has been to use the most relative possible variable that meets my needs.

Can you also elaborate on what {filePath} , {fileNameExt} , and {fileProjectPath} mean in this context?

You can see them, and the other variables, by clicking on the Insert Variable button or checking out the process-palette documentation.


#11

My recent experiments show that an .exe file is actually generated, just not in the C# file’s directory but in the working directory of the cmd (sorry for not noticing that).
Thanks to you and the documentation you referenced, I managed to get the compile-c# command to compile the file no matter where it is and to generate an .exe file in its directory, regardless of the project (if any) that’s open in Atom.
Shell Command: start cmd /k "csc {fileNameExt}"
Working Directory: {fileDirAbsPath}

Now, what’s left is opening the .exe file, which can be done in a 2 ways:
After the file was generated (which takes a couple of seconds), process-palette should either…

  1. run {fileName}.exe in the cmd window that process-palette has opened.
    That will make the .exe run inside that cmd window.
    Preferably, run cls in the cmd window first, so that the long message that shows up in cmd when compiling using an older version of csc.exe (which I have) gets cleared and that the .exe runs in a clean interface.
    OR:
  2. run start {fileName}.exe in the cmd window that process-palette has opened.
    That will open the .exe in a different console window.
    Preferably, run exit in the cmd window (not the console window of the .exe) afterward or close the cmd in another way.

I’d like to be have 2 process-palette commands, one for each of the ways discussed above. However, I couldn’t figure out how to run another command in the cmd window.
How would I go about making those 2 commands?


#12

process-palette is not well-equipped to do this, but your system shell is. The && operator allows you to string multiple commands together, so that the second one gets fired after the first one is resolved. Try this out:

echo "hi" && echo "bye"

So a command string that looks like csc {fileNameExt} && {fileName}.exe should compile and run the code.

Preferably, run cls in the cmd window first

Same principle as above: csc {fileNameExt} && cls && {fileName}.exe.

Preferably, run exit in the cmd window (not the console window of the .exe ) afterward or close the cmd in another way.

That’s the /k switch. If you replace it with /c, the window will close automatically on completion. If your program is something that uses the terminal, it will stay open until the program concludes.

So try out start cmd /k "csc {fileNameExt} && cls && {fileName}.exe" for when you want to keep the terminal open and start cmd /c "csc {fileNameExt} && cls && {fileName}.exe" for when you don’t.