C#三个平台上的文件选择方法

wpf:

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
       // Open document 
       string filename = dlg.FileName;
}    
FileStream aFile = new FileStream(filename, FileMode.Open);
            StreamReader sr = new StreamReader(aFile);

需要 using System.Windows.Documents; using System.IO;

windows app:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation
= PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(
".txt"); ss.file = await openPicker.PickSingleFileAsync();
string filename =ss.file.Path;
StorageFile file =ss.file;
var fl = await FileIO.ReadLinesAsync(file);

需要:

using Windows.Storage;
using Windows.Storage.Pickers;
using System.Collections;

Windows phone:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".txt");
openPicker.PickSingleFileAndContinue();
void MainPage_Activated(CoreApplicationView sender, Windows.ApplicationModel.Activation.IActivatedEventArgs args)
{
            FileOpenPickerContinuationEventArgs continueArgs = args as FileOpenPickerContinuationEventArgs;
            if (continueArgs != null)
            {
                ss.file = continueArgs.Files[0];
                if (ss.file != null)
                {
                    BlankPage1 pg1 = new BlankPage1();
                    ss.bl = true;
                    Frame.Navigate(typeof(BlankPage1));
                }
                else
                {

                }
            }
}
string filename = MainPage.ss.file.Path;
StorageFile file = MainPage.ss.file;
var fl = await FileIO.ReadLinesAsync(file);

需要:


using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.ApplicationModel.Core;
using Windows.ApplicationModel.Activation;

using System.Collections;

原文地址:https://www.cnblogs.com/wos1239/p/4527600.html