ラベル

Server (108) work (77) Idea (68) Car (31) PC (29) DAW (28) other (19) MakingWEBsite (18) 趣味 (18) health (13) CentOS (11) drupal (11) (9) android (4) スマホ (4) communication (3) drupal7 (3) hint (3) meno (3) モバイル (3) 歯医者 (3) 第二種電気工事士 (3) 英語 (3) PC Server (2) drupal8 (2) ms access (2) uwp C# (2) めし (2) 整備 (2) 音楽 (2) MIDI (1) diy (1) 会計 (1) 動画再生 (1) 生活 (1) 郵便 (1) 食べ物 (1)

2017年12月1日金曜日

uwp ファイルの書き込み デバイス一覧をテキストファイルに書き込み

UWPでのテキストファイルの書き込み

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

        }
    }
}