0

I'm writing an visual studio extension and I'm creating a read me file that I want to add to the c# project. I'm running this visual studio commands

var dte = (DTE2)ServiceProvider.GetService(typeof(DTE));
var tsFile = @"C:\Users\devVictorC\Documents\Visual Studio 2017\Projects\ClassLibrary1\ClassLibrary1\Readme.txt";
dte.ExecuteCommand("File.AddExistingItem", tsFile);

but I get this exception

System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.'

1 Answer 1

1

If you need add file to .csproj you can try this code:

var dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
var tsFile = filePath;
dte.ItemOperations.AddExistingItem(tsFile);

Also see MSDN documentation: ItemOperations.AddExistingItem Method

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

2 Comments

Thank you. Now, doesn't even compile because GlobalProvider is missing some references or doesn't exist. any advice?
@VictorAChavez do I understand correctly that you are using Visual Studio 2017? If it so, is the reference to the library Microsoft.VisualStudio.Shell.15.0 added to the project?

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.