如何在WSL2和其他应用程序中打开Visual Studio?

问题描述 投票:0回答:2

我已经在 wsl2 上安装了 ubuntu 18.04。我知道使用命令“code”。您可以打开 VS Code,但我想知道是否有适用于所有应用程序的命令,您可以从 wsl2 终端打开它们,例如 Visual Studio 的命令?

visual-studio ubuntu windows-subsystem-for-linux
2个回答
2
投票

默认情况下,WSL 启用“Interop”,它允许您从 WSL 中运行/启动任何 Windows 可执行文件。同样默认情况下,WSL 会将 Windows 路径附加到您的 WSL 路径(也在

/etc/wsl.confg
的同一 Interop 部分中进行控制)。

因此,只要 Windows 应用程序位于路径中,

appname.exe
就可以工作。例如,
notepad.exe
将启动记事本。

VSCode 不需要

.exe
的原因是因为 Windows 版本提供了一个旨在与 WSL 配合使用的 shell 脚本 (
code
)。

对于不在路径上的应用程序,您可以编辑 WSL 路径(例如,在您的

.bashrc
中)或提供完整路径,例如:

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

请注意,您需要考虑 Linux/POSIX 路径与 Windows 等效路径之间的差异。例如:

notepad.exe ~/test.txt

... 将不起作用,因为记事本无法识别 Linux 路径。 WSL 提供了

wslpath
命令来转换路径:

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

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

请注意:这是一个不好的例子,只是为了方便理解。不要使用 notepad.exe 编辑 Linux 文件,因为记事本会添加 CRLF(DOS 行结尾),而 Linux 应用程序将无法正确处理。


0
投票

我已成功通过这种方式从 WSL2 运行 Visual Studio 2019:

# 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
© www.soinside.com 2019 - 2024. All rights reserved.