/// <summary>存在チェックのみのメソッド</summary> /// <param name="assemblyString"> /// アセンブリ名("既定の名前空間"とは異なる) /// </param> /// <param name="loadfileName">[埋め込まれたリソース ファイル]の名前(名前空間付き)</param> /// <param name="throwException">存在しない場合例外をスローするかどうかを指定</param> /// <returns>存在する:true、存在しない:false</returns> /// <remarks>自由に利用できる。</remarks> public static bool Exists(string assemblyString, string loadfileName, bool throwException) { // エントリのアセンブリ Assembly thisAssembly = null; ManifestResourceInfo manifestResourceInfo = null; // 例外処理 try { thisAssembly = MyAssemblies.GetAssembly(assemblyString); manifestResourceInfo = thisAssembly.GetManifestResourceInfo(loadfileName); } catch (Exception) { // なにもしない。 } // 存在チェック if (manifestResourceInfo != null) { if (0 != (manifestResourceInfo.ResourceLocation & (ResourceLocation.ContainedInManifestFile | ResourceLocation.Embedded))) { // 存在する。 return(true); } } // 存在しない。 if (throwException) { throw new ArgumentException(String.Format( PublicExceptionMessage.RESOURCE_FILE_NOT_FOUND, loadfileName)); } else { return(false); } }
private static Assembly GetEntryAssembly(string assemblyString) { return(MyAssemblies.GetAssembly(assemblyString)); }
/// <summary>[埋め込まれたリソース ファイル]から文字列を読み込む。</summary> /// <param name="assemblyString"> /// アセンブリ名("既定の名前空間"とは異なる) /// </param> /// <param name="loadfileName">[埋め込まれたリソース ファイル]の名前(名前空間付き)</param> /// <param name="enc">エンコード</param> /// <returns>[埋め込まれたリソース ファイル]から読み込んだ文字列</returns> /// <remarks>自由に利用できる。</remarks> public static string LoadAsString(string assemblyString, string loadfileName, Encoding enc) { // エントリのアセンブリ Assembly thisAssembly = null; ManifestResourceInfo manifestResourceInfo = null; // ストリーム StreamReader sr = null; // 例外処理 try { thisAssembly = MyAssemblies.GetAssembly(assemblyString); manifestResourceInfo = thisAssembly.GetManifestResourceInfo(loadfileName); } catch (Exception) { // なにもしない。 } try { // 存在チェック if (manifestResourceInfo != null) { if (0 != (manifestResourceInfo.ResourceLocation & (ResourceLocation.ContainedInManifestFile | ResourceLocation.Embedded))) { // 存在する。 } else { // 存在しない。 throw new ArgumentException(String.Format( PublicExceptionMessage.RESOURCE_FILE_NOT_FOUND, loadfileName)); } } else { // 存在しない。 throw new ArgumentException(String.Format( PublicExceptionMessage.RESOURCE_FILE_NOT_FOUND, loadfileName)); } // 開く sr = new StreamReader(thisAssembly.GetManifestResourceStream(loadfileName), enc); // 読む return(sr.ReadToEnd()); } finally { // nullチェック if (sr == null) { // 何もしない。 } else { // 閉じる sr.Close(); } } }
/// <summary>[埋め込まれたリソースXMLファイル]から文字列を読み込む。</summary> /// <param name="assemblyString"> /// アセンブリ名("既定の名前空間"とは異なる) /// </param> /// <param name="loadfileName">[埋め込まれたリソースXMLファイル]の名前(名前空間付き)</param> /// <returns>[埋め込まれたリソースXMLファイル]から読み込んだ文字列</returns> /// <remarks>自由に利用できる。</remarks> public static string LoadXMLAsString(string assemblyString, string loadfileName) { // エントリのアセンブリ Assembly thisAssembly = null; ManifestResourceInfo manifestResourceInfo = null; // ストリーム StreamReader sr = null; // リーダ(XML) XmlTextReader xtr = null; // 例外処理 try { thisAssembly = MyAssemblies.GetAssembly(assemblyString); manifestResourceInfo = thisAssembly.GetManifestResourceInfo(loadfileName); } catch (Exception) { // なにもしない。 } try { // 存在チェック if (manifestResourceInfo != null) { if (0 != (manifestResourceInfo.ResourceLocation & (ResourceLocation.ContainedInManifestFile | ResourceLocation.Embedded))) { // 存在する。 } else { // 存在しない。 throw new ArgumentException(String.Format( PublicExceptionMessage.RESOURCE_FILE_NOT_FOUND, loadfileName)); } } else { // 存在しない。 throw new ArgumentException(String.Format( PublicExceptionMessage.RESOURCE_FILE_NOT_FOUND, loadfileName)); } // 既定のエンコーディングでロードして、 sr = new StreamReader(thisAssembly.GetManifestResourceStream(loadfileName)); // XML宣言を取得して、 // <?xml version="1.0" encoding="xxxx" ?> string xmlDeclaration = sr.ReadLine(); sr.Close(); // エンコーディング オブジェクトの取得 Encoding enc = XmlLib.GetEncodingFromXmlDeclaration(xmlDeclaration); // 指定のエンコーディングで再ロード sr = new StreamReader(thisAssembly.GetManifestResourceStream(loadfileName), enc); // 読む return(sr.ReadToEnd()); } finally { // nullチェック if (xtr == null) { // 何もしない。 } else { // 閉じる xtr.Close(); } // nullチェック if (sr == null) { // 何もしない。 } else { // 閉じる sr.Close(); } } }
/// <summary>[埋め込まれたリソース ファイル]からStreamを読込</summary> /// <param name="assemblyString"> /// アセンブリ名("既定の名前空間"とは異なる) /// </param> /// <param name="loadfileName">string</param> /// <returns>[埋め込まれたリソース ファイル]から読込んだStream</returns> /// <remarks>自由に利用できる。</remarks> public static Stream LoadAsStream(string assemblyString, string loadfileName) { Assembly assembly = MyAssemblies.GetAssembly(assemblyString); return(assembly.GetManifestResourceStream(loadfileName)); }