Send As SMS

Monday, July 04, 2005

Custom files in Assembly [Coding]

Custom files in Assembly


Main dificulty with placing files in assembly and reading it is namespaces.
Path to your file consist from:
  • Default Namespace from project properties
  • Folder structure in project
  • filename itself

To divide folders and namespaces, '.' (dot) is used.

Warning: When you change default namespace in project properties, filename folder will be changed immediately in compilation

Full source code (reading text file from assembly) is here [5K].

Main lines are:

private const string RESOURCE_NAME = "Folder.resource.txt";

System.Type type = typeof(Class1);
Stream stream = type.Assembly.GetManifestResourceStream(type, RESOURCE_NAME);
if (stream == null)
throw new Exception("System could not load resource");
OR

stream = Assembly.GetAssembly(typeof(Class1)).GetManifestResourceStream(typeof(Class1).Namespace + "." + RESOURCE_NAME);

Applies to all dotnet languages: C#, VB.NET, C++.NET, J#

0 Comments:

Post a Comment

<< Home