?
Solved

Problem opening multiple files using StreamReader

Posted on 2004-04-14
9
Medium Priority
?
971 Views
Last Modified: 2011-10-03
I'm trying to open several separate text files and populate some label controls with them.  My system works fine when I only try to do it for a single file, but when I try to do more than one file, I get the following error:

>Compiler Error Message: CS1023: Embedded statement cannot be a declaration or labeled statement
>
>Source Error:
>
>Line 48:       //lblStrVariable.Text = sOutput;
>Line 49:
>Line 50:       StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
>Line 51:       string sLine0="";
>Line 52:       ArrayList arrText0 = new ArrayList();      

My code is as follows:
--------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{


      StreamReader objReader = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window_left_combined.txt");
      string sLine="";
      ArrayList arrText = new ArrayList();      
      while (sLine != null)
                  {
                        sLine = objReader.ReadLine();
                        if (sLine != null)
                              arrText.Add(sLine);
                  }
                        objReader.Close();
      foreach (string sOutput in arrText)


      StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
      string sLine0="";
      ArrayList arrText0 = new ArrayList();      
      while (sLine0 != null)
                  {
                        sLine0 = objReader0.ReadLine();
                        if (sLine0 != null)
                              arrText0.Add(sLine0);
                  }
                        objReader0.Close();
-------------------------------------------------------------------

What am I supposed to do?

Thanks.

Leprechaun



 

0
Comment
Question by:Leprechaun
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10830331
The problem is this code...

foreach (string sOutput in arrText)

     StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");

You are opening the same file in a single line loop without closing it which would be a runtime problem.  The error is that you are declaring the StreamReader in a single line loop, which is not allowed.
 
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10830866
Replace your code with this (and check the logic there, I'm not sure if you wanted to do this)

StreamReader objReader = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window_left_combined.txt");
string sLine="";
ArrayList arrText = new ArrayList();    
while (sLine != null)
{
      sLine = objReader.ReadLine();
      if (sLine != null)
            arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
{
      StreamReader objReader0 = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
      string sLine0="";
      ArrayList arrText0 = new ArrayList();    
      while (sLine0 != null)
      {
            sLine0 = objReader0.ReadLine();
            if (sLine0 != null)
                  arrText0.Add(sLine0);
      }
      objReader0.Close();
}
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10833161


does this really work?  with the slashes pointing the wrong way??
"f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt"


I would have assumed you'd get an error, and your code shoul look more like:

@"f:\inetpub\wwwroot\islam-democracy\content\window0_content.txt"


Also, I don't think that you can create a new variable in a loop like that

  foreach (string sOutput in arrText)
     StreamReader objReader0 = new StreamReader(...);

I think you'll need to do something like this:


  StreamReader objReader0;
  foreach (string sOutput in arrText)
      objReader0 = new StreamReader(...);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:dfiala13
ID: 10833551
>>does this really work?  with the slashes pointing the wrong way??
>>"f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt"
Sure, those are valid directory delimiters and one way around escaping or using @.

the problem is the declaration in the single-line loop
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10833670
TAD, if you try what you type:

  StreamReader objReader0;
  foreach (string sOutput in arrText)
      objReader0 = new StreamReader(...);

you will just create n StreamReader() assigned to objReader0 and will use the last created one.. I don't think he wants to do that..
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 1000 total points
ID: 10835786


If he doesn't want to re-declare the same object, but instead create many, many objects with a similar name then he needs to create an array of objects


  StreamReader[] objReader0 = new StreamReader[100] ;
  int i = 0;
  foreach (string sOutput in arrText)
      objReader0[i++] = new StreamReader(...);
   
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10836275
Yeah, that may be the point
0
 

Author Comment

by:Leprechaun
ID: 10837452
Thanks, folks.  Will test suggestions later today.

Leprechaun
0
 

Author Comment

by:Leprechaun
ID: 10856388
The array solved the problem.  

I also removed the foreach logic by using ReadToEnd, which simplified the code.  

The code is now:

> StreamReader[] objReaderArray = new StreamReader[5] ;
>      
> objReaderArray[0] = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window0_content.txt");
> string sLeftTop = objReaderArray[0].ReadToEnd();
>
> objReaderArray[1] = new StreamReader("f:/inetpub/wwwroot/islam-democracy/content/window1_content.txt");
> string sRightTop = objReaderArray[1].ReadToEnd();

Thanks.

Leprechaun

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question