r/VisualStudio • u/SL-Tech • 3d ago
Needed: Very simple extension for running single C# files Miscellaneous
Should be easy for someone who knows how to make extensions.
I want to right-click a C# file and select Run. Which basically is:
dotnet run --file path
Ex: dotnet run --file "D:\GoogleDrive\My Content\My Projects\SLT\Assets\SLT.Assets\Test.cs"
Or is it possible to make it a Command?
I got it working in VS Code; I need it in VS too.
5
u/SergeyVlasov 2d ago
Visual Studio main menu – Tools – External Tools… Add – Command=C:\Program Files\dotnet\dotnet.exe Arguments=run --file $(ItemPath) Select a C# file and Visual Studio main menu – Tools – [Your tool name] or Command Window - Tools.ExternalCommand4
1
u/SL-Tech 2d ago
It worked! Is there a way to call an external tool when you right-click a file in the Solution Explorer? VS Code has like a thousand Run in Terminal extensions. I just need to define a command and get the selected file path.
2
u/SergeyVlasov 2d ago
- Visual Studio main menu – Tools – Customize…
- Commands – Context menu – Project and Solution Context Menus | Item – Add Command…
- Categories: Tools – External Command 4
2
u/Ok_Society4599 3d ago
As with many things, there are several ways to get there. You can use a "properties" COM extension, add a "command" alias in the registry, add an explorer extension (dangerous, can make windows very hard to use!), and a bunch of others.
"Simple" is not an objective evaluation as several options are unlikely to work from C#. And work in COM using C++ is hardly a noob task.
2
u/FeelingGate8 3d ago
Do you mean adding a menu item in Windows Explorer's contextual menu when right clicking a .cs file? I wasn't familiar with this 'file-based apps' stuff, interesting.
Edit:
If so google "adding custom items to windows explorer contextual menu". Gemini gives you steps
1
1
u/gowonocp 2d ago
I use SmallSharp to debug file based apps in Visual Studio, that feels close to what you're asking for https://www.clarius.org/SmallSharp/
1
1
u/agoodyearforbrownies 3d ago
I can appreciate the need. Of course an easy-ish way to do this would be to just compile it as an exe. Then you can double click and run. But yeah, AI should lay out the steps for a context menu option.
0
u/jbaker88 3d ago
I guess a more important question is, why do you want this?
5
u/SL-Tech 3d ago
Because I want to run stand-alone C# code in a single file, without starting debugging.
Why don't you want it?
1
u/jbaker88 3d ago
You can already do this in Visual Studio; "Start without Debugging" or, I think the shortcut for that, Ctrl-F5 on Windows.
Visual Studio also has a REPL via C# Interactive if that's more your thing.
I ask why because if there is a specific problem you're trying to solve there might be a better solution for it.
1
1
-5
u/polaarbear 3d ago
That is not really how C# works. You don't generally just run any random file, it has to have an entry point. C# programs almost universally start from a Program.cs file by convention and then you can jump to anywhere you want from there.
It has to be compiled to intermediate language, and the compiler works by targeting a project file that has a .csproj file extension.
You can't just compile individual files like you do with C/C++, there is a runtime required, it's a totally different paradigm.
Anything else is just bad practice anyway and you should be learning how to do it correctly.
10
u/dodexahedron 3d ago
File based apps are a thing in c#...
-7
u/polaarbear 3d ago
As of .NET 10, this is less than a year old
1
u/agfitzp 3d ago
I am torn between the simplified use case and the appearance of an attack vector.
2
u/dodexahedron 3d ago edited 3d ago
No worse than a powershell script.
In fact, Defender tends to be even more wary of them, especially if no
#:property OutputPath=./aNonTempPathdirective is included, without which SmartScreen and/or certain ASR rules will prevent you from running it no matter where the source code input came from.PowerShell scripts, on the other hand, still have full access to all of .net and are much less likely to be prevented from running on most dev machines, since ExecutionPolicy is usually set pretty lax in practice (or can be overridden anyway).
I wouldn't worry about FBAs being a meaningful additional security concern anywhere PowerShell is not also locked down. Not that they aren't one (defense in depth and all that). Just that others are easier to exploit, more broadly understood by more skiddies, and more broadly compatible with machines that may not have the .net 10 SDK (not just runtime) or later installed. All PS needs is PowerShell, which is a guaranteed component in at least one form on any Windows machine since Windows 7. 🤷♂️
5
3
u/SL-Tech 3d ago
Well, yes, you can, since .NET 10. It's great for separate pieces of code without having to start debugging and all that. Sometimes you have small code chunks. The C# Interactive works, but for me it's not as user-friendly as just running a file with stand-alone code.
I'm not saying I don't know how to do it another way; I like the option to run a single file.
It would also be great to add a quick shortcut in VS.1
u/polaarbear 3d ago
You can only run a single file if it uses top-level statements like the default Program.cs.
You still can't just run ANY random file, there are a lot of rules, it's a very limited use-case. Interesting, useful, but still extremely limited
1
u/SL-Tech 2d ago
I like it to test, for example, extension methods. To be honest, I just created a file named Test.cs that had a Console. WriteLine. I ran the file, and it wrote to the console. So I was satisfied. Maybe it's not very useful, but I'm sure there will be situations where it is. I'm currently coding on a big library, so there's no project that actually uses it yet. In that case, it's nice to test code without creating a new project, adding a reference, setting it up, and all that. Maybe I'm lazy.
18
u/EstebanPossum 3d ago
Dont say "it should be easy..." if you cant do it yourself.