zoukankan      html  css  js  c++  java
  • Metro Style App开发快速入门 之文件访问操作示例

    文件访问操作示例

    最近在学习Win 8 Metro Style App的一些示例,接下来我写一下这段时间的学习感想。Win 8的最新版本是Window Consumer Preview 版本。Metro Style App的Demo中用了大量的异步操作。(使用async 和await)。

    在Win8中,使用异步操作相当简单,与同步差不多,具体看运行的代码就知道了。

    运行示例的环境是: Window 8 Consumer Preview  + Visual studio 11 Beta. 下载地址。 Window 8的安装不在此处介绍。

    Metro style App 的Sample下载地址为。  下面是自己的操作后的一些感想:

    1 在指定文件夹中创建指定类型的文件

            async void CreateFileBtn_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                StorageFile sampleFile = await storageFolder.CreateFileAsync("Sample.txt", CreationCollisionOption.ReplaceExisting);
    
            }
    

     注意:

     A、将Docments Library勾上,这样才能访问C:\Users\userName\Documents文件夹。这里必须勾上,否则程序无法运行,没有访问权限。

    B、添加文件类型(本例为txt文本文件)。这里我们可以添加文件访问的类型。如果没有将无法访问相关的文件。

    2、写入数据。采用FileIO.WriteTextAsync方法。

            void WriteTextToTextFileBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile != null)
                {
                    FileIO.WriteTextAsync(sampleFile, "Hello Test! ");
                }
    
            }
    

      读取数据,采用FileIO.ReadTextAsync方法。

          async void ReadTextFromTextFileBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile != null)
                {
                    string msg = await FileIO.ReadTextAsync(sampleFile);
                }
            }
    

     3、 用Bytes向文本写入数据

            async void WriteTextUseByteBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile != null)
                {
                    string Data = "Hello, world!";
                    IBuffer buffer = GetBufferFromString(Data);
                    await FileIO.WriteBufferAsync(sampleFile, buffer);
                }
    
    
            }
    
            IBuffer GetBufferFromString(string str)
            {
    
                using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    using (DataWriter write = new DataWriter(stream))
                    {
                        write.WriteString(str);
                        return write.DetachBuffer();
                    }
                }
            }
    

     用Byte从文本中读出数据。

            async void ReadTesxUseByte_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile != null)
                {
                    IBuffer buffer = await FileIO.ReadBufferAsync(sampleFile);
                    using (DataReader read = DataReader.FromBuffer(buffer))
                    {
                        string fileContent =  read.ReadString(buffer.Length);
                    }
    
                }
            }
    

    4、 使用流向文件写入数据。

            async void WriteTextUseStreamBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile!=null)
                {
                    using (IRandomAccessStream writeStram = await sampleFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        using (DataWriter dataWrite = new DataWriter(writeStram))
                        {
                            dataWrite.WriteString("Write Stream Text!");
                            await dataWrite.StoreAsync();
                            await dataWrite.FlushAsync();
                        }
                    }
                }
            }
    

    使用流向文件read数据。

             async void ReadFromStreamBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile!=null)
                {
                    using (IRandomAccessStream readSteam = await sampleFile.OpenAsync(FileAccessMode.Read))
                    {
                        using (DataReader dataReader = new DataReader(readSteam))
                        {
                            uint numByteLoaded = await dataReader.LoadAsync((uint)readSteam.Size);
                            string fileContent = dataReader.ReadString(numByteLoaded);
                            
                        }
                    }
                }
            }
    

     5、  拷贝文件。采用CopyAsync方法。

            async void CopyFileBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile!=null)
                {
                    StorageFile sampleFileCopy = await sampleFile.CopyAsync(KnownFolders.DocumentsLibrary, "sample-copy.txt", NameCollisionOption.ReplaceExisting);
                }
            }
    

    6、 删除文件,采用DeleteAsync方法。

           async void DeleteFileBtn_Click(object sender, RoutedEventArgs e)
            {
                if (sampleFile!=null)
                {
                    await sampleFile.DeleteAsync();
                    sampleFile = null;
                }
            }
    

     总结:现在Windows 8 只是预览版,所以在发布之前可能会改变,但是我觉得基本思想应该是一样的。

  • 相关阅读:
    js之判断非空
    解决eclipse添加不了tomcat8的问题
    Java(eclipse)连接MySQL8.0以上版本数据库方式
    面试必备的:Redis和MongoDB的区别
    简述关系型数据库和非关系型数据库
    非关系型数据库MongoDB初探,以及和Redis的对比
    day53:django:URL别名/反向解析&URL分发&命名空间&ORM多表操作修改/查询
    day52:django:ORM单表/多表操作
    day51:django:dispatch&模板渲染&过滤器&标签&组件&静态文件配置
    day50:django:有名/无名分组&FBV/CBV
  • 原文地址:https://www.cnblogs.com/linlf03/p/2391987.html
Copyright © 2011-2022 走看看