Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi guys, I am trying to read a text file that I have included with the project folder itself in a separate folder. I am trying to read this text file and then add each line to a list, each line as a separate item in the list, then I am looking to bind it to a listbox and each listbox item (each line previously) would be a hyperlink in the listbox. It's been very frustrating since the app just freezes every single time as soon as the code starts executing at runtime. What could be the problem?

C#
public partial class Page2 : PhoneApplicationPage
   {
       public Page2()
       {
           InitializeComponent();

           // Will contain the names of malls added through a text file

           List<string> Mall_List = new List<string>();

           using(StreamReader reader = new StreamReader("/Mall_List/Mall_List.txt"))
           {
                   while(reader.Peek() >= 0)
                   {
                       Mall_List.Add(reader.ReadLine());
                   }

                   reader.Close();
           }

               Malllist.ItemsSource = Mall_List;
        }

   }



I have no idea at this point how to display the listbox items as hyperlinks so help is much appreciated, thanks!
Posted

1 solution

First of all, never do it in the constructor. Defer this part if initialization until the moment when the main event handling cycle of the application is already working, for example, when the main window is already shown. This way, the mechanism of catching of all of the exception in the main UI loop can be used.

If this file reading and processing procedure can also get some considerable time, do it in even more accurate way: do it in the separate thread.

This might be not the real reason of the hanging you observe. You need to execute your code under debugger to be sure.

[EDIT #1]

Also, there are no situations where a hard-coded path like your "/Mall_List/Mall_List.txt" could be useful, no matter if this is a relative or absolute path. Now your code depends on working directory, and this directory can be anywhere at all; it's defined by the user, how an application is started.

The file can be found relative to the executable directory, or "special folder" (System.Environment.GetFolderPath, http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[]), or calculated based on some configuration data — never hard-coded.

[EDIT #2: following up the discussion in the comments to this question]

I looks like you mix up working directory and executable directory. The working directory has nothing to do with the directory where your executable files are — it is defined be the user and can be anywhere. This is a parameter in a .LNK file; and if started directly, is still defined by the user. Most usually, this is the current working directory of some other program, like a file manager (Explorer or any other) at the moment when the user starts the application. So, it can be anywhere; and your program can change it. When you use a relative path, the actual path is found relative to the current working directory, which can be anything. This approach is often used in console applications, where a more experienced user directly controls the working directory and know where to provide and expect files.

The directory where your executable files are, in contrast, never changes unless you move those executable files and start the application again. Here is how to find it:
C#
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string executableDirectory = System.IO.Path.GetDirectoryName(location);


Please pay attention, that this is a reliable method; there are other methods which can give you confusing results in some more special cases, but this will work the same way in all cases. I explain it in some more detail here:
How to find my programs directory [ (executable directory) ].

And I advised in the working directory and "special folder" here:
How to find my programs directory [ (current directory and "special folder") ].

I think now you should have comprehensive information on the directory issues.

—SA
 
Share this answer
 
v5
Comments
AM117 1-Oct-12 12:18pm    
I took it out of the constructor and into the function that is executed when the button is pressed (there is a button on the main page which leads to page 2. Page 2 is supposed to show the list).

Still the same thing - freezes/hangs.
Sergey Alexandrovich Kryukov 1-Oct-12 12:27pm    
It was quite a good idea to try with the button click first; later you could change it.
If it hangs after the click -- please execute the handler's code under debugger, could you?
--SA
AM117 1-Oct-12 14:06pm    
Well I'll have to apologise because I'm a complete noob here lol. I've built a full game for Windows Phone 7 but never came across anything remotely close to file reading, so I haven't a clue about IsolatedStorage, even after reading about it in the links. The most I can understand is that direct access is impossible and IsolatedStorage may act as a temporary holder for whatever is to be loaded (to write or read).

And same thing with debugging pretty much. Never got the hang of it. If you can walk me through I can do it. Else if you'd like I can send the files over. The directory thing you wrote in the "edit" section of your original post left me confused because I'm used to the idea of packing everything needed within the directory of the entire program so the hard-coded file path is always going to refer to some file within that program's own directory, no matter which PC/user.
Sergey Alexandrovich Kryukov 1-Oct-12 14:22pm    
Oh, please, there is nothing to apologize about; in your search for some help, you act in a quite an appropriate way. Maybe the experience needed for a phone game is somewhat above your present experience, but you will gain it with time and learning.

Still, let's try to make one step at a time. First, why IsolatedStorage (pretty advanced concept, not often used), not just reading of a "regular" file?

And let me explain you mistake about the executable directory first. If you get it, it will be able to move a bit further...
--SA
AM117 1-Oct-12 14:28pm    
Thanks man.

I think you misread: I DID make a game for the phone, but since it was pure XNA/C# it had nothing to do with reading/writing (we didn't have enough time to take care of saving performance of the player). Thus I was apologising because I have made a game yet I'm stuck here with reading/writing lol.

It is a regular file. Before I learned about IsolatedStorage, I began with regular reading. Just a StreamReader doing the job. But whether IsolatedStorage or not, it hangs in the same way.

Yeah that would be a good start. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900