locked
"Replace in Files" file modification/save event for Documents not opened in Visual Studio RRS feed

  • Question

  • Hi,

    As part of our Visual Studio plugin, we would like to take some actions (e.g. SCC check-out, etc.) upon files modification.

    Whereas it is working fine for Documents already opened in the Editor (via TextEditorEvents.LineChanged, TextDocumentKeyPressEvents.BeforeKeyPress or even IVsRunningDocTableEvents3), I cannot figure out how to do the same for the ones which are not e.g. When executing "Replace in Files", on a Project or Solution, we would like to check-out (via Perforce)  the resulting files before being automatically saved by Visual Studio.

    Thanks,

    Manu.

    Monday, February 10, 2020 9:59 PM

All replies

  • Hi EmBeau,

    Welcome to MSDN forum.

    If you don't want to open file in VS IDE, you could directly use File class which is in System.IO namespace, to replace and save file.

    string text = File.ReadAllText("test.txt");
    text = text.Replace("some text", "new value");
    File.WriteAllText("test.txt", text);

    Besides, you could also create a new file which save your the replacement result.

    using (FileStream fs = File.Create(path))
    {
        string text = File.ReadAllText(path);
        Byte[] info = new UTF8Encoding(true).GetBytes(text);
    
        fs.Write(info, 0, info.Length);
    }

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, February 11, 2020 9:46 AM
  • Hi Dylan,

    I think I did not explain my issue properly.

    When a User is executing "Replace All" from "Replace in Files" dialog, I need to somehow intercept the fact that Visual Studio is writing to the files where the text is being replaced in order to do some actions such as SCC checkout.

    I'm able to get events for the files which are already opened in the Editor (via TextEditorEvents.LineChanged, TextDocumentKeyPressEvents.BeforeKeyPress or even IVsRunningDocTableEvents3) but not for the ones which are not.

    Cheers,

    Manu.

    Tuesday, February 11, 2020 10:01 AM
  • Hi EmBeau,

    Thank you for reply.

    After my research, I'm afraid that there is no appropriate method or event to do this. Maybe you could create a temporary file which is used to preview the changes when do replacement.

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, February 14, 2020 8:25 AM