C# 使用C获取解决方案文件的父文件夹的路径#
我是C#的初学者,我有一个文件夹,可以从中读取文件 我想读取位于解决方案文件父文件夹中的文件。我该怎么做C# 使用C获取解决方案文件的父文件夹的路径#,c#,file,path,io,streamreader,C#,File,Path,Io,Streamreader,我是C#的初学者,我有一个文件夹,可以从中读取文件 我想读取位于解决方案文件父文件夹中的文件。我该怎么做 string path = ""; StreamReader sr = new StreamReader(path); 因此,如果我的文件XXX.sln位于C:\X0\A\XXX\中,那么请阅读C:\X0\A\中的.txt文件,我觉得如果您的应用程序依赖于基于文件路径和解决方案路径之间关系的文件位置,那将是一种疏忽。虽然您的程序很可能在解决方案/Project/Bin/$(配置名称)/$(
string path = "";
StreamReader sr = new StreamReader(path);
因此,如果我的文件
XXX.sln
位于C:\X0\A\XXX\
中,那么请阅读C:\X0\A\
中的.txt
文件,我觉得如果您的应用程序依赖于基于文件路径和解决方案路径之间关系的文件位置,那将是一种疏忽。虽然您的程序很可能在解决方案/Project/Bin/$(配置名称)/$(目标文件名)
上执行,但只有在Visual Studio的范围内执行时,它才起作用。在VisualStudio之外的其他场景中,情况未必如此
我看到两种选择:
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Yourfile.txt");
注意,在部署期间,您必须确保此文件也与可执行文件一起部署我想这正是你想要的。不过,我不确定出版时这是否是个好主意:
string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName;
需要使用System.IO;
尝试以下操作:
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);
您可能会喜欢这个更通用的解决方案,这取决于通过扫描当前或选定目录中的所有父目录来查找解决方案
*.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.anchor
或mySuperSpecificFileNameToRead.ext
;P如果您想让我写我能写的方法,请告诉我。”
现在,您可能真的很喜欢!:D如果出于某种原因您希望在项目的解决方案路径中编译,您可以使用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) #>";
}
}
使用制度;
使用System.IO;
名称空间解决方案信息
{
公共静态类路径
{
静态字符串解决方案路径=@“”;
}
}
我认为Tah只能在Visual Studio中工作。从某种意义上说是父文件?.txt文件就是这个文件type@Leez父文件夹。因此,上面的
解决方案文件
所在的文件夹。您的意思是.sln文件文件夹ryt?但您随后部署了它???如果您想要完全正确的解决方案文件夹,请签出:@User1204501,我已看到编辑。具有您的程序根据其“解决方案目录”的位置加载一个文本文件是在自找麻烦。只有在解决方案文件夹中调试时才起作用。如果将exe复制到另一个位置,这将失败。@RvdK是的,当然会失败。但他的问题非常明确,是关于解决方案文件的位置。请阅读问题最好的答案是解释会出现什么样的陷阱,并提供更好的答案。(虽然目前它在解决方案文件中,但最好的解决方案是将文件放在同一文件夹als de executable中,以避免复制等问题)。在这种情况下,使用@“.\…\abc.txt”更容易。这也取决于项目设置,但似乎不太复杂。只有在解决方案文件夹中调试时才起作用。如果将exe复制到其他位置,这将失败。我不明白为什么会出现否决票。他的问题非常明确地涉及解决方案文件的位置。有时,最好的答案是解释哪种类型许多陷阱将会出现,并提供一个更好的答案。这是一个学术练习(他自己也这么说)依赖于解决方案文件。在我看来,它与真实世界的发行版并不相关。完整的命名空间是什么?我找不到that@KolobCanyon在System.Windows.FormsBy far中,这是最好的答案,而目录输出协调会随着时间的推移而变化,这样我们就可以随时从其他环境中提取解决方案的文件夹(例如,当构建从AnyCPU更改为x86时,默认路径将从bin\Debug更改为bin\x86\Debug)。
// resolve file path
var filePath = Path.Combine(
VisualStudioProvider.TryGetSolutionDirectoryInfo()
.Parent.FullName,
"filename.ext");
// usage file
StreamReader reader = new StreamReader(filePath);
<#@ 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) #>";
}
}