How to open a untitled file

Vasu Bansal 20 Reputation points
2025-06-19T09:52:38.9633333+00:00

I want to open a Untitled file in visual studio, when i open the file it is supposed to save in the disk until user explicitly saves it and also I want to attach my Editor factory or language service to that untitled file

I am using this code but it is not invoking save.

string filePath = GetNextAvailableUntitledFilePath();
Guid EditorFactoryGuid = new Guid("GUID");
// Create empty or initial-content file
File.WriteAllText(filePath, "");
VsShellUtilities.OpenDocumentWithSpecificEditor(
    ServiceProvider.GlobalProvider,
    filePath,
    EditorFactoryGuid ,
    VSConstants.LOGVIEWID_Primary,
    out IVsUIHierarchy hierarchy,
    out uint itemId,
    out IVsWindowFrame windowFrame
);
windowFrame?.Show();


private string GetNextAvailableUntitledFilePath()
 {
     int counter = 1;
     string filePath;
     while (true)
     {
         filePath = Path.Combine(_tempDir, $"Untitled-{counter}.sql");
         if (!File.Exists(filePath))
             return filePath;
         counter++;
     }
 }
Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Omkara Varshitha Kunapalli (INFOSYS LIMITED) 385 Reputation points Microsoft External Staff
    2025-08-19T09:40:49.31+00:00

    Thanks for reaching out!

    To achieve the desired behavior—opening an untitled file that doesn’t persist to disk until explicitly saved—you’ll need to:

    1. Avoid writing the file to disk initially. Instead, use a memory stream or in-memory buffer to simulate the file content.
    2. Use a custom editor factory that supports untitled documents. This typically involves implementing IVsEditorFactory and handling the CreateEditorInstance method to manage the document lifecycle.
    3. Use IVsUIShellOpenDocument or IVsRunningDocumentTable to register the document without a backing file, and defer saving until the user triggers it.
    4. Set up your language service via MEF (Managed Extensibility Framework) and ensure it’s registered to handle the file extension or content type.

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.