locked
Read Embedded Resource file in Setup Project RRS feed

  • Question

  • I am trying to convert some code to C# from an example I found on MSDN:

     

     Walkthrough: Using a Custom Action to Create a Database During Installation

     

    Most of code is working except the reading of the embedded resource file that contains the SQL to fill the database. My C# implementation for that part currently is:

     

    Code Block

    private string GetSql(string Name)

    {

    string sql = "";

    try

    {

    Assembly Asm = Assembly.GetExecutingAssembly();

    Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

    StreamReader reader = new StreamReader(strm);

    sql = reader.ReadToEnd();

    }

    catch(Exception ex)

    {

    MessageBox.Show(ex.Message);

    MessageBox.Show(@"Error reading file containing the SQL for installing the database.");

    }

    return sql;

    }

     

     

    The exception always get raised. The code that calls the method:

     

    Code Block

    ExecuteSql(sqlServer,"TestDatabase",dbUser,dbPassword,GetSql("sql.txt"));

     

     

    sql.txt is part of the CustomAction class project and has it property set to an embedded resource.

     

    The exception returns:

     

    Value cannot be null.

    Parametername: stream

     

    I am not sure if I implemented the code based on the VB.NET examply correctly since the VB.NET example uses

     

    Code Block

    Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()

     

     

    What am I doing wrong here?

    Friday, November 23, 2007 1:43 PM

Answers

  • Hi Philip,

     

    I tested your sample codes and found it worked on my side. For your concern, I suggest you to try to attach "sql.txt" to your setup project and set it as an embedded resource. Then see if it works for you. 

    Further more, to resolve your issue, you can try to check if "Asm.GetName().Name + "." + Name" can get the embeded resource file correctly.

     

    Hope this helps,

    Regards,

    Monday, November 26, 2007 7:34 AM
  • I found it!

     

    First there was a space in the name of the output dll. I removed the space, but never changed the namespace in the code or the namespace in project properties. When I changed both to the output dll name (the name without the space) it worked.

     

    Thanks for your time!

     

    Monday, November 26, 2007 11:54 AM

All replies

  • Hi Philip,

     

    I tested your sample codes and found it worked on my side. For your concern, I suggest you to try to attach "sql.txt" to your setup project and set it as an embedded resource. Then see if it works for you. 

    Further more, to resolve your issue, you can try to check if "Asm.GetName().Name + "." + Name" can get the embeded resource file correctly.

     

    Hope this helps,

    Regards,

    Monday, November 26, 2007 7:34 AM
  • Hi,

     

    I am now debugging the class file and it seems that resolving the name is going ok. I made some screenshots from within VS.NET 2003, maybe those can help spot the problem.

     

    Command window - resolviong the embedded resource

     

    My method with debug info

     

    Solution explorer

     

    Embedded resource properties

     

    The file sql.txt was first over 60 MB, so I tried it with a new sql.txt with only one line of text in it, but the same problem.

    Monday, November 26, 2007 11:15 AM
  • I found it!

     

    First there was a space in the name of the output dll. I removed the space, but never changed the namespace in the code or the namespace in project properties. When I changed both to the output dll name (the name without the space) it worked.

     

    Thanks for your time!

     

    Monday, November 26, 2007 11:54 AM