C# Как из модуля вытащить встроенный текстовый ресурс Итак задача: в модуль мы вставили текстовый файл, как встроенный ресурс (Embedded resource). Как его прочесть. Нам поможет вот такая конструкция: Stream? filestream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{Path.GetFileNameWithoutExtension(modulename)}.{filename}"); filename нам известен, а как узнать имя модуля? Здесь поможет вот такая конструкция: var modulename = Assembly.GetExecutingAssembly().ManifestModule.Name; Беда только, что она вернет имя модуля с расширением: moudulename.dll, а нам нужен без расширения. Здесь нам на помощь приходит Path.GetFileNameWithoutExtension. ---- string filename = "work.txt"; var modulename = Assembly.GetExecutingAssembly().ManifestModule.Name; var mod = Path.GetFileNameWithoutExtension(modulename); Stream? filestream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{Path.GetFileNameWithoutExtension(modulename)}.{filename}"); ---- Или коротко: Stream? filestream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.Name)}.{filename}"); ---- Заметка для себя
1 год назад