如何从 Visual Studio Package 项目中获取当前的解决方案名称? [英] How to get the current solution name from a Visual Studio Package project?

查看:59
本文介绍了如何从 Visual Studio Package 项目中获取当前的解决方案名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有自定义菜单的 Visual Studio Package 项目,我想添加到 Visual Studio (2013) 中.

我正在尝试在运行时获取当前的解决方案名称/目录.

我已经尝试过这个解决方案:

DTE dte = (DTE)GetService(typeof(DTE));string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

dte.Solution.FullName 始终为空.

我虽然这与我在调试模式下运行并且为此目的创建了一个新的 Visual Studio 实例有关,但是当我安装我的扩展并在运行时从 Visual Studio 运行它时也会发生这种情况任何菜单.

对我缺少什么有什么想法吗?

谢谢

附言我使用的解决方案取自此处:

I created a Visual Studio Package project with a custom menu I would like to add to Visual Studio (2013).

I'm trying to get the current solution name/directory in run time.

I've tried this solution:

DTE dte = (DTE)GetService(typeof(DTE));
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

but dte.Solution.FullName is always empty.

I though it is related to the fact that i'm running in debug mode and a new instance of Visual Studio is created for this purpose, but it happened also when I installed my extension and ran it from Visual Studio as I ran any menu.

Any ideas what i'm missing?

Thanks

P.S. the solution I used is taken from here:
How do you get the current solution directory from a VSPackage?

解决方案

You can achieve it by finding the .sln file up the directory tree from your executing assembly:

public static class FileUtils
{
    public static string GetAssemblyFileName() => GetAssemblyPath().Split(@"\").Last();
    public static string GetAssemblyDir() => Path.GetDirectoryName(GetAssemblyPath());
    public static string GetAssemblyPath() => Assembly.GetExecutingAssembly().Location;
    public static string GetSolutionFileName() => GetSolutionPath().Split(@"\").Last();
    public static string GetSolutionDir() => Directory.GetParent(GetSolutionPath()).FullName;
    public static string GetSolutionPath()
    {
        var currentDirPath = GetAssemblyDir();
        while (currentDirPath != null)
        {
            var fileInCurrentDir = Directory.GetFiles(currentDirPath).Select(f => f.Split(@"\").Last()).ToArray();
            var solutionFileName = fileInCurrentDir.SingleOrDefault(f => f.EndsWith(".sln", StringComparison.InvariantCultureIgnoreCase));
            if (solutionFileName != null)
                return Path.Combine(currentDirPath, solutionFileName);

            currentDirPath = Directory.GetParent(currentDirPath)?.FullName;
        }

        throw new FileNotFoundException("Cannot find solution file path");
    }
}

Results:

FileUtils.GetAssemblyFileName();
"CommonLibCore.dll"
FileUtils.GetAssemblyPath();
"G:\My Files\Programming\CSharp\Projects\MyAssemblyMVC\MyAssemblyConsole\bin\Debug\netcoreapp3.1\CommonLibCore.dll"
FileUtils.GetAssemblyDir();
"G:\My Files\Programming\CSharp\Projects\MyAssemblyMVC\MyAssemblyConsole\bin\Debug\netcoreapp3.1"
FileUtils.GetSolutionFileName();
"MyAssemblyMVC.sln"
FileUtils.GetSolutionPath();
"G:\My Files\Programming\CSharp\Projects\MyAssemblyMVC\MyAssemblyMVC.sln"
FileUtils.GetSolutionDir();
"G:\My Files\Programming\CSharp\Projects\MyAssemblyMVC"

这篇关于如何从 Visual Studio Package 项目中获取当前的解决方案名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆