zoukankan      html  css  js  c++  java
  • Windows Phones 7 文件操作

    Windows Phones 文件操作,自己重新测试了一遍,通过,给大家参考使用。

         private const string foldername = "xu";
            private const string filename = "info.txt";
            private const string filepath = foldername +"/"+ filename;
            private const string settingname = "sname";
            /// <summary>
            /// 创建文件夹
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file=IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.CreateDirectory(foldername);
                }
            }
          //检查文件夹是否存在
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file=IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.DirectoryExists(foldername))
                    {
                        MessageBox.Show("存在"+foldername);
                    }
                    else
                    {
                        MessageBox.Show("不存在");
                    }
                }
            }
            //删除文件夹
            private void button3_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.DeleteDirectory(foldername);
                }
            }
    
            //创建文件
            private void button6_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                  IsolatedStorageFileStream sd= file.CreateFile(filepath);
            //一定要记得关闭 不然有bug
                  sd.Close();
                }
            }
            //判断文件是否存在
            private void button4_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.FileExists(filepath))
                    {
                        MessageBox.Show("存在"+filepath);
                    }
                    else
                    {
                        MessageBox.Show("不存在");
                    }
                }
            }
            //删除文件操作
            private void button5_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.DeleteFile(filepath);
                }
            }
    
            //在文件里增加内容
            private void button7_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file=IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fs=file.OpenFile(filepath,FileMode.OpenOrCreate,FileAccess.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.WriteLine("我爱你");
                        } 
                    }
                }
            }
            //读取文件内容
            private void button8_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fs = file.OpenFile(filepath, System.IO.FileMode.OpenOrCreate, FileAccess.Read))
                    {
                        using (StreamReader sr=new StreamReader(fs))
                        {
                            MessageBox.Show(sr.ReadToEnd());
                        }
                    }
                }
            }
            //保存信息配置信息
            private void button9_Click(object sender, RoutedEventArgs e)
            {
                IsolatedStorageSettings.ApplicationSettings[settingname] = "哈哈";
                IsolatedStorageSettings.ApplicationSettings.Save();
                MessageBox.Show("保存成功");
            }
            //读取程序配置信息
            private void button10_Click(object sender, RoutedEventArgs e)
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(settingname))
                {
                    MessageBox.Show(IsolatedStorageSettings.ApplicationSettings[settingname].ToString());
                }
            }
    
    作者:蘑菇先生 出处: http://mushroom.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载。未经作者同意下,必须在文章页面明显标出原文链接及作者,否则保留追究法律责任的权利。
    如果您认为这篇文章还不错或者有所收获,可以点击右下角的【推荐】按钮,因为你的支持是我继续写作,分享的最大动力!
  • 相关阅读:
    android开发布局三(微信布局)
    Android开发adb,SQLite数据库运用
    直线电机磁负荷、电负荷
    MIT公开课(一):电场和偶极子
    哈工大电气工程系硕士研究生入学复试——自动控制原理1、2章
    直线电机与旋转电机的区别
    Math类介绍
    Scala编辑器和IntelliJ IDEA开发环境配置
    减少cpu的方法
    AS内存清理,建议以及查找内存泄露的方法
  • 原文地址:https://www.cnblogs.com/mushroom/p/2353120.html
Copyright © 2011-2022 走看看