Windows服务创建及安装word转换成xps.docx

上传人:b****1 文档编号:2394635 上传时间:2023-05-03 格式:DOCX 页数:17 大小:365.67KB
下载 相关 举报
Windows服务创建及安装word转换成xps.docx_第1页
第1页 / 共17页
Windows服务创建及安装word转换成xps.docx_第2页
第2页 / 共17页
Windows服务创建及安装word转换成xps.docx_第3页
第3页 / 共17页
Windows服务创建及安装word转换成xps.docx_第4页
第4页 / 共17页
Windows服务创建及安装word转换成xps.docx_第5页
第5页 / 共17页
Windows服务创建及安装word转换成xps.docx_第6页
第6页 / 共17页
Windows服务创建及安装word转换成xps.docx_第7页
第7页 / 共17页
Windows服务创建及安装word转换成xps.docx_第8页
第8页 / 共17页
Windows服务创建及安装word转换成xps.docx_第9页
第9页 / 共17页
Windows服务创建及安装word转换成xps.docx_第10页
第10页 / 共17页
Windows服务创建及安装word转换成xps.docx_第11页
第11页 / 共17页
Windows服务创建及安装word转换成xps.docx_第12页
第12页 / 共17页
Windows服务创建及安装word转换成xps.docx_第13页
第13页 / 共17页
Windows服务创建及安装word转换成xps.docx_第14页
第14页 / 共17页
Windows服务创建及安装word转换成xps.docx_第15页
第15页 / 共17页
Windows服务创建及安装word转换成xps.docx_第16页
第16页 / 共17页
Windows服务创建及安装word转换成xps.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Windows服务创建及安装word转换成xps.docx

《Windows服务创建及安装word转换成xps.docx》由会员分享,可在线阅读,更多相关《Windows服务创建及安装word转换成xps.docx(17页珍藏版)》请在冰点文库上搜索。

Windows服务创建及安装word转换成xps.docx

Windows服务创建及安装word转换成xps

什么是Windows服务?

 

  Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。

它没有用户界面,并且也不会产生任何可视输出。

任何用户消息都会被写进Windows事件日志。

计算机启动时,服务会自动开始运行。

它们不要用户一定登录才运行,它们能在包括这个系统内的任何用户环境下运行。

通过服务控制管理器,Windows服务是可控的,可以终止、暂停及当需要时启动。

创建一个Windows服务

选择VisualC#->Windows->WindowsService,创建一个word2xpsservice项目。

设计界面点右键,在弹出菜单中选择”AddInstaller“。

点击后出来的设计界面上添加了ServiceProcessInstaller1和ServiceInstaller1两个控件。

 ServiceProcessInstall安装一个可执行文件,该文件包含扩展ServiceBase的类。

该类由安装实用工具(如InstallUtil.exe)在安装服务应用程序时调用。

在这里主要是修改其Account属性。

ServiceAccount指定服务的安全上下文,安全上下文定义其登录类型。

LocalService   充当本地计算机上非特权用户的帐户,该帐户将匿名凭据提供给所有远程服务器。

 

LocalSystem   服务控制管理员使用的帐户,它具有本地计算机上的许多权限并作为网络上的计算机。

 

NetworkService   提供广泛的本地特权的帐户,该帐户将计算机的凭据提供给所有远程服务器。

 

User   由网络上特定的用户定义的帐户。

如果为ServiceProcessInstaller.Account成员指定User,则会使系统在安装服务时提示输入有效的用户名和密码,除非您为ServiceProcessInstaller实例的Username和Password这两个属性设置值。

ServiceInstaller安装一个类,该类扩展ServiceBase来实现服务。

在安装服务应用程序时由安装实用工具调用该类。

在这里主要修改其StartType属性。

此值指定了服务的启动模式。

Automatic 指示服务在系统启动时将由(或已由)操作系统启动。

如果某个自动启动的服务依赖于某个手动启动的服务,则手动启动的服务也会在系统启动时自动启动。

Disabled 指示禁用该服务,以便它无法由用户或应用程序启动。

 

Manual 指示服务只由用户(使用“服务控制管理器”)或应用程序手动启动。

 

在ProjectInstaller.cs代码里

publicProjectInstaller()

{

InitializeComponent();

this.Committed+=newInstallEventHandler(ProjectInstaller_Committed);

}

voidProjectInstaller_Committed(objectsender,InstallEventArgse)

{

System.ServiceProcess.ServiceControllercontroller=newSystem.ServiceProcess.ServiceController("Service2");//Service1为ServiceInstaller中ServiceName

controller.Start();

}

测试服务

在Service1.cs里面

•OnStart–控制服务启动

•OnStop–控制服务停止

protectedoverridevoidOnStart(string[]args)

{

FileStreamfs1=newFileStream(@"d:

\mcWindowsService.txt",FileMode.OpenOrCreate,FileAccess.Write);

StreamWriterm_streamWriter1=newStreamWriter(fs1);

m_streamWriter1.BaseStream.Seek(0,SeekOrigin.End);

m_streamWriter1.WriteLine("测试OnStart"+DateTime.Now.Date+"\n");

m_streamWriter1.Flush();

m_streamWriter1.Close();

fs1.Close();

}

protectedoverridevoidOnStop()

{

FileStreamfs1=newFileStream(@"d:

\mcWindowsService.txt",FileMode.OpenOrCreate,FileAccess.Write);

StreamWriterm_streamWriter1=newStreamWriter(fs1);

m_streamWriter1.BaseStream.Seek(0,SeekOrigin.End);

m_streamWriter1.WriteLine("测试OnStop"+DateTime.Now.Date+"\n");

m_streamWriter1.Flush();

m_streamWriter1.Close();

fs1.Close();

}

打开MicrosoftVisualStudio2010—》VisualStudioTools—》VisualStudioCommandPrompt(2010)右键选择以管理员身份运行(A)。

找到word2xpsservice.exe所在的路径F:

\戴天赐\word2xpsservice\word2xpsservice\Bin

\Debug将下面的复制到VisualStudioCommandPrompt(2010)

installutil"F:

/戴天赐/word2xpsservice/word2xpsservice/bin/Debug/word2xpsservice.exe"。

可以在d:

\mcWindowsService.txt中看到上面的结果,服务这个可以使用。

卸载服务则installutil-u"F:

/戴天赐/word2xpsservice/word2xpsservice/bin/Debug/word2xpsservice.exe"。

创建基于Timer的服务器任务

Timer组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed事件的周期性间隔。

然后可以操控此事件以提供定期处理。

例如,假设您有一台关键性服务器,必须每周7天、每天24小时都保持运行。

可以创建一个使用Timer的服务,以定期检查服务器并确保系统开启并在运行。

如果系统不响应,则该服务可以尝试重新启动服务器或通知管理员。

基于服务器的Timer是为在多线程环境中用于辅助线程而设计的。

服务器计时器可以在线程间移动来处理引发的Elapsed事件,这样就可以比Windows计时器更精确地按时引发事件。

System.Timers.Timertimer1=newSystem.Timers.Timer();

publicService1()

{

InitializeComponent();

timer1.Interval=2000;//SettheIntervalto2seconds(2000milliseconds).

timer1.Enabled=true;

timer1.Elapsed+=newSystem.Timers.ElapsedEventHandler(timer1_Elapsed);

timer1.Start();

}

voidtimer1_Elapsed(objectsender,System.Timers.ElapsedEventArgse)

{

FileStreamfs1=newFileStream(@"d:

\mcWindowsService.txt",FileMode.OpenOrCreate,FileAccess.Write);

StreamWriterm_streamWriter1=newStreamWriter(fs1);

m_streamWriter1.BaseStream.Seek(0,SeekOrigin.End);

m_streamWriter1.WriteLine("测试Helloworld!

"+DateTime.Now.ToString()+"\n");

m_streamWriter1.Flush();

m_streamWriter1.Close();

fs1.Close();

}

加上上面这段代码系统将会每隔2秒往d:

\mcWindowsService.txt添加一条"测试Helloworld!

"语句。

创建读取xml文件路径信息对文件路径内的word转化成.xps文件的服务器任务

首先,获取xml文件内的路径信息。

privatevoidReadXml()

{

#region

FileStreamfs1=newFileStream(@"d:

\mcWindowsService.txt",FileMode.OpenOrCreate,FileAccess.Write);

StreamWriterm_streamWriter1=newStreamWriter(fs1);

m_streamWriter1.BaseStream.Seek(0,SeekOrigin.End);

m_streamWriter1.WriteLine("starttoread:

"+thread_interval+"\n");

m_streamWriter1.Flush();

m_streamWriter1.Close();

fs1.Close();

#endregion

wordPathList=newList();

stringxmlLocation=@"D:

\终结版\word2xps_window_service\WindowsService1\bin\Debug\windows_service_config.xml";

XmlReaderreader=XmlReader.Create(xmlLocation);

while(reader.Read())

{

if(reader.NodeType==XmlNodeType.Element&&reader.Name=="thread_tmie_interval")

{

thread_interval=int.Parse(reader.GetAttribute("value").ToString());

}

if(reader.NodeType==XmlNodeType.Element&&reader.Name=="thread_count")

{

thread_count=int.Parse(reader.GetAttribute("value").ToString());

}

if(reader.NodeType==XmlNodeType.Element&&reader.Name=="path")

{

wordPathList.Add(reader.GetAttribute("value").ToString());

}

if(reader.NodeType==XmlNodeType.EndElement&&reader.Name=="pathlist")

{

break;

}

}

if(thread_interval>1)

{

timer1.Interval=thread_interval;

timer1.Start();

timer1.Elapsed+=newSystem.Timers.ElapsedEventHandler(timer1_Elapsed);

}

}

XmlReader提供对XML数据进行快速、非缓存、只进访问的读取器。

reader.GetAttribute("value").ToString()来获取值。

其次,在获取了路径信息后就对文件夹内的文件进行遍历,取出未转化成xps的.doc/.docx文件。

DirectoryInfo.GetFileSystemInfos()取出文件内的文件。

publicvoidListFiles(FileSystemInfoinfo)

{

ListfliesName=newList();

DirectoryInfodir=infoasDirectoryInfo;

//不是目录

if(dir==null)

{

return;

}

FileSystemInfo[]files=dir.GetFileSystemInfos();

ListwordFileInfos=newList();

ListxpsFileInfos=newList();

fileInfos=newList();

#region

for(inti=0;i

{

FileInfofile=files[i]asFileInfo;

//是文件

if(file!

=null)

{

if(file.FullName.Substring(file.FullName.LastIndexOf("."))==".doc"||file.FullName.Substring(file.FullName.LastIndexOf("."))==".docx")

{

wordFileInfos.Add(file);

fileInfos.Add(file);

}

elseif(file.FullName.Substring(file.FullName.LastIndexOf("."))==".xps")

{

xpsFileInfos.Add(file);

}

foreach(varwordinwordFileInfos)

{

stringwordName=word.Name.Substring(0,word.Name.LastIndexOf("."));

foreach(varxpsinxpsFileInfos)

{

if(wordName==xps.Name.Substring(0,xps.Name.LastIndexOf(".")))

{

fileInfos.Remove(word);

}

}

}

}

//对于子目录,进行递归调用

else

{

}

}

#endregion

}

最后将所有为转化的word文件转化成xps文件

privatevoidStartToConvert()

{

timer1.Stop();

wordPathList.ForEach(re=>

{

ListFiles(newDirectoryInfo(re));

fileInfos.ForEach(fif=>

{

//ConvertDelegateconvert_delegate=Convert;

//IAsyncResultresult=convert_delegate.BeginInvoke(fif.FullName,fif.DirectoryName+"\\"+fif.Name.Substring(0,fif.Name.LastIndexOf("."))+".xps",Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatXPS,null,null);

Word2Xps.Convert(fif.FullName,fif.DirectoryName+"\\"+fif.Name.Substring(0,fif.Name.LastIndexOf("."))+".xps",Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatXPS);

});

});

timer1.Start();

}

Word2Xps为word转化成xps的方法类。

放在Word2Xps.cs里面

publicclassWord2Xps

{

publicstaticboolConvert(stringsourcePath,stringtargetPath,Microsoft.Office.Interop.Word.WdExportFormatexportFormat)

{

boolresult=false;

objectparamMissing=Type.Missing;

Microsoft.Office.Interop.Word.ApplicationwordApplication=newMicrosoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word.DocumentwordDocument=null;

try

{

objectparamSourceDocPath=sourcePath;

stringparamExportFilePath=targetPath;

Microsoft.Office.Interop.Word.WdExportFormatparamExportFormat=exportFormat;

boolparamOpenAfterExport=false;

Microsoft.Office.Interop.Word.WdExportOptimizeForparamExportOptimizeFor=

Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;

Microsoft.Office.Interop.Word.WdExportRangeparamExportRange=Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;

intparamStartPage=0;

intparamEndPage=0;

Microsoft.Office.Interop.Word.WdExportItemparamExportItem=Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;

boolparamIncludeDocProps=true;

boolparamKeepIRM=true;

Microsoft.Office.Interop.Word.WdExportCreateBookmarksparamCreateBookmarks=

Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;

boolparamDocStructureTags=true;

boolparamBitmapMissingFonts=true;

boolparamUseISO19005_1=false;

wordDocument=wordApplication.Documents.Open(

refparamSourceDocPath,refparamMissing,refparamMissing,

refparamMissing,refparamMissing,refparamMissing,

refparamMissing,refparamMissing,refparamMissing,

refparamMissing,refparamMissing,refparamMissing,

refparamMissing,refparamMissing,refparamMissing,

refparamMissing);

if(wordDocument!

=null)

wordDocument.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat,paramOpenAfterExport,

paramExportOptimizeFor,paramExportRange,paramStartPage,

paramEndPage,paramExportItem,paramIncludeDocProps,

paramKeepIRM,paramCreateBookmarks,paramDocStructureTags,

paramBitmapMissingFonts,paramUseISO19005_1,

refparamMissing);

result=true;

}

catch(Exceptione)

{

}

finally

{

if(wordDocument!

=null)

{

wordDocument.Close(refparamMissing,refparamMissing,refparamMissing);

wordDocument=null;

}

if(wordApplication!

=null)

{

wordApplication.Quit(refparamMissing,refparamMissing,refparamMissing);

wordApplication=null;

}

GC.Collect();

GC.WaitForPendingFinalizers();

}

returnresult;

}

}

在转化过程中会出现WordDocuments.Openreturnsnull,解决办法就是在C:

\Wind

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2