标题 | C#基于互联网开发公文传输之经验 |
范文 | 安国有 王淑妍 【摘? 要】基于VS2012编程工具,通过C#语言有效利用FtpWebRequest与DosFramer控件开发实用的互联网公文编写与传输工具。 【Abstract】Based on VS2012 programming tools, this paper makes effective use of FtpWebRequest and DosFramer control in C# language to develop practical internet document writing and transmission tools. 【关键词】C#;互联网开发;VS2012;公文编写与传输 【Keywords】C#; internet development; VS2012; document writing and transmission 【中图分类号】TP311? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?【文献标志码】A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?【文章编号】1673-1069(2020)09-0118-02 1 引言 随着信息技术不断发展与普及应用,单位信息技术管理人员的技术水平不断提升,其为单位量身开发应用软件之意愿越发强烈,结合多年的工作经验,现将“基于互联网络环境”,利用C#语言,有效利用FtpWebRequest与DosFramer控件,开发公文编写与传输的经验加以总结,供同行及程序开发爱好者借鉴。 2 开发与运行环境构建 ①FTP服务器环境:基于Windows 7.0,基于Internet信息服务(IIS)管理器,添加FTP站点,依据对话框,设置站点名称(MyFTP)、内容目录(D:\MyFile)、IP地址(127.0.0.1)等,其中SSL选择,选择“无”,“身份验证”选择“基本”,授权“允许访问”选择“指定用户”,并填写已建的用户(用户名:user001,密码:123),“权限”选择“读取”和“写入”。 ②FTP公文目录规划。FTP服务器内容目录(D:\MyFile)下建立Template和WandTDocument文件夹。 3 开发环境构建 第一,下载安装Microsoft Visual Studio 2012(以下简称VS2012)。 第二,下载DosFramer.ocx控件,如果控件为32位,拷贝到System32,否则拷贝到SysWOW64文件夹,并对其进行注册。 第三,利用Word 2010创建公文模板(MyTemplate.docx),存于FTP服务器的Template文件夹内。 4 主要功能方法的实现 ①利用VS2012创建WandTDocument项目,通过工具箱建选项卡,添加doframer.ocx。 ②在Form1窗体放置:两个button控件和一个DSO Framer Control Object控件,设置button1的Text为“公文编写”,button2的Text為“公文发送”,axFramerControl1的Name为MyEdit。 ③在WandTDocument项目的输出路径(bin\Debug\)下建立Document文件夹。 ④方法定义。在Form1空间引用处输入:using System.IO;using System.Net;定义窗体级对象与变量,如下: private FtpWebRequest MyFTP;private string LocalFileName; private string LocalPath=Application.StartupPath+ @"\Document\"; 最后定义如下方法,实现调用公文模板编写公文,并发送公文,具体如下: private void Connect(String path)//连接FTP服务器 { MyFTP=(FtpWebRequest)FtpWebRequest.Create(new Uri(path)); MyFTP.UseBinary=true; MyFTP.Credentials=new NetworkCredential("user001","123"); } public bool SendFile(string filename) { FileInfo fileIno=new FileInfo(filename); string url="ftp://127.0.0.1/WandTDocument/"+ fileIno.Name; ConFtp(url);MyFTP.KeepAlive=false; MyFTP.Method=WebRequestMethods.Ftp.UploadFile; MyFTP.ContentLength=fileIno.Length;int bufLng=2048; byte[] buf=new byte[bufLng];int contLng; FileStream fs=fileIno.OpenRead(); try { Stream strm=MyFTP.GetRequestStream(); contLng=fs.Read(buf,0,bufLng); while (contLng !=0) {strm.Write(buf,0,contLng);contLng=fs.Read(buf,0,bufLng);} strm.Close();fs.Close();return true; } catch (Exception ex) {return false;} } public bool WriteFile(string fileName) { try { string localpath=Application.StartupPath + @"\Document\"; string localFName=localpath + fileName; string url="ftp://127.0.0.1/Template/template.docx"; ConFtp(url); MyFTP.Credentials=new NetworkCredential("user001","123"); FtpWebResponse Rpe=(FtpWebResponse)MyFTP. GetResponse(); Stream ftpStm=Rpe.GetResponseStream(); long cl=Rpe.ContentLength;int buflng=2048;int readsl; byte[] buf=new byte[buflng];readsl=ftpStm.Read(buf,0,buflng); FileStream outStm=new FileStream(localFName,FileMode.Create); while (readsl>0) { outStm.Write(buf,0,readsl);readsl=ftpStm.Read(buf,0,buflng); } ftpStm.Close();outStm.Close();Rpe.Close();return true; } catch (Exception ex) {return false;} } private string GetFileName() { string filename=DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff");filename=filename.Replace("-","").Replace("/","").Replace(" ", "").Replace(":","")+".docx";return filename; } 5 基本功能調用 private void button1_Click(object sender, EventArgs e) { LocalFileName=GetFileName(); if (WriteFile(LocalFileName)) {MyEdit.Open(LocalPath +LocalFileName);} else{ MessageBox.Show("调取公文模板失败!");} } private void button2_Click(object sender, EventArgs e) { string localfile =LocalPath +LocalFileName; if(!SendFile(localfile)){MessageBox.Show("公文发送失败!");} } |
随便看 |
|
科学优质学术资源、百科知识分享平台,免费提供知识科普、生活经验分享、中外学术论文、各类范文、学术文献、教学资料、学术期刊、会议、报纸、杂志、工具书等各类资源检索、在线阅读和软件app下载服务。