简体   繁体   English

如何转换 System.Collections.Generics.IENumerable<String> 串起来

[英]How to convert System.Collections.Generics.IENumerable <String> to string

I want to convert a variable obtained from a Directory.EnumerateFiles method to a string .我想将从 Directory.EnumerateFiles 方法获得的变量转换为 string 。 This Variable Contains a set of file paths of folders that contain the FileServer.config in them此变量包含一组文件夹的文件路径,其中包含 FileServer.config

 var foundFiles = Directory.EnumerateFiles(rootDirectory, "fileserver.config", 

    SearchOption.AllDirectories);
    var RepositoryName = Path.GetFileName(Path.GetDirectoryName(foundFiles));

Here string rootDirectory is a particular path这里的字符串 rootDirectory 是一个特定的路径

Any way I can achieve this ?我有什么办法可以做到这一点?

As per my current implementation I get the error "Cannot convert System.Collections.Generics.IENumerable to string " underlined Right under the foundFiles variable.根据我当前的实现,我收到错误“无法将 System.Collections.Generics.IENumerable 转换为字符串”,在 foundFiles 变量下有下划线。

My original answer我的原答案

Originally I thought that you only expected there to be a single file named fileserver.config and you wanted the path to that file - which can be done using FirstOrDefault() .最初我认为您只希望有一个名为fileserver.config并且您想要该文件的路径 - 这可以使用FirstOrDefault()来完成。

using System.Linq;

String firstFileMatch = Directory
    .EnumerableFiles( rootDirectory, "fileserver.config" )
    .FirstOrDefault();

if( firstFileMatch == null )
{
    throw new FileNotFoundException();
}

return Path.GetDirectoryName( Path.GetDirectoryName( fileFirstMatch ) );

New answer新答案

In a follow-up comment you said you wanted to print all of the files - so that's straightforward too, using String.Join :在后续评论中,您说您想打印所有文件 - 所以这也很简单,使用String.Join

using System.IO;
using System.Linq;

IEnumerable<String> fileNames = new DirectoryInfo( rootDirectoryPath )
    .EnumerableFiles( "fileServer.config", SearchOption.AllDirectories )
    .Select( fi => fi.Directory.FullName )
    .Select( dirPath => dirPath.Substring( rootDirectoryPath.Length ) );

String allNames = String.Join( "\r\n\r\n", fileNames );
MessageBox.Show( allNames );

暂无
暂无

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

相关问题 从&#39;string&#39;转换为&#39;System.Collections.Generic.IEnumerable <string> - Convert from 'string' to 'System.Collections.Generic.IEnumerable<string> 转换&#39;System.Collections.Generic.IEnumerable <string> &#39;至&#39;string [] - Convert 'System.Collections.Generic.IEnumerable<string>' to 'string[] 无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string> - Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string> 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> TreeView:无法从&#39;System.Collections.Generic.IEnumerable转换 <string> &#39;到&#39;System.Collections.Generic.IEnumerable - TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable 无法从“System.Collections.Generic.IEnumerable”转换<string> &#39; 到 &#39;T&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'T' 无法从&#39;System.Collections.Generic.IEnumerable转换 <char> &#39;到&#39;字符串&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string' 转换System.Collections.Generic.SortedList <string,T> 进入IEnumerable <T> - Convert System.Collections.Generic.SortedList<string,T> into IEnumerable<T> 无法隐式转换类型&#39;System.Collections.Generic.IEnumerable <string> &#39;到&#39;System.Collections.Generic.ICollection <x> “ - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.ICollection<x>' 在 Unity3d 中使用 RESTful web 服务,无法从 'System.Collections.IEnumerable' 转换为 'string'... 如何转换或工作 - Consuming RESTful web services in Unity3d, cannot convert from 'System.Collections.IEnumerable' to 'string'… how to convert or work around?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM