Windows8 文件操作

        //获取“库\文档”文件夹
        StorageFolder storageFolder = KnownFolders.DocumentsLibrary;

        //创建文件以及普通string写入读取文本
        private async void btnCreateFile_Click(object sender, RoutedEventArgs e)
        {
            StorageFile sf = await storageFolder.CreateFileAsync(this.tbFileName.Text.Trim(), CreationCollisionOption.ReplaceExisting);
            this.tb_show.Text = "提示-创建了文件";
            this.tb_content.Text = this.tbFileName.Text.Trim();
        }

        private async void btnWriteFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string writeStr = this.tbContent.Text.Trim();

                StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
                await FileIO.WriteTextAsync(sf, writeStr);

                this.tb_show.Text = "提示-文件写入成功,以Text方式写入内容";
                this.tb_content.Text = writeStr;
            }
            catch (Exception ex)
            {
                this.tb_show.Text = "提示-未找到该文件,请先创建文件";
            }
        }

        private async void btnReadFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
                string filecontent = await FileIO.ReadTextAsync(sf, UnicodeEncoding.Utf8);

                this.tb_show.Text = "提示-文件以Text方式读取成功,读取的内容";
                this.tb_content.Text = filecontent;
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示-未找到该文件,请先创建文件";
            }
        }

        //Buffer方式写入和读取
        private async void btnWriteBufferFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string writestr = this.tbContent.Text.Trim();

                StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
                IBuffer buffer = GetBufferFromString(writestr);
                await FileIO.WriteBufferAsync(sf, buffer);

                this.tb_show.Text = "提示-文件写入成功,以Buffer方式写入内容";
                this.tb_content.Text = writestr;
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示-未找到该文件,请先创建文件";
            }
        }

        private async void btnReadBufferFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
                IBuffer buffer = await FileIO.ReadBufferAsync(sf);
                string filecontent = GetStringFromBuffer(buffer);

                this.tb_show.Text = "提示-文件以Buffer方式读取成功,读取的内容";
                this.tb_content.Text = filecontent;
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示-未找到该文件,请先创建文件";
            }
        }

        //将String转为Buffer
        private IBuffer GetBufferFromString(String str)
        {
            using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
            {
                using (DataWriter dataWriter = new DataWriter(memoryStream))
                {
                    dataWriter.WriteString(str);
                    return dataWriter.DetachBuffer();
                }
            }
        }

        //将Buffer转为String
        private String GetStringFromBuffer(IBuffer buffer)
        {
            using (DataReader dataReader = DataReader.FromBuffer(buffer))
            {
                return dataReader.ReadString(buffer.Length);
            }
        }

        //Stream方式写入和读取
        private async void btnWriteStreamFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string writestr = this.tbContent.Text.Trim();

                StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
                using (IRandomAccessStream iRanAccessStream = await sf.OpenAsync(FileAccessMode.ReadWrite))
                {
                    IOutputStream outputStream = iRanAccessStream.GetOutputStreamAt(0);
                    using (DataWriter dataWriter = new DataWriter(outputStream))
                    {
                        dataWriter.WriteString(writestr);
                        await dataWriter.StoreAsync(); 
                        await dataWriter.FlushAsync();

                        this.tb_show.Text = "提示-文件写入成功,以Stream方式写入内容";
                        this.tb_content.Text = writestr;
                    }
                }
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示-未找到该文件,请先创建文件";
            }

        }

        private async void btnReadStreamFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
                using (IRandomAccessStream iRanAccessStream = await sf.OpenAsync(FileAccessMode.Read))
                {
                    IInputStream inputStream = iRanAccessStream.GetInputStreamAt(0);
                    using (DataReader dataReader = new DataReader(inputStream))
                    {
                        UInt64 size = iRanAccessStream.Size;
                        if (size <= UInt32.MaxValue)
                        {
                            UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)size);
                            string filecontent = dataReader.ReadString(numBytesLoaded);

                            this.tb_show.Text = "提示-文件以Stream方式读取成功,读取的内容";
                            this.tb_content.Text = filecontent;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示未找到该文件,请先创建文件";
            }
        }

 

posted on 2013-03-28 10:47  JackSlaterYu  阅读(257)  评论(0编辑  收藏  举报