1、C#文件操作和FTP操作FileInfo类FileInfo实例封装了计算机文件系统中某个目录中的一个文件,它包含操作文件及获取该文件详细信息的无数方法。当创建一个新的FileInfo对象时,必须给FileInfo构造函数传递一个表示文件名的字符串来关联此对象与文件名对应的文件。此文件名既可以是一个已有的文件,也可以使一个将要创建的不存在文件。File类File类支持对文件的基本操作,包括提供用于创建,复制,删除,移动和打开文件的静态方法,并协助创建FileStream对象。FileInfo类和File类之间去多方法调用都是相同的,但是FileInfo类没有静态方法,仅可以用于实例化的对象。Fi
2、leStream类FileStream用于读写二进制的文件。它是一个常用且功能强大的二进制流类,可用于读写单个字节和块字节。因为字符只是编码的字节,所以FileStream也能用于流字符。我们可以使用FileStream自身的构造函数来创建一个新的FileStream对象,或者使用FileInfo的某个方法来完成。Directory类Directory类用于复制,移动,重命名,创建,删除目录等典型操作。DirectoryInfo类DirectoryInfo类和Directory类之间的关系与FileInfo类和File类之间关系十分类似。上传文件源代码public static void on
3、load(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.Conten
4、tLength = f.Length; int buffLength = 20480; / byte buff = new bytebuffLength; 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
5、); 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(Des
6、tination + + 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
7、 = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = ftpresponse.GetResponseStream(); long cl = ftpresponse.ContentLength; int bufferSize = 2048; int readCount; byte buffer = new bytebufferSize; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount 0) outputStream.Write(buffer,
8、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 Directory
9、Info(textBox1.Text.ToString(); j = 0; label1.Text = GetAllFiles(dir).ToString(); private int GetAllFiles(DirectoryInfo dir) /为 FileInfo 和 DirectoryInfo 对象提供基类,因此它可以强制转换为FileInfo对象和DirectoryInfo对象 /GetFileSystemInfos()检索表示当前目录的文件和子目录的强类型 FileSystemInfo 对象的数组 FileSystemInfo fileinfo = dir.GetFileSyste
10、mInfos(); foreach (FileSystemInfo i in fileinfo) if (i is DirectoryInfo) /判断如果检索到的是子目录,则继续向下检索,否则文件数加一。 GetAllFiles(DirectoryInfo)i); else j+; return j; 通过对话框选择文件或是文件夹/ / 通过对话框来选择一个或是多个文件 / / / private void BtnSelectFile_Click(object sender, EventArgs e) /通过此类来选择一个文件, 并获得文件的信息 OpenFileDialog fileDia
11、log = new OpenFileDialog(); /设置是否能选项多个文件夹 fileDialog.Multiselect = true; /设置选择文件对话框的标题 fileDialog.Title = 请选择文件; /设置选择文件的类型 /fileDialog.Filter = Text files (*.txt)|*.txt|All files (*.*)|*.*; fileDialog.Filter = 文本文件 |*.txt;*.docx;*.pptx|All files (*.*)|*.*; /判断是否选择文件,如果选择,则继续进行操作 if (fileDialog.Show
12、Dialog()=DialogResult.OK) /获取文件夹包括路径的全部名称 string file = fileDialog.FileName; textBox2.Text = file; Filter属性:用来获取或设置当前文件名筛选器字符串,该字符串决定对话框的【另存为文件类型】或【文件类型】框中出现的选择内容。对于每个筛选选项,筛选器字符串都包含筛选器说明、垂直线条(|)和筛选器模式。不同筛选选项的字符串由垂直线条隔开,例如: “文本文件(*.txt)|*.txt|所有文件(*.*)|*.*” 。还可以通过用分号来分隔各种文件类型,可以将多个筛选器模式添加到筛选器中,例如: “图
13、像文件(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG; *.GIF|所有文件(*.*)|*.*” 。/ / 选择要进行转换的文件夹 / / / 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;