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

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.