我是C#的初学者,我有一个文件夹,我正在从中读取一个文件。
我想要读取位于解决方案文件的父文件夹中的文件。我该怎么做?
string path = "";
StreamReader sr = new StreamReader(path);
如果我的文件XXX.sln
位于C:\X0\A\XXX\
,那么请读取位于C:\X0\A\
的.txt
文件。
我是C#的初学者,我有一个文件夹,我正在从中读取一个文件。
我想要读取位于解决方案文件的父文件夹中的文件。我该怎么做?
string path = "";
StreamReader sr = new StreamReader(path);
如果我的文件XXX.sln
位于C:\X0\A\XXX\
,那么请读取位于C:\X0\A\
的.txt
文件。
您可能更喜欢这种更通用的解决方案,它取决于通过扫描当前或选定目录的所有父目录来找到解决方案*.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
试一试:
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);
如果你的应用程序依赖于文件路径和解决方案路径之间的关系来确定文件位置,那么我认为这是不足够的。虽然你的程序可能会在 Solution/Project/Bin/$(ConfigurationName)/$(TargetFileName)
路径下执行,但这仅适用于在Visual Studio中执行的情况。在其他场景下,这并不一定成立。
我看到两个选项:
Include the file as part of your project, and in its' properties, have it copied to the output folder. You can then access the file thusly:
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Yourfile.txt");
Note, during deployment you'll have to ensure that this file is also deployed alongside your executable.
Use command line arguments to specify the absolute path to the file on startup. This can be defaulted within Visual Studio (see Project Properties -> Debug Tab -> Command line arguments". e.g:
filePath="C:\myDevFolder\myFile.txt"
There's a number of ways and libraries concerning parsing the command line. Here's a Stack Overflow answer on parsing command line arguments.
string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName;
需要使用 System.IO
如果出于某些原因,您想将解决方案路径编译到您的项目中,您可以使用 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 中工作。
public static DirectoryInfo? TryGetSolutionDirectoryInfo(string? currentPath = null)
{
DirectoryInfo? directory = new (
currentPath ?? Directory.GetCurrentDirectory());
while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}
return directory;
}
string path = Application.StartupPath;
解决方案文件
所在的文件夹上方。 - User1204501