3

I'm using .net core to create and run some very simple C# code through the command line. I create a new console app using dotnet new console . It creates a .csproj file in the name of the directory I'm in (classes). So I have a classes folder which contains a classes.csproj file, a Program.cs file and some other folders.

In order to create some classes I need some other .cs files here, but I can't find out how to do it. I've tried the File:New command according to https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/intro-to-csharp/introduction-to-classes, but I must be using the wrong syntax since it says:

The filename, directory name, or volume label syntax is incorrect.

Since I'm pretty much new to command line and coding(obviously), I've googled for some cmd syntax but still couldn't find anything to get it work.

I know It can be easily done in Visual Studio but I was wondering if there is a way to do it using cmd.

8
  • '' > ./File.cs should do the trick but it depends on your shell. Commented Jan 14, 2021 at 12:34
  • Does this answer your question? how to add a new c# file to a project using dotnet-cli Commented Jan 14, 2021 at 12:38
  • If you don't want to use VS or VS Code or any other IDE, you need to manually create the new .cs file and to manually add it to the .csproj using a text editor in an ItemGroup section: <ItemGroup><Compile Include="Dependencies\Windows Global Hotkey\Hotkey.cs"/></ItemGroup>. Commented Jan 14, 2021 at 12:38
  • @OlivierRogier usually, with dotnet and new-style csproj, you don't need to add the .cs to the .csproj Commented Jan 14, 2021 at 12:49
  • 1
    Creating an empty .cs file isn't really a very useful thing - you're going to need some editor to put code in it; and: any editor is going to be able to create a new file. So... why not just do that? Commented Jan 14, 2021 at 12:49

1 Answer 1

6

Unfortunately, there is no command line to create a C# file in dotnet cli

but if you want to create a C# file with the terminal you can use bellow command

in Linux bash OR git bash in windows

touch YourFileName.cs

in Windows cmd

type NUL > YourFileName.cs
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.