CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

菜鸟一问?

楼主myhouwei(myhouwei)2004-04-04 18:43:30 在 .NET技术 / C# 提问

我想在关闭对话筐的时候将LISTVIEW中的内容保存下来,DEBUG能通过但就是不能建立文件,0字节的文件都不能建立,为什么啊?  
  private   void   Form1_Closing(objectsender,System.ComponentModel.CancelEventArgs   e)  
  {  
        StreamWriter   sw=null;  
  try  
  {           sw   =   File.CreateText("save.txt");  
              ListView.ListViewItemCollection   items=listView1.Items;  
            foreach   (   ListViewItem   item   in   items   )  
          {          
              String   string1,string2;  
              string1=item.SubItems[0].Text;  
              string2=item.SubItems[1].Text;  
              sw.WriteLine(string1);  
              sw.WriteLine(string2);  
          }  
  }  
  catch(Exception   excep)  
  {  
        MessageBox.Show   (excep.Message,"error",MessageBoxButtons.OK,MessageBoxIcon.Error);  
  }  
  finally  
  {  
        if(sw!=null)  
      {     sw.Flush();  
          sw.Close();  
      }  
         
  }  
  }  
  问题点数:0、回复次数:2Top

1 楼greatsft(C的使者)回复于 2004-04-04 19:19:26 得分 0

在我机器上跑很正常,可能你的subitmes越界了,你设断点,跟踪下,自己找下原因,就知道了Top

2 楼myhouwei(myhouwei)回复于 2004-04-04 22:59:00 得分 0

单独测试可以,但item.SubItems[0].Text是从一个TEXTBOX读入的;item.SubItems[1].Text放的是路径,是   用openFileDialog的FileName属性得到的.但就是不能建立文件,搞不懂?  
   
  //将textBox的内容和openFileDialog返回的路径添加到LISTVIEW中  
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
        if(textBox1.Text=="")  
            MessageBox.Show("请输入游戏名字!","Worning!");  
      else  
        {  
            if(openFileDialog1.ShowDialog()==DialogResult.OK)  
                {  
  int   itemsNumber=listView1.Items.Count;  
  String   name=textBox1.Text;  
                    String   path=openFileDialog1.FileName;  
  String[]   subItems=new   String[]  
  {  
      name,  
      path  
  };  
  listView1.Items.Insert(itemsNumber,new   ListViewItem(subItems));  
   
   
                }  
        }  
  }  
   
  //关闭窗口并保存LISTVIEW的内容到文件  
  private   void   Form1_Closing(object   sender,   System.ComponentModel.CancelEventArgs   e)  
  {  
        StreamWriter   sw=null;  
         
  try  
  {           sw   =   File.CreateText("save.txt"); ListView.ListViewItemCollection   items=listView1.Items;  
  foreach   (   ListViewItem   item   in   items   )  
  {         String   string1,string2;  
            string1=item.SubItems[0].Text;  
            string2=item.SubItems[1].Text;  
            sw.WriteLine(string1);  
            sw.WriteLine(string2);  
  }  
  }  
  catch(Exception   excep)  
  {  
  MessageBox.Show(excep.Message,"error",MessageBoxButtons.OK,MessageBoxIcon.Error);  
  }  
  finally  
  {  
      if(sw!=null)  
      {  
            sw.Flush();  
            sw.Close();  
        }  
  }  
  }Top

相关问题

  • 菜鸟一问!!!
  • 菜鸟一问!
  • 菜鸟一问
  • 菜鸟一问?
  • 菜鸟一问!
  • 菜鸟一问
  • 菜鸟一问!!!!!!!!!!!!!!!!!!!
  • API菜鸟一问~~~
  • php菜鸟一问!!!
  • 菜鸟一问,急!!!

关键词

  • 文件
  • 内容
  • null
  • listview
  • sw
  • subitems
  • openfiledialog
  • items
  • textbox
  • 不能建立

得分解答快速导航

  • 帖主:myhouwei

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo