UWPではStreamWriterが使えない。
UWPでは任意のフォルダにテキストを書き出すことができない。
----------------------------------
自PCの全デバイス名をテキストファイルに書き出すコード。
Buttonをひとつ配置
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください
namespace 練習20171130a24UWP_ファイル
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
string selector = "System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
DeviceInformationCollection collection = await DeviceInformation.FindAllAsync(selector);
// Create sample file; replace if exists.
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =
await storageFolder.CreateFileAsync("sample.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);
var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
using (var outputStream = stream.GetOutputStreamAt(0))
{
// We'll add more code here in the next step.
using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
{
foreach (DeviceInformation info in collection)
{
dataWriter.WriteString(string.Format("Name={0} IsEnabled={1} Id={2} \r\n", info.Name, info.IsEnabled, info.Id));
}
// dataWriter.WriteString("DataWriter has methods to write to various types, such as DataTimeOffset.");
await dataWriter.StoreAsync();
await outputStream.FlushAsync();
}
}
stream.Dispose(); // Or use the stream variable (see previous code snippet) with a using statement as well.
}
}
}
--------------------------------------------------------------------------------
msn
https://docs.microsoft.com/ja-jp/windows/uwp/files/quickstart-reading-and-writing-files#writing-to-a-file
のテキストファイルへの書き込みの解説通りにコードを組み立てた。
buttonを一つ配置
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください
namespace 練習20171130a24UWP_ファイル
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
// Create sample file; replace if exists.
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =
await storageFolder.CreateFileAsync("sample.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);
var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
using (var outputStream = stream.GetOutputStreamAt(0))
{
// We'll add more code here in the next step.
using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
{
dataWriter.WriteString("DataWriter has methods to write to various types, such as DataTimeOffset.");
await dataWriter.StoreAsync();
await outputStream.FlushAsync();
}
}
stream.Dispose(); // Or use the stream variable (see previous code snippet) with a using statement as well.
//書込み終了ダイアログ
await new MessageDialog("書込み終了", "インフォメーション").ShowAsync();
}
}
}
0 件のコメント:
コメントを投稿