lundi 11 mai 2015

Reading .txt file using FlowDocumentReader - WPF

In my main window when I click on the menu item, File --> Open and select the .txt file, it should be displayed using a Flow Document Reader in a new window. I have included the code below, but it doesn't work.

private void file_open(object sender, System.Windows.RoutedEventArgs e)
    {

        // Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = ""; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "List Files (.txt)|*.txt"; // Filter files by extension    
dlg.InitialDirectory = @"D:\TextFiles\"; // Initial directory

// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process open file dialog box results 
if (result == true)
{
// Open document 
string filename = dlg.FileName;

// Open new window for displaying text file
var openwin = new Open_Window2();
openwin.Show();

// Read text file
string filename = dlg.FileName;

Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(System.IO.File.ReadAllText(filename));
FlowDocument document = new FlowDocument(paragraph);
FlowDocReader.Document = document; 

}

}

Aucun commentaire:

Enregistrer un commentaire