Jordan
Jordan

Reputation: 25

WPF c#: Reading from a text file

using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

   namespace Lumiplex_Cinemas
   {
       public partial class Booking : UserControl, ISwitchable
       {
           string movieName;

            public Booking()
            {
                    InitializeComponent();

                    movieName = "Descriptions/miss_peregrines_home_for_peculiar_children.txt";
                    descriptionTextBox.Text = ReadingDesciptions(movieName);
            }

            public string ReadingDesciptions(string movieName)
            {

                //Tried both of these, still the same error
                string description = System.IO.File.ReadAllText(movieName);
                string description = File.ReadAllText(movieName);

                return description;
            }
        }   
   }

I'm trying to display the contents of the text file into a text box, but keep getting an error saying:

"An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

Additional information: Could not find a part of the path 'C:\Descriptions\miss_peregrines_home_for_peculiar_children.txt'."

Sorry if I've not included everything you might need, I'll edit the post if so.

Upvotes: 1

Views: 1357

Answers (2)

mm8
mm8

Reputation: 169420

Set the Build Action of the file in the "Descriptions" folder to Content and the Copy to Output Directory property to Copy if newer and it should work:

There is no folder created in the output directory unless there are some files in it.

Upvotes: 1

Simon Price
Simon Price

Reputation: 3271

your path doesn't exist because your not escaping your \'s

you want to either use something like this

movieName = @"Descriptions\miss_peregrines_home_for_peculiar_children.txt";

using this way you don't need to escape your \

movieName = "Descriptions\\miss_peregrines_home_for_peculiar_children.txt";

but looking at the path, it doesn't exist unless that's in your application folder but even then you should be doing it a different way

var exPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//exPath is the path of the exe \ dll that is being run which this is what it looks like from your code example
var filePath = Path.Combine(exPath,"Descriptions", "miss_peregrines_home_for_peculiar_children.txt" );

personally I like Path.Combine because you don't have to worry about escaping characters and it will build the path up for you so you can easily add and manipulate it

If your folder isn't in the executing assembly folder you should fully qualify the path and then you wont get the error

Upvotes: 0

Related Questions