<div> PrintDocument docToPrint = new PrintDocument();
docToPrint.DocumentName = GetPath(sourcePath);
//开始打印
docToPrint.Print();[/code]
[code]/// <summary>
/// 运行命令
/// </summary>
/// <param name="strShellCommand">命令字符串</param>
/// <returns>命令运行时间</returns>
private static double RunShell(string strShellCommand)
{
double spanMilliseconds = 0;
DateTime beginTime = DateTime.Now;
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.Arguments = String.Format(@"/c {0}", strShellCommand);
cmd.Start();
cmd.WaitForExit();
DateTime endTime = DateTime.Now;
TimeSpan timeSpan = endTime – beginTime;
spanMilliseconds = timeSpan.TotalMilliseconds;
return spanMilliseconds;[/code]
3、检查文件是否生成完毕
[code]</strong>
<strong>/// <summary>
/// 检查是否转换成功(文件是否生成完毕)
/// </summary>
/// <param name="sourcePath">要检查文件地址</param>
/// <param name="targetPath">要复制到的地址(如果不需要真正复制,请跟sourcePath一致)</param>
/// <param name="timeout">最大等待时间</param>
/// <returns></returns>
private static bool IsParseSuccess(string sourcePath, string targetPath, int timeout)
{
bool isSuccess = false;
if (timeout <= 0)
timeout = 30;
int i = 0;
while (!RenameFile(sourcePath, targetPath))
{
Thread.Sleep(1000);
i++;
if (i == timeout)
break;
}
if (i < timeout)
isSuccess = true;
return isSuccess;
}
/// <summary>
/// 重命名文件(用来检查文件是否生成完成)
/// </summary>
/// <param name="sourePath">源地址</param>
/// <param name="targetPath">目标地址</param>
/// <returns></returns>
private static bool RenameFile(string sourePath, string targetPath)
{
bool isOpen = false;
//如果是相同地址,直接移动检查是否文件已经生成,否则进行复制(因为目标文件存在的话会有问题)
if (sourePath.Equals(targetPath))
{
try
{
//移动文件
File.Move(sourePath, targetPath);
isOpen = true;
}
catch (Exception e)
{
}
}
else
{
bool isCopySuccess = false;
try
{
//复制文件
File.Copy(sourePath, targetPath, true);
isCopySuccess = true;
}
catch (Exception e)
{
isCopySuccess = false;
}
if (isCopySuccess)
{
//如果复制成功,删除源文件
File.Delete(sourePath);
}
}
return isOpen;
}[/code]
4、杀掉进行函数
[code] /// <summary>
/// 根据进程名称来关闭进程
/// </summary>
/// <param name="processName"></param>
private static void KillPrecess(string processName)
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == processName)
{
p.Kill();
}
}
}[/code]
评论前必须登录!
注册