Home > Software design >  How to add a c# file to Visual Studio Solution Explorer at runtime?
How to add a c# file to Visual Studio Solution Explorer at runtime?

Time:08-05

Consider the code snippet below for a split second:

  static void Main(string[] args)
  {
      //...
      File.Create(path);
  }

This, obviously, creates a file in a given path. However, created file does not appear in the solution explorer.

How to extend this function, so that after running it, the newly created file would appear at the solution explorer and would be part of a project?

Updated

To add the newly created file to a project, just open and modify the .csproj file.

CodePudding user response:

If you want to add the created file to the project, just open the .csproj file and add the relevant code.

In each example, the following item declaration was used to include the file C:\MyProject\Source\Program.cs in the project.

<ItemGroup>
    <MyItem Include="Source\Program.cs" />
</ItemGroup>
  • Related