2016-12-29 67 views
1
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; 
      } 
     } 
    } 

我試圖顯示文本文件的內容到一個文本框,但要得到一個錯誤說閱讀:WPF C#:從文本文件

「未處理的異常類型的「系統.IO.DirectoryNotFoundException'發生在mscorlib.dll中

附加信息:無法找到部分路徑'C:\ Descriptions \ miss_peregrines_home_for_peculiar_children.txt'。「

對不起,如果我沒有包括你可能需要的一切,我會編輯帖子,如果是這樣的話。

+4

我認爲該文件實際上存在,對嗎? – dasblinkenlight

+0

找不到目錄。您的說明文件夾在您的exe文件夾中? – SAm

+1

聽起來很像「C:\ Descriptions \」不存在... – Mr47

回答

1

設置的文件在「說明」文件夾中的內容,並複製到輸出目錄屬性生成操作是否有更新的複製錯誤它應該工作:

除非輸出目錄中有一些文件,否則在輸出目錄中沒有創建文件夾。

+0

是的!謝謝,我一直在調整它幾個小時,甚至不知道你可以做到這一點。謝謝 – Jordan

0

您的路徑並不存在,因爲你不能逃脫你的\的

你希望用這樣的

movieName = @"Descriptions\miss_peregrines_home_for_peculiar_children.txt"; 

使用這種方式,你不需要逃避你\

movieName = "Descriptions\\miss_peregrines_home_for_peculiar_children.txt"; 

但看着道路,它不存在,除非是在你的應用程序文件夾,但即使如此,你應該做它用不同的方式

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"); 

我個人喜歡Path.Combine因爲你不必擔心轉義字符,它會創建路徑彌補你,讓你可以輕鬆地添加和操縱它

如果你的文件夾不在執行程序集的文件夾,你應該完全限定的路徑,然後你不會得到

+0

是「System.IO.Path」還是「System.Windows.Shapes.Path」?因爲它不像Path.Combine(說這是一個模糊的參考)。 – Jordan

+0

System.IO.Path是您需要的 –

+0

抱歉,我在文件頂部的using語句中使用了它,但是如果您想要,也可以在此限定它 –