Reputation: 330
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
Upvotes: 1
Views: 1941
Reputation: 27880
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).
Upvotes: 3