安國有 王淑妍
【摘? 要】基于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("公文發送失敗!");}
}