安國有 王淑妍
【摘? 要】基于VS2012編程工具,通過C#語言有效利用FtpWebRequest與DosFramer控件開發(fā)實(shí)用的互聯(lián)網(wǎng)公文編寫與傳輸工具。
【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.
【關(guān)鍵詞】C#;互聯(lián)網(wǎng)開發(fā);VS2012;公文編寫與傳輸
【Keywords】C#; internet development; VS2012; document writing and transmission
【中圖分類號】TP311? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?【文獻(xiàn)標(biāo)志碼】A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?【文章編號】1673-1069(2020)09-0118-02
1 引言
隨著信息技術(shù)不斷發(fā)展與普及應(yīng)用,單位信息技術(shù)管理人員的技術(shù)水平不斷提升,其為單位量身開發(fā)應(yīng)用軟件之意愿越發(fā)強(qiáng)烈,結(jié)合多年的工作經(jīng)驗(yàn),現(xiàn)將“基于互聯(lián)網(wǎng)絡(luò)環(huán)境”,利用C#語言,有效利用FtpWebRequest與DosFramer控件,開發(fā)公文編寫與傳輸?shù)慕?jīng)驗(yàn)加以總結(jié),供同行及程序開發(fā)愛好者借鑒。
2 開發(fā)與運(yùn)行環(huán)境構(gòu)建
①FTP服務(wù)器環(huán)境:基于Windows 7.0,基于Internet信息服務(wù)(IIS)管理器,添加FTP站點(diǎn),依據(jù)對話框,設(shè)置站點(diǎn)名稱(MyFTP)、內(nèi)容目錄(D:\MyFile)、IP地址(127.0.0.1)等,其中SSL選擇,選擇“無”,“身份驗(yàn)證”選擇“基本”,授權(quán)“允許訪問”選擇“指定用戶”,并填寫已建的用戶(用戶名:user001,密碼:123),“權(quán)限”選擇“讀取”和“寫入”。
②FTP公文目錄規(guī)劃。FTP服務(wù)器內(nèi)容目錄(D:\MyFile)下建立Template和WandTDocument文件夾。
3 開發(fā)環(huán)境構(gòu)建
第一,下載安裝Microsoft Visual Studio 2012(以下簡稱VS2012)。
第二,下載DosFramer.ocx控件,如果控件為32位,拷貝到System32,否則拷貝到SysWOW64文件夾,并對其進(jìn)行注冊。
第三,利用Word 2010創(chuàng)建公文模板(MyTemplate.docx),存于FTP服務(wù)器的Template文件夾內(nèi)。
4 主要功能方法的實(shí)現(xiàn)
①利用VS2012創(chuàng)建WandTDocument項(xiàng)目,通過工具箱建選項(xiàng)卡,添加doframer.ocx。
②在Form1窗體放置:兩個(gè)button控件和一個(gè)DSO Framer Control Object控件,設(shè)置button1的Text為“公文編寫”,button2的Text為“公文發(fā)送”,axFramerControl1的Name為MyEdit。
③在WandTDocument項(xiàng)目的輸出路徑(bin\Debug\)下建立Document文件夾。
④方法定義。在Form1空間引用處輸入:using System.IO;using System.Net;定義窗體級對象與變量,如下:
private FtpWebRequest MyFTP;private string LocalFileName;
private string LocalPath=Application.StartupPath+ @"\Document\";
最后定義如下方法,實(shí)現(xiàn)調(diào)用公文模板編寫公文,并發(fā)送公文,具體如下:
private void Connect(String path)//連接FTP服務(wù)器
{
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,F(xiàn)ileMode.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 基本功能調(diào)用
private void button1_Click(object sender, EventArgs e)
{
LocalFileName=GetFileName();
if (WriteFile(LocalFileName))
{MyEdit.Open(LocalPath +LocalFileName);}
else{ MessageBox.Show("調(diào)取公文模板失??!");}
}
private void button2_Click(object sender, EventArgs e)
{
string localfile =LocalPath +LocalFileName;
if(!SendFile(localfile)){MessageBox.Show("公文發(fā)送失?。?);}
}