简体   繁体   English

如何从 Visual Studio Package 项目中获取当前的解决方案名称?

[英]How to get the current solution name from a Visual Studio Package project?

I created a Visual Studio Package project with a custom menu I would like to add to Visual Studio (2013).我创建了一个带有自定义菜单的 Visual Studio Package 项目,我想添加到 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.dte.Solution.FullName始终为空。

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

Any ideas what i'm missing?任何想法我缺少什么?

Thanks谢谢

PS the solution I used is taken from here: PS我使用的解决方案取自这里:
How do you get the current solution directory from a VSPackage? 如何从 VSPackage 获取当前解决方案目录?

Try this:尝试这个:

var solutionName = Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

You need to be using System.IO and System.Diagnostics .您需要使用System.IOSystem.Diagnostics There also might be some file extensions at the end of solutionName that you will need to trim.solutionName的末尾可能还有一些文件扩展名,您需要对其进行修剪。

You can achieve it by finding the .sln file up the directory tree from your executing assembly:您可以通过从正在执行的程序.sln在目录树中查找.sln文件来实现它:

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"

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 nuget 包中获取项目/解决方案的名称 - How to get the name of a project/solution from a nuget package 如何获取当前Active Solution Platform名称以在Visual Studio加载项中使用? (C#) - How can I get the current Active Solution Platform name for use in a Visual Studio add-in? (C#) 如何在 Visual Studio 中更改整个解决方案的项目名称? - How to change whole solution's project's name in Visual Studio? 如何获取当前Visual Studio解决方案中的项目列表? - How to get list of projects in current Visual studio solution? 如何使用Visual Studio Extension从当前解决方案中收集类型? - How to collect types from current solution using Visual Studio Extension? 如何从 Visual Studio 解决方案/项目中读取文件? - How to read file from Visual Studio solution/project? 如何从Visual Studio中的项目解决方案中删除其他构建文件 - How to Delete Additional build files from the Project Solution in visual studio 如何通过Visual Studio插件获取Visual Studio Project(在解决方案资源管理器中加载)的位置? - How to get Visual Studio Project (which is loaded in solution explorer) location via a Visual Studio Addin? 如何使用 c# 在 Visual Studio 2010 中以编程方式将现有项目添加到当前解决方案 - How to add an existing project to a current solution programmatically in visual studio 2010 using c# 引用 a.Net Core 3.1 NuGet package 的 a.Net 5 Visual Studio 项目/解决方案如何编译成功? - How can a .Net 5 Visual Studio Project / Solution that reference a .Net Core 3.1 NuGet package compile successfuly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM