296 questions
How to open a untitled file
Vasu Bansal
20
Reputation points
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
Sign in to answer