Windows 10 – Unable to Associate .txt File with Visual Studio Code

visual-studio-codewindows 10

I just installed Visual Studio Code 1.28.1, but I'm unable to associate it with the .txt extension on Windows 10.

I'm well aware how to associate an extension with an application on Windows. I can right-click in Explorer, select, "Open As…", select the application, and make sure "Always open with this app" is checked. Or I can go in to the properties of a file in Explorer, look at "Opens with", and click on "Change". Or I can go to "Choose default apps by file type …", etc. The problem is that none of this is working with VS Code.

Currently my .txt files are associated with EmEditor. I change the association to Notepad. No problem. I change the association back to EmEditor. That works.

But if I select the blue "Visual Studio Code" option that appears in the list of applications, nothing changes. The old association remains.

I even tried ignoring the blue icon in the selection list, and manually browsed and selected %LocalAppData%\Programs\Microsoft VS Code\Code.exe as the default editor. That doesn't work, either. Nor did manually selecting %LocalAppData%\Programs\Microsoft VS Code\bin\code.cmd.

In case you think this might be related to EmEditor, I tried to associate .tidyconfig files (which on my system are currently not associated with any editor at all) with VS Code. It did nothing.

What's going on? Associating VS Code with my text files should be the most basic of basic Windows integration. Why won't it work?

(I filed this as VSCode Issue #61084, but I had no responses. I'm filing it here in case the "bug" is some sort of user error.)

Best Answer

This isn't user error or a Code bug. Microsoft have broken how file associations work in the latest alpha. I found a workaround which works in Windows 10 Pro - Version 1803 - OS Build 17134.320.

Find the command to start Visual Studio Code with a named file: click Start, type "code", right click Visual Studio Code and select Open File Location.

Right click the shortcut, click Properties and copy the contents of Target. This will be something like "C:\Users\[your name]\AppData\Local\Programs\Microsoft VS Code\Code.exe" "%1"

In Registry Editor, create HKEY_CLASSES_ROOT/.txt and set Default to txt_auto_file. Now create HKEY_CLASSES_ROOT/txt_auto_file/shell/open/command and set Default to the text you copied from Target.

At this point you can stop because double clicking a .txt file will now open it in Code. However the icon in Explorer is wrong. Right click the .txt file and select Open With. You'll now find everything works as it should; you can tick Always Use and this will fix the icon.

Related Question