Resource Content

C

cql90

Hi All,

I have created a folder call Resource within my project, I have added a XML
file into the resource folder, and I set the BuildAction property of the XML
file to "Embedded Resource". How can I retrieve its content in the runtime?
Any help is deeply appreciated. Thanks you very much in advance.

Kate
 
I

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

Hi,

It will be inside the .exe ( or dll )

You can use this code to retrieve it:

IIRC the full name would be the namespace +. + the name of the file, not
100% sure now though.

m_bitmap = new
System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(
"TheNameOfTheImage.gif" ));
 
C

cql90

Thanks you very much for your responded, I am deeply appreciated. Your code
would work well for bitmap, but not for XML file. Take care and have a nice
day...

Kate,
 
J

John Murray

Actually, his call to GetManifestResourceStream returns what it says --
a stream .... so whether your are feed that stream to an XML Reader or a
Bitmap constructor should work equally well.
 
C

cql90

My project name is: Test. I did insert a folder call: "Resource" into my
project. I did insert an XML file into the resource folder, I set the
BuildAction of XML file to Embedded resource. After all, I do this:

try

{

System.Reflection.Assembly _Assembly =
System.Reflection.Assembly.GetExecutingAssembly();

StreamReader rd = new StreamReader( _Assembly .GetManifestResourceStream(
"Test.Resource.Configuration.xml" ) );

}

catch( Exception Err )

{

MessageBox.Show( Err.Message );

}

I am always get an error message said that: the "stream" cannot be null

Any ways, Thanks you so much for your help, John. I am very appreciated. I
did try the code but it doen't work. Take care and have a nice day.
 
I

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

Hi,

I have never put the resource file in a folder.
Do this:

string[] resources = GetType().Assembly.GetManifestResourceNames()


You can then iterate in all the resources and see how it's named

Please post back your findings
 
C

cql90

Hi Ignacio Machin,

The reason why I put the resource in the folder because it will be cleanner.
I have images in one folder, xml files in another folder, and picture in
other folder. Now, you know why I have to put my xml file in a folder. it
works if I don't put my xml file in folder and the code look like this:

// Get the data from XML file and input into the stream

System.IO.Stream _stream = _Assembly.GetManifestResourceStream(
"Test.Configuration.xml" );

// Create read stream object so we can read data out from the stream

StreamReader _streamReader = new StreamReader( _stream );

// Make file name in current working environment

string strFilename = AppDomain.CurrentDomain.BaseDirectory +
"Configuration.xml";

// Create a file to write data from the stream to the file

StreamWriter _streamWriter = new StreamWriter( strFilename );

// Read all the data from stream and write to the file

_streamWriter.Write( _streamReader.ReadToEnd() );

// Close when finish

_streamReader.Close();

_streamWriter.Close();

// Create temporary process

System.Diagnostics.Process _Process = new System.Diagnostics.Process();

// Open the notepad file, read data from XML file and then paste data into
the notepad

System.Diagnostics.ProcessStartInfo _pcInfo = new

System.Diagnostics.ProcessStartInfo( "Notepad.exe", strFilename );

_pcInfo.UseShellExecute = false;

_pcInfo.RedirectStandardOutput = true;

_Process.StartInfo = _pcInfo;

// Start Nodepad

_Process.Start();

// Pause right here until the notepad is close

_Process.WaitForExit();

// Release the temporary process

_Process.Close();

// Create new fileinfo object

FileInfo _finfos = new FileInfo( strFilename );

// Delete file

_finfos.Delete();


Thanks you so much for your help, Ignacio. I am greatly appreciated. Take
care and have a nice day...
 

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