how to load xml files stored in the resources of a project

R

rizwanahmed24

Hi

i have created a project that contains some class files and xml files
stored in resources. i have created dll of this project and am using
it in another project.

In other project i want to read the xml stored in the resources of
loaded project. Please tell me how can i extract xml file from the
resources of the loaded project (in dll).

Regards
Rizwan
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Hi

i have created a project that contains some class files and xml files
stored in resources. i have created dll of this project and am using
it in another project.

In other project i want to read the xml stored in the resources of
loaded project. Please tell me how can i extract xml file from the
resources of the loaded project (in dll).

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(­)
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream­(
currentResource);


byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();


System.IO.FileStream destStream = new System.IO.FileStream ( destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();


return destFile;
}


throw new Exception("Resource not found : " + resourceName);


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top