存储少量单个数据,不用数据库和xml,用txt更加方便 

    private void btnCreate_Click(object sender, System.EventArgs e)
        {        
            
string path = Server.MapPath("~/rw.txt");

              //这里在打开的同时回去判断文件是否存在
            FileStream fs 
= new FileStream(path,FileMode.OpenOrCreate);    
            StreamWriter sw 
= new StreamWriter(fs);
            
string content = txtContent.Text;
            sw.WriteLine(content);
            sw.Close();
            txtContent.Text 
= "";
            BindRead();

        }
        
void BindRead()
        {
            
string path = Server.MapPath("~/rw.txt");
            FileStream fs 
= new FileStream(path,FileMode.Open);
            StreamReader sr 
= new StreamReader(fs);
            labMessage.Text 
= sr.ReadLine();
            sr.Close();
        }

如果用我们最常用的方法来判断文件是否存在,则需要考虑进程占用问题。

 

string path = Server.MapPath("sun.txt");        
if(!File.Exists(path))
{
    FileStream fs 
=  File.Create(p);//如果不存在则创建
    fs.Close();//线程占用 则关闭线程    **重点        
}
//System.Threading.Thread.Sleep(5000);
//让线程延时5秒执行  与这里无关 仅多了解一个方法
StreamWriter sw = new StreamWriter(p);
sw.WriteLine(
"sss");
sw.Close();

 

一开始我用到xml保持和更改值,但这样保持和读写操作都复杂

<?xml version="1.0" encoding="utf-8"?>
<products>
  
<product>
    
<type>女性险</type>
  
</product>
</products>

相关文章:

  • 2022-12-23
  • 2021-05-27
  • 2021-11-04
  • 2021-09-29
  • 2021-09-25
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
  • 2021-10-21
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案