C#文件操作和FTP操作

更新时间:2024-01-26 10:10:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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类之间关系十分类似。

上传文件源代码

publicstaticvoidonload(string file)

{

FtpWebRequest ftp;

FileInfo f = newFileInfo(file);

ftp = (FtpWebRequest)FtpWebRequest.Create(newUri(\ + f.Name));

ftp.Credentials = newNetworkCredential(\, \); ftp.KeepAlive = false;

ftp.Method = WebRequestMethods.Ftp.UploadFile;

ftp.UseBinary = true;

ftp.ContentLength = f.Length;

intbuffLength = 20480; //// byte[] buff = newbyte[buffLength]; intcontentLen; try

{

FileStreamfs = f.OpenRead();

Streamsw = 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()); } }

下载文件代码

publicvoid Download(stringftpServerIP, stringftpUserID, stringftpPassword, stringfileName,

string Destination)

{

FtpWebRequestreqFTP; try

{

FileStreamoutputStream = newFileStream(Destination + \ + fileName, FileMode.Create);

reqFTP = (FtpWebRequest)FtpWebRequest.Create(newUri(\ + ftpServerIP + \ +

fileName));

reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

reqFTP.UseBinary = true;

reqFTP.Credentials = newNetworkCredential(ftpUserID, ftpPassword FtpWebResponseftpresponse = (FtpWebResponse)reqFTP.GetResponse(); StreamftpStream = ftpresponse.GetResponseStream(); long cl = ftpresponse.ContentLength; intbufferSize = 2048; intreadCount;

byte[] buffer = newbyte[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()); } }

遍历文件夹中的文件

privatevoid button3_Click(object sender, EventArgs e)

{

DirectoryInfodir = newDirectoryInfo(textBox1.Text.ToString()); j = 0;

label1.Text = GetAllFiles(dir).ToString(); }

privateintGetAllFiles(DirectoryInfodir) {

//为 FileInfo 和 DirectoryInfo 对象提供基类,因此它可以强制转换为FileInfo对象和

DirectoryInfo对象

//GetFileSystemInfos()检索表示当前目录的文件和子目录的强类型 FileSystemInfo 对象的数组 FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); foreach (FileSystemInfo i infileinfo) {

if (i isDirectoryInfo) //判断如果检索到的是子目录,则继续向下检索,否则文件数加一。 {

GetAllFiles((DirectoryInfo)i); } else

{ j++;

} } return j; }

通过对话框选择文件或是文件夹

///

///通过对话框来选择一个或是多个文件 ///

///

privatevoidBtnSelectFile_Click(object sender, EventArgs e) {

//通过此类来选择一个文件,并获得文件的信息 OpenFileDialogfileDialog = newOpenFileDialog(); //设置是否能选项多个文件夹 fileDialog.Multiselect = true; //设置选择文件对话框的标题 fileDialog.Title = \请选择文件\; //设置选择文件的类型

//fileDialog.Filter = \ 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|所有文件(*.*)|*.*”。

///

///选择要进行转换的文件夹

///

///

privatevoidbutton_SelectFile_Click(object sender, EventArgs e) {

//通过此类来获得弹出一个文件夹,获得一个文件夹,并得到文件夹的信息 FolderBrowserDialog dialog = newFolderBrowserDialog(); //设置选择文件夹对话框的标题 dialog.Description = \请选择文件路径\; //判断文件夹是否选中

if (dialog.ShowDialog() == DialogResult.OK) {

//获得文件夹的完全路径

stringfoldPath = dialog.SelectedPath;

textBox1.Text = foldPath; } }

///

///

privatevoidbutton_SelectFile_Click(object sender, EventArgs e) {

//通过此类来获得弹出一个文件夹,获得一个文件夹,并得到文件夹的信息 FolderBrowserDialog dialog = newFolderBrowserDialog(); //设置选择文件夹对话框的标题 dialog.Description = \请选择文件路径\; //判断文件夹是否选中

if (dialog.ShowDialog() == DialogResult.OK) {

//获得文件夹的完全路径

stringfoldPath = dialog.SelectedPath;

textBox1.Text = foldPath; } }

本文来源:https://www.bwwdw.com/article/e7zw.html

Top