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.

Zahra Ahangari
  • 262
  • 4
  • 17
  • `'' > ./File.cs` should do the trick but it depends on your shell. – Aluan Haddad Jan 14 '21 at 12:34
  • Does this answer your question? [how to add a new c# file to a project using dotnet-cli](https://stackoverflow.com/questions/49087421/how-to-add-a-new-c-sharp-file-to-a-project-using-dotnet-cli) –  Jan 14 '21 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: ``. –  Jan 14 '21 at 12:38
  • @OlivierRogier usually, with `dotnet` and new-style csproj, you don't need to add the .cs to the .csproj – Marc Gravell Jan 14 '21 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? – Marc Gravell Jan 14 '21 at 12:49
  • @OlivierRogier Yeah I've seen the topic but still couldn't get the answer. It mostly suggests to do it in an editor but I'm looking for a way to do it through the command line. – Zahra Ahangari Jan 14 '21 at 12:53
  • @MarcGravell It's just the creating of the _.cs_ file in command prompt that I'm looking for. I will put the code in the class using the editor afterwards. – Zahra Ahangari Jan 14 '21 at 12:59
  • @Zahra You can create an empty file from the command line using the pipe redirection: `0 > test.cs` to create new or `echo any text >> test.cs` to append. –  Jan 14 '21 at 13:02

1 Answers1

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
Nima Airyana
  • 146
  • 5