【问题标题】:Getting path to the parent folder of the solution file using C#使用 C# 获取解决方案文件的父文件夹的路径
【发布时间】:2013-09-30 20:07:45
【问题描述】:

我是 C# 的初学者,我有一个文件夹,我正在从中读取文件。

我想读取位于解决方案文件的父文件夹中的文件。我该怎么做?

string path = "";
StreamReader sr = new StreamReader(path);

因此,如果我的文件 XXX.sln 位于 C:\X0\A\XXX\ 中,则读取 .txt 中的 C:\X0\A\ 文件。

【问题讨论】:

  • 某种意义上的父文件?
  • .txt文件为文件类型
  • @Leez 父文件夹。所以solution file所在的文件夹上方
  • 你的意思是 .sln 文件夹 ryt?但是你然后部署它???
  • 如果您想要完全正确的解决方案文件夹,请查看:stackoverflow.com/a/67492470/15343165

标签: c# file path io streamreader


【解决方案1】:

您可能会喜欢这种更通用的解决方案,它取决于通过扫描当前或选定的父目录中的所有父目录来找到解决方案*.sln 文件,同时涵盖未找到解决方案目录的情况!

public static class VisualStudioProvider
{
    public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null)
    {
        var directory = new DirectoryInfo(
            currentPath ?? Directory.GetCurrentDirectory());
        while (directory != null && !directory.GetFiles("*.sln").Any())
        {
            directory = directory.Parent;
        }
        return directory;
    }
}

用法:

// get directory
var directory = VisualStudioProvider.TryGetSolutionDirectoryInfo();
// if directory found
if (directory != null)
{
    Console.WriteLine(directory.FullName);
}

在你的情况下:

// resolve file path
var filePath = Path.Combine(
    VisualStudioProvider.TryGetSolutionDirectoryInfo()
    .Parent.FullName, 
    "filename.ext");
// usage file
StreamReader reader = new StreamReader(filePath);

享受吧!

现在,一个警告......您的应用程序应该与解决方案无关 - 除非这是我不介意的某个解决方案处理工具的个人项目。请理解,您的应用程序一旦分发给用户驻留在没有解决方案的文件夹中。现在,您可以使用“锚”文件。例如。像我一样搜索父文件夹并检查是否存在空文件 app.anchormySuperSpecificFileNameToRead.ext ;P 如果你想让我写我能写的方法 - 请告诉我。

现在,您可能真的很享受! :D

【讨论】:

  • 到目前为止是最好的答案,而目录输出协调会不时改变,这样我们总是可以在其他情况下获取解决方案的文件夹(例如,当构建从 AnyCPU 更改为 x86 时,默认路径将从 bin\Debug 更改为 bin\x86\Debug)。
【解决方案2】:

试试这个:

string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName,"abc.txt");

// Read the file as one string. 
string text = System.IO.File.ReadAllText(startupPath);

【讨论】:

  • 仅在解决方案文件夹中调试时有效。如果 exe 被复制到另一个位置,这将失败。
  • @RvdK 是的,它肯定会失败。但他的问题非常具体是关于解决方案文件的位置。请阅读问题
  • 有时最好的答案是解释会发生什么样的陷阱并提供更好的答案。 (虽然目前它在解决方案文件中,但最好的解决方案是将文件放在与可执行文件相同的文件夹中,以避免复制等问题。)
  • 在这种情况下使用@"..\..\abc.txt" 更容易。它也取决于项目设置,但似乎不那么复杂。
【解决方案3】:

我觉得,如果您的应用程序基于文件路径和解决方案路径之间的关系依赖文件的位置,那将是失职。虽然您的程序很可能在 Solution/Project/Bin/$(ConfigurationName)/$(TargetFileName) 处执行,但仅当您在 Visual Studio 范围内执行时才有效。在 Visual Studio 之外,在其他情况下,情况不一定如此。

我看到两个选项:

  1. 将文件作为项目的一部分包含在内,并在其属性中将其复制到输出文件夹。然后您可以访问该文件:

    string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Yourfile.txt");
    

    请注意,在部署期间,您必须确保此文件也与您的可执行文件一起部署。

  2. 使用命令行参数指定启动时文件的绝对路径。这可以在 Visual Studio 中默认设置(参见项目属性 -> 调试选项卡 -> 命令行参数”。例如:

    filePath="C:\myDevFolder\myFile.txt"
    

    有许多关于解析命令行的方法和库。 Here's a Stack Overflow answer 解析命令行参数。

【讨论】:

  • @User1204501,我已经看到了编辑。让您的程序根据其解决方案目录的位置加载文本文件是自找麻烦。
【解决方案4】:

我想这就是你想要的。不确定发布时是否是个好主意:

string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName;

需要using System.IO;

【讨论】:

  • 仅在解决方案文件夹中调试时有效。如果 exe 被复制到另一个位置,这将失败。
  • 我不明白为什么这是一个downvote。他的问题非常具体地是关于解决方案文件的位置。
  • 有时最好的答案是解释会发生什么样的陷阱并提供更好的答案。
  • 他是一个依赖于解决方案文件的学术练习(他自己这么说)。在我看来,它与现实世界的分布并没有真正的关系。
【解决方案5】:
string path = Application.StartupPath;

【讨论】:

  • 什么是完整的命名空间?我找不到那个
  • @KolobCanyon in System.Windows.Forms
【解决方案6】:

如果由于某种原因您想在项目的解决方案路径中编译,您可以使用 T4 模板来执行此操作。

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#@ parameter name="model" type="System.String" value=""#>
<#
    IServiceProvider serviceProvider = (IServiceProvider)this.Host;
    DTE dte = serviceProvider.GetService(typeof(DTE)) as DTE;
#>
using System;
using System.IO;

namespace SolutionInfo
{
    public static class Paths
    {
        static string solutionPath = @"<#= Path.GetDirectoryName(dte.Solution.FullName) #>";
    }
}

我认为 Tah 只能在 Visual Studio 中工作。

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多