代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
C#文件读写IO操作
作者:
Dezai.CN
/ 发布于
2012/11/6
/
566
<div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">1.前台代码</span></div> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileTest.aspx.cs" Inherits="FileTest" %> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" style="color: rgb(51, 102, 153); text-decoration: initial;">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><html xmlns="<a href="http://www.w3.org/1999/xhtml" style="color: rgb(51, 102, 153); text-decoration: initial;">http://www.w3.org/1999/xhtml</a>" > <head runat="server"> <title>文件IO操作</title> </head> <body> <form id="form1" runat="server"> <asp:FileUpload ID="FuReport" runat="server" /> <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> <asp:Button ID="btnLoad" runat="server" OnClick="btnLoad_Click" Text="Load" /> <asp:Button ID="btnCheck" runat="server" OnClick="btnCheck_Click" Text="Check" /> </form> </body> </html> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">2.后台代码 <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">public partial class FileTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected void btnSave_Click(object sender, EventArgs e) { string path = "C://"; Save(FuReport, path); } <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> protected void btnLoad_Click(object sender, EventArgs e) { string path = "C://sql.txt"; <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> FileInfo Fi = new FileInfo(path); if (Fi.Exists) { StreamReader Sdr = new StreamReader(path, System.Text.Encoding.UTF8); string sLine = ""; ArrayList arr = new ArrayList(); while (sLine != null) { sLine = Sdr.ReadLine(); if (sLine != null) { arr.Add(sLine); <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> } } Sdr.Close(); foreach (string s in arr) { Response.Write(s + ""); } } else Response.Write("No file matched!!"); } <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> public bool Save(System.Web.UI.WebControls.FileUpload fuFile, string EndPath) { string FilePathName = ""; try { FilePathName = System.IO.Path.GetFileName(fuFile.FileName); fuFile.PostedFile.SaveAs(EndPath + "//" + FilePathName); } catch (Exception ex) { throw ex; } return true; } <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> public byte[] Load(string FilePath) { FileInfo fi = new FileInfo(FilePath); byte[] fileByte; try { FileStream fs = fi.OpenRead(); fileByte = new byte[fs.Length]; fs.Read(fileByte, 0, Convert.ToInt32(fs.Length)); } catch (Exception ex) { throw ex; } return fileByte; } protected void btnCheck_Click(object sender, EventArgs e) { string path = "C://sql.txt"; FileInfo Fi = new FileInfo(path); if (Fi.Exists) { StreamReader Sdr = new StreamReader(path, System.Text.Encoding.UTF8); string sLine = ""; string A = ""; string B = ""; <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> ArrayList arr = new ArrayList(); while (sLine != null) { sLine = Sdr.ReadLine(); if (sLine != null) { arr.Add(sLine); <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> A=A+sLine.Replace("A",""); B = B + sLine.Replace("B", ""); } <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> } Sdr.Close(); <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> WriteFile("C://A.txt", A); WriteFile("C://B.txt", B); <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> foreach (string s in arr) { Response.Write(s + ""); } } else Response.Write("No file matched!!"); } /// < ummary> /// 写文件 /// </summary> /// < aram name="Path">文件路径</param> /// < aram name="Strings">文件内容</param> public static void WriteFile(string Path, string Strings) { if (!System.IO.File.Exists(Path)) { System.IO.FileStream f = System.IO.File.Create(Path); f.Close(); } System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312")); f2.WriteLine(Strings); f2.Close(); f2.Dispose(); } }
试试其它关键字
读写IO操作
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3