Ref:http://www.c-sharpcorner.com/UploadFile/raj1979/236/
1. The OpenFileDialog can't be work in WPF application.
Ref: http://msdn.microsoft.com/zh-tw/library/system.windows.controls.openfiledialog(v=vs.95).aspx
It seems to work in silverlight application.
2. Get the OpenFileDialog from Microsoft.Win32 and the Openfile method has to be changed.
xaml parts was the same as msdn example while coding can be changed as follows
private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
{
// Create an instance of the open file dialog box.
// 3 Employ Win32 component //OpenFileDialog openFileDialog1 = new OpenFileDialog();
Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
// Open the selected file to read.
// 4 Change the OpenRead method
// System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
System.IO.Stream fileStream = openFileDialog1.OpenFile();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
// Read the first line from the file and write it the textbox.
tbResults.Text = reader.ReadLine();
}
fileStream.Close();
}
}
5.
沒有留言:
張貼留言