| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 812 人关注过本帖
标题:[求助]FileStream这样用对不对啊
只看楼主 加入收藏
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
 问题点数:0 回复次数:9 
[求助]FileStream这样用对不对啊

想用FileStream对文件进行读写操作,不知道这样写对不对

图片附件: 游客没有浏览图片的权限,请 登录注册

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 0;
this.label1.Text = "文件名称";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(176, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(304, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 2;
this.button1.Text = "浏览";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 48);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(432, 184);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(392, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 4;
this.button2.Text = "修改";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 261);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog()==DialogResult.OK)
{
textBox1.Text = open.FileName;
using(FileStream fs1 = new FileStream (textBox1.Text,FileMode.OpenOrCreate,FileAccess.Read))
{//还有,这和File.OpenRead(filename)的对象有什么不同啊
byte[] content = new byte[fs1.Length];
while(fs1.Read(content,0,content.Length)>0)
{
textBox2.Text = Encoding.Default.GetString(content);
}
}
}
}

private void button2_Click(object sender, System.EventArgs e)
{
using(FileStream fs2 = new FileStream(textBox1.Text,FileMode.Create,FileAccess.ReadWrite,FileShare.None))
{
byte[] content =Encoding.Default.GetBytes(textBox2.Text);
fs2.Write(content,0,content.Length);
fs2.Flush();
MessageBox.Show("保存成功","保存");
}
}
}
}


[此贴子已经被作者于2006-9-8 9:35:38编辑过]

搜索更多相关主题的帖子: FileStream 
2006-09-08 09:27
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
还有用FileStream和StreamReader(Writer)对文件读写,修改有什么不同?

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-08 10:30
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
怎么叛变了?

win32汇编
病毒 加密
目前兴趣所在
2006-09-08 10:43
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
hehe,不行啊~!

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-08 10:56
xxxxx52
Rank: 4
等 级:贵宾
威 望:13
帖 子:689
专家分:0
注 册:2006-4-30
收藏
得分:0 
你也叛变吧~ 来这边 嘎嘎

好的资料下载网站http:///in.asp?id=xuelion2006 嘿嘿帮点一下拉~
2006-09-08 10:57
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
楼上的别顾着灌水啊,帮我看看啊

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-09-08 10:58
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
都一樣的拉

你查下msdn就知道有啥差別了
擴展的功能不同

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-09-08 11:02
unpowertome
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-6
收藏
得分:0 
SaveFileDialog sd=new SaveFileDialog();
sd.Filter="txt(*.txt)|*.txt|allfill(*.*)|*.*";
sd.RestoreDirectory=true;
if(sd.ShowDialog()==DialogResult.OK)
{
string flname=sd.FileName;
StreamWriter sw=new StreamWriter(flname);
sw.Write(this.textBox1.Text);
sw.Flush();
sw.Close();
}

2007-05-06 12:45
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 

首先,我觉得你这个using 用得,我比较菜,还没有这样用过,呵呵!

你的程序打开txt文本没有问题,打开Word和写字板是不行的,是乱码。

至于File.OpenRead(filename)和FileMode.OpenOrCreate的区别嘛,我暂时只知道一点,那就是后者可以输个文件名直接创建文件,而前者只能打开文件。


IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2007-05-08 10:17
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
收藏
得分:0 
File.OpenRead(filename)在文件不存在时好象会抛异常,FileMode.OpenOrCreate在文件不存在时会在指定目录创建一个

愛生活 && 愛編程
2007-05-08 21:00
快速回复:[求助]FileStream这样用对不对啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051327 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved