How can I save a temporary disk file from an embedded string resource

Platform: WinForms| Category: Resources
//usage:   string s = CopyResourceToTempFile(GetType(), 'showcase.txt');
//where showcase.txt is an embedded resource

static string CopyResourceToTempFile(Type type, string name)
{
	string temp = '';
	string nsdot = type.Namespace;
	if (nsdot == null)
		nsdot = '';
	else if (nsdot != '')
		nsdot += '.';
	Stream stream = type.Module.Assembly.GetManifestResourceStream(nsdot + name);
	if (stream != null)
	{
		StreamReader sr = new StreamReader(stream);
		temp = Path.GetTempFileName();
		StreamWriter sw = new StreamWriter(temp, false);
		sw.Write(sr.ReadToEnd());
		sw.Flush();
		sw.Close();
	}
	return temp;
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.