Thread: Getting an embedded resource

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129

    Getting an embedded resource

    I'm stuck on this one...

    I've figured out how to get an embedded picture file to be referenced (and that successfully shows a pic in a control, depending on another item being clicked). That works fine, got loads of pics sorted. What I can't figure out is how to read an embedded text file, process it, and throw the data in to an array.

    Code:
    public ResourceManager pics128Ships = new ResourceManager("POS_and_Fuel_Calculator.128shipPics", Assembly.GetExecutingAssembly());
            public ResourceManager pics128Pos = new ResourceManager("POS_and_Fuel_Calculator.128posPics", Assembly.GetExecutingAssembly());
            public ResourceManager pics256Pos = new ResourceManager("POS_and_Fuel_Calculator.256Pics", Assembly.GetExecutingAssembly());
            public ResourceManager DataPOSShips = new ResourceManager("POS_and_Fuel_Calculator.POSandShipsData", Assembly.GetExecutingAssembly());
    the first three are pictures, no problems with those, so I should expect to do the same with the last one, which calls the *.resx file that has the information about the text files.

    However, I'm not sure if I should be using the streamreader method to read through the text files, as building the program is fine, but running/debugging it produces -

    Code:
    System.ArgumentNullException was unhandled
      Message="Value cannot be null.\r\nParameter name: path"
      Source="mscorlib"
      ParamName="path"
      StackTrace:
           at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
           at System.IO.StreamReader..ctor(String path)
           at POS_and_Fuel_Calculator.Form1.Form1_Load(Object sender, EventArgs e) in K:\Programming Projects\POS and Fuel Calculator\POS and Fuel Calculator\Form1.cs:line 43
           at System.Windows.Forms.Form.OnLoad(EventArgs e)
           at System.Windows.Forms.Form.OnCreateControl()
           at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
           at System.Windows.Forms.Control.CreateControl()
           at System.Windows.Forms.Control.WmShowWindow(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WmShowWindow(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
           at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
           at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
           at System.Windows.Forms.Control.set_Visible(Boolean value)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at POS_and_Fuel_Calculator.Program.Main() in K:\Programming Projects\POS and Fuel Calculator\POS and Fuel Calculator\Program.cs:line 17
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
    The error originates from the first line here - (which is pretty much the first line of code in the whole program)

    Code:
    StreamReader sr = new StreamReader(DataPOSShips.GetString("Ships.db"));
                //StreamReader sr = new StreamReader(@"K:\Programming Projects\POS and Fuel Calculator\POS and Fuel Calculator\POS.db");  //the old way of calling a file (it worked...)
                StringBuilder sb = new StringBuilder("");
                int stringCounter = 0;
                while (fuelSize != -1)
                {
                    char char1 = (char)sr.Read();
                    string string1 = char1.ToString();
                    if (string1 == "#")
                    {
                        shipsList[stringCounter] += sb.ToString(0, sb.Length);
                        break;
                    }
                    if (string1 == "@")
                    {
                        shipsList[stringCounter] += sb.ToString(0, sb.Length);
                        sb.Remove(0, sb.Length);
                        stringCounter++;
                    }
                    else
                    {
                        sb.Append(string1);
                    }
                }
    What should I be doing to read in this damn text file??
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Code:
    StringReader sr = new StringReader(DataPOSShips.GetString("Ships"));
    That's how...
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 07:12 PM
  2. unmanaged resource
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2008, 04:23 AM
  3. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 01:07 AM
  4. resource problem/question
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 01-29-2003, 02:08 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM