1

I made a visual studio extension that adds a file to the project, however I'm having a lot of trouble actually adding the file to the project.

The extension adds a button to Visual Studio. Clicking the button will add an interface to the project. If a file with that name already exists, it will ask whether or not you want to overwrite it.

In the past I tried something along the lines of DTE.ItemOperations.NewFile. This worked, however VS would crash if you tried overwriting a file that you previously made with this extension. If it was a file made the normal way of adding new files it wouldn't crash. Another issue I ran into with this was every file that was added showed up as a "Miscellaneous File". I'd have to close the tab and reopen it for it to show as a CS file.

My solution to "crashing when renaming" was to instead just use File.WriteAllText and have it write the code I wanted to be in the file. This works if Visual Studio automatically adds the file to the project, however sometimes it doesn't do that and I have to manually add an existing item.

Not sure where to go from here, any info is appreciated

1 Answer 1

3

You can look how madskristensen/AddAnyFile extension is implemented.

You need to get a project reference first and then use project.AddFileToProject(file) or target.ProjectItem.ProjectItems.AddFromFile(file.FullName).

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I need. Thank you!

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.