在c#中,如何从资源文件夹中获取以前缀开头的文件名

在c#中,如何从资源文件夹中获取以前缀开头的文件名,c#,resources,C#,Resources,如何基于前缀访问文本文件 var str = GrvGeneral.Properties.Resources.ResourceManager.GetString(configFile + "_Nlog_Config"); var str1 = GrvGeneral.Properties.Resources.ResourceManager.GetObject(configFile + "_Nlog_Config"); 其中configfile是资源文件A和B的前缀 根据configfile内容

如何基于前缀访问文本文件

var str = GrvGeneral.Properties.Resources.ResourceManager.GetString(configFile + "_Nlog_Config");
var str1  = GrvGeneral.Properties.Resources.ResourceManager.GetObject(configFile + "_Nlog_Config");
其中configfile是资源文件A和B的前缀


根据configfile内容(前缀),必须访问资源文件A和B

使用
DirectoryInfo
类()。然后可以使用搜索模式调用
GetFiles

string searchPattern = "abc*.*";  // This would be for you to construct your prefix

DirectoryInfo di = new DirectoryInfo(@"C:\Path\To\Your\Dir");
FileInfo[] files = di.GetFiles(searchPattern);

编辑:如果您有一种方法来构造您要查找的实际文件名,您可以直接转到,否则您将不得不在我前面的示例中遍历匹配的文件。

您的问题相当模糊……但听起来您想获取嵌入资源的文本内容。通常,您可以使用
Assembly.GetManifestResourceStream
来完成此操作。您始终可以将LINQ与
Assembly.GetManifestResourceNames()
一起使用,以查找与模式匹配的嵌入式文件的名称

ResourceManager
类更常用于自动检索本地化字符串资源,例如不同语言的标签和错误消息

更新:一个更普遍的例子:

internal static class RsrcUtil {
    private static Assembly _thisAssembly;
    private static Assembly thisAssembly {
        get {
            if (_thisAssembly == null) { _thisAssembly = typeof(RsrcUtil).Assembly; }
            return _thisAssembly;
        }
    }

    internal static string GetNlogConfig(string prefix) {
        return GetResourceText(@"Some\Folder\" + prefix + ".nlog.config");
    }

    internal static string FindResource(string pattern) {
        return thisAssembly.GetManifestResourceNames()
               .FirstOrDefault(x => Regex.IsMatch(x, pattern));
    }

    internal static string GetResourceText(string resourceName) {
        string result = string.Empty;
        if (thisAssembly.GetManifestResourceInfo(resourceName) != null) {
            using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName)) {
                result = new StreamReader(stream).ReadToEnd();
            }
        }
        return result;
    }
}
使用以下示例:

string aconfig = RsrcUtil.GetNlogConfig("a");
string bconfigname = RsrcUtil.FindResource(@"b\.\w+\.config$");
string bconfig = RsrcUtil.GetResourceText(bconfigname);

我已经添加了项目中的资源,项目中的资源。所以有一个.nlog.config文件和一个b.config.file文件。我有一个方法,通过一个方法传递的前缀来访问它们。所以我想访问这些配置文件中的文本。所以我不能理解上面的代码对我有什么帮助?我想我真的不理解你原来的问题,因为我不理解你刚才问我的问题。您是否询问如何根据配置文件中的设置引用资源文件?否,我想访问资源文件并将其文本内容分配给var。您的代码有什么问题?它根据给定的名称加载资源。