收藏 分销(赏)

C#文件操作及FTP操作.docx

上传人:仙人****88 文档编号:7226804 上传时间:2024-12-28 格式:DOCX 页数:7 大小:29.28KB 下载积分:10 金币
下载 相关 举报
C#文件操作及FTP操作.docx_第1页
第1页 / 共7页
C#文件操作及FTP操作.docx_第2页
第2页 / 共7页


点击查看更多>>
资源描述
C#文件操作和FTP操作 FileInfo类 FileInfo实例封装了计算机文件系统中某个目录中的一个文件,它包含操作文件及获取该文件详细信息的无数方法。当创建一个新的FileInfo对象时,必须给FileInfo构造函数传递一个表示文件名的字符串来关联此对象与文件名对应的文件。此文件名既可以是一个已有的文件,也可以使一个将要创建的不存在文件。 File类 File类支持对文件的基本操作,包括提供用于创建,复制,删除,移动和打开文件的静态方法,并协助创建FileStream对象。FileInfo类和File类之间去多方法调用都是相同的,但是FileInfo类没有静态方法,仅可以用于实例化的对象。 FileStream类 FileStream用于读写二进制的文件。它是一个常用且功能强大的二进制流类,可用于读写单个字节和块字节。因为字符只是编码的字节,所以FileStream也能用于流字符。我们可以使用FileStream自身的构造函数来创建一个新的FileStream对象,或者使用FileInfo的某个方法来完成。 Directory类 Directory类用于复制,移动,重命名,创建,删除目录等典型操作。 DirectoryInfo类 DirectoryInfo类和Directory类之间的关系与FileInfo类和File类之间关系十分类似。 上传文件源代码 public static void onload(string file) { FtpWebRequest ftp; FileInfo f = new FileInfo(file); ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://192.168.1.120/" + f.Name)); ftp.Credentials = new NetworkCredential("yts", "yts"); ftp.KeepAlive = false; ftp.Method = WebRequestMethods.Ftp.UploadFile; ftp.UseBinary = true; ftp.ContentLength = f.Length; int buffLength = 20480; //// byte[] buff = new byte[buffLength]; int contentLen; try { FileStream fs = f.OpenRead(); Stream sw = ftp.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { sw.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } sw.Close(); fs.Close(); MessageBox.Show("上传成功"); } catch (Exception e) { MessageBox.Show(e.Message.ToString()); } } 下载文件代码 public void Download(string ftpServerIP, string ftpUserID, string ftpPassword, string fileName, string Destination) { FtpWebRequest reqFTP; try { FileStream outputStream = new FileStream(Destination + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword FtpWebResponse ftpresponse = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = ftpresponse.GetResponseStream(); long cl = ftpresponse.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); ftpresponse.Close(); MessageBox.Show("下载成功"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } 遍历文件夹中的文件 private void button3_Click(object sender, EventArgs e) { DirectoryInfo dir = new DirectoryInfo(textBox1.Text.ToString()); j = 0; label1.Text = GetAllFiles(dir).ToString(); } private int GetAllFiles(DirectoryInfo dir) { //为 FileInfo 和 DirectoryInfo 对象提供基类,因此它可以强制转换为FileInfo对象和DirectoryInfo对象 //GetFileSystemInfos()检索表示当前目录的文件和子目录的强类型 FileSystemInfo 对象的数组 FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断如果检索到的是子目录,则继续向下检索,否则文件数加一。 { GetAllFiles((DirectoryInfo)i); } else { j++; } } return j; } 通过对话框选择文件或是文件夹 /// <summary> /// 通过对话框来选择一个或是多个文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSelectFile_Click(object sender, EventArgs e) { //通过此类来选择一个文件, 并获得文件的信息 OpenFileDialog fileDialog = new OpenFileDialog(); //设置是否能选项多个文件夹 fileDialog.Multiselect = true; //设置选择文件对话框的标题 fileDialog.Title = "请选择文件"; //设置选择文件的类型 //fileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; fileDialog.Filter = "文本文件 |*.txt;*.docx;*.pptx|All files (*.*)|*.*"; //判断是否选择文件,如果选择,则继续进行操作 if (fileDialog.ShowDialog()==DialogResult.OK) { //获取文件夹包括路径的全部名称 string file = fileDialog.FileName; textBox2.Text = file; } } Filter属性:用来获取或设置当前文件名筛选器字符串,该字符串决定对话框的【另存为文件类型】或【文件类型】框中出现的选择内容。对于每个筛选选项,筛选器字符串都包含筛选器说明、垂直线条(|)和筛选器模式。不同筛选选项的字符串由垂直线条隔开,例如: “文本文件(*.txt)|*.txt|所有文件(*.*)|*.*” 。还可以通过用分号来分隔各种文件类型,可以将多个筛选器模式添加到筛选器中,例如: “图像文件(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG; *.GIF|所有文件(*.*)|*.*” 。 /// <summary> /// 选择要进行转换的文件夹 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_SelectFile_Click(object sender, EventArgs e) { //通过此类来获得弹出一个文件夹,获得一个文件夹,并得到文件夹的信息 FolderBrowserDialog dialog = new FolderBrowserDialog(); //设置选择文件夹对话框的标题 dialog.Description = "请选择文件路径"; //判断文件夹是否选中 if (dialog.ShowDialog() == DialogResult.OK) { //获得文件夹的完全路径 string foldPath = dialog.SelectedPath; textBox1.Text = foldPath; } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服