How to open Visual Studio in WSL2 and other apps?
Asked Answered
V

2

5

I have installed ubuntu 18.04 on wsl2. I know that with the command 'code .' you can open VS Code, but I want to know is there a commands for all Apps that u can open them from wsl2 terminal, like for example command for Visual Studio?

Vyner answered 10/3, 2021 at 15:34 Comment(2)
Just a heads-up - I'd recommend being careful with this type of question on Stack Overflow that isn't directly programming related. I think this one is borderline since you ask about "All apps, such as Visual Studio" (a programming tool, obviously). But really, this is a more general question that would be a better fit for Super User.Auscultate
Okey sir got it !Vyner
A
2

By default, WSL has "Interop" enabled, which allows you to run/launch any Windows executable from within WSL. Also by default, WSL appends the Windows path to your WSL path (also controlled in the same Interop section of /etc/wsl.confg).

So as long as the Windows application is in the path, appname.exe will work. For instance, notepad.exe will launch Notepad.

The reason that you don't need the .exe with VSCode is because the Windows version provides a shell script (code) that is designed to work with WSL.

For apps that are not on the path, you can either edit the WSL path (in, for instance, your .bashrc) or provide the full path like:

/mnt/c/Program\ Files/Windows\ Photo\ Viewer/ImagingDevices.exe

Note that you need to account for the difference between Linux/POSIX path and the Windows equivalent. For instance:

notepad.exe ~/test.txt

... will not work, since Notepad won't understand the Linux path. WSL provides the wslpath command to translate paths:

notepad.exe $(wslpath -w ~/test.txt)

or

notepad.exe '\\wsl$\Ubuntu\home\username\test.txt'

Please note: This is a bad example, just to make it easy to understand. Do not use notepad.exe to edit Linux files, as Notepad will add CRLF (DOS line-endings) which Linux apps will not handle properly.

Auscultate answered 10/3, 2021 at 16:8 Comment(0)
R
1

I've managed to run Visual Studio 2022 from WSL2 this way:

# Path to Visual Studio executable
VSRUN='C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Visual Studio 2022.lnk'

function open_in_vs() {
   # Take first passed argument and generate absolute path
   FIRST_ARG=`realpath $1`
   # Convert path (which is now in Unix style) to Windows path
   PATH_TO_FILE=`wslpath -w $FIRST_ARG`
   # Run process using PowerShell
   powershell.exe "Start-Process '${VSRUN}' '${PATH_TO_FILE}'"
}

# open_in_vs ./testfile.txt

# This adds an `openvs` alias you can use in your terminal to open any file in VS
alias openvs=open_in_vs
Rabbit answered 30/3 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.