How to open a new file in memory in visual studio using my editory factory

Vasu Bansal 20 Reputation points
2025-07-11T06:50:32.34+00:00

I want to open a new .sql file in memory not in the disc and also when the file is open i want to use my editory factory to it

I tried this method to create a new .sql in memory file but using this way I am not able to use my editory factory to it.

var dte = (EnvDTE.DTE)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE));
            if (dte != null)
            {
                EnvDTE.Window window = dte.ItemOperations.NewFile("General\\SQL File", "Untitled", EnvDTE.Constants.vsViewKindTextView);
                if (window != null)
                {
                    window.Activate();
}
}
Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Varsha Dundigalla(INFOSYS LIMITED) 795 Reputation points Microsoft External Staff
    2025-07-14T08:26:55.0033333+00:00

    Thank you for reaching out. please find the answer below:

    • Use IVsUIShellOpenDocument.OpenStandardEditor instead of DTE.ItemOperations.NewFile to open a file with your custom editor factory.
    • Set flags OSE_UseNewFile and OSE_OpenAsNewFile to ensure the file is treated as a new, unsaved, in-memory document.
    • Use a virtual filename like "Untitled.sql" or "inmemory://Untitled.sql" to avoid creating a physical file on disk.
    • Provide your custom editor factory’s GUID when calling OpenStandardEditor.
    • Ensure your editor factory is registered using [ProvideEditorExtension] or [ProvideEditorFactory].
    • Your editor factory must implement IVsEditorFactory and support virtual monikers and the VSEDITOR_CREATE_NEW_FILE flag.
    • Use VSConstants.LOGVIEWID_Primary as the logical view GUID.
    • Handle the result with Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr) for proper COM error checking.
    • Call frame.Show() to display the editor window once the file is opened.
    • This method ensures the file is opened entirely in memory and routed through your custom editor, not the default SQL editor.

    Let us know if the issue persists after following these steps. We’ll be happy to assist further if needed.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.