locked
EnvDTE: Modifying a solution explorer File (C# File) Content Programmatically RRS feed

  • Question

  • Hi all,

    Can any one help me how to programatically modify a c# file content (which is in solution explorer)?

     If Class1.cs is one of my project files in solution explorer, how should i use EnvDte to modify its content? 



    Wednesday, October 16, 2013 11:36 AM

Answers

  • Hi Santosh,

    There is no need to use EnvDte here for modifying just the content of cs files. You can read them as any text file and then save, or you can also look into using T4 Templates in .NET for generating cs files.

    Thanks,

    Amit Kumar

    --------------

    Please mark this post as answer if it help you


    • Edited by Amit Tonk Wednesday, October 16, 2013 1:32 PM
    • Proposed as answer by Jane Wang - MSFT Thursday, October 17, 2013 7:30 AM
    • Marked as answer by Santhosh Ramini Thursday, October 17, 2013 9:10 AM
    Wednesday, October 16, 2013 1:31 PM

All replies

  • Hi Santosh,

    There is no need to use EnvDte here for modifying just the content of cs files. You can read them as any text file and then save, or you can also look into using T4 Templates in .NET for generating cs files.

    Thanks,

    Amit Kumar

    --------------

    Please mark this post as answer if it help you


    • Edited by Amit Tonk Wednesday, October 16, 2013 1:32 PM
    • Proposed as answer by Jane Wang - MSFT Thursday, October 17, 2013 7:30 AM
    • Marked as answer by Santhosh Ramini Thursday, October 17, 2013 9:10 AM
    Wednesday, October 16, 2013 1:31 PM
  • Hi Amit,

      Thanks for your answer. As per your answer, i can use FileStream class for editing. But how to get that file path (or how to get access to that file) with out using EnvDTE ?(since file to be edited is there in one of the projects in solution explorer)


    Wednesday, October 16, 2013 2:23 PM
  • Hi Santhosh,

    >>But how to get that file path (or how to get access to that file) with out using EnvDTE ?(since file to be edited is there in one of the projects in solution explorer)

    First,please take a look at the Automation Object Model Chart .

    In fact,the file path is one property of the projectitem.

    If the file could be opened in the Solution Explore, the file path could be accessed by the EnvDTE.

    You could use the ProjectItem::FileNames Property to get the full path and names of the files associated with a project item.

    Best Regards,

    Jane.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 17, 2013 7:30 AM
  • Hi jane,

    The path of the file to be edited is not fixed since the users who create the project may choose different locations for saving the projects, also, the file editing process is done from a user control hosted into a tool window (intiated from a menu command). I Agree with @Amit 'There is no need to use EnvDte here for modifying just the content of cs files'. But i think i have to get current running instance of visual studio (DTE) to know the path of the file to be edited (As path is dynamic based on the project creation).

      There may be chances of running multiple instances of VS with same type of projects but in different locations.

    Finally, i had to ask the question with more explanation which i didnt.

    Thursday, October 17, 2013 8:24 AM