【发布时间】:2018-12-30 00:02:39
【问题描述】:
我在一家公司工作,我被要求编写一些脚本来对目的地进行 robocopy,然后通过电子邮件发送日志,主题为失败或成功。经过相当多的研究后,我无法弄清楚为什么我的程序不断中断,而且它也以一种奇怪的方式中断。
该脚本由一个批处理脚本和一个用于发送电子邮件的 c# 应用程序组成。
RobocopyScript.cmd
@echo off
set robocopydestination=\\[computer]\guest\TESTFOLDER\
set robocopysource=C:\Users\[username]\Script
set robocopylogoutput=log.log
set mailfrom=[email address]
set mailto=[email address]
set successsubject=Robocopy Success
set successbody=Robocopy successfully completed its backup.
set failuresubject=Robocopy Failure
set failurebody=Robocopy failed to complete its backup
set networkcredentialusername=[email address]
set networkcredentialpassword=[password]
set logfile=log.log
robocopy %robocopysource% %robocopydestination% /e /z /tee /log:%robocopylogoutput%
if errorlevel 1 goto success
if errorlevel 0 goto success
goto fail
:fail
EmailProgram.exe %mailfrom% %mailto% %failuresubject% %failurebody% %networkcredentialusername% %networkcredentialpassword% %logfile%
exit
:success
EmailProgram.exe %mailfrom% %mailto% %successsubject% %successbody% %networkcredentialusername% %networkcredentialpassword% %logfile%
exit
EmailProgram.csx
using System;
using System.Net.Mail;
namespace EmailProgram
{
class MainClass
{
public static void Main(string[] args)
{ // from To Subject Body
MailMessage mail = new MailMessage(args[0], args[1], args[2], args[3]);
SmtpClient client = new SmtpClient();
//set up the smtp client
client.Port = 25;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com"; //ncu ncp
client.Credentials = new System.Net.NetworkCredential(args[4], args[5]);
//add 4 lines of space between the body message and the log information for readability
mail.Body += "\r\n\r\n\r\n\r\n----------------\r\nLOG START:\r\n----------------";
//break up the log file into its lines, then write the lines to the email message body
//log file path
string[] logLines = System.IO.File.ReadAllLines(args[6]);
foreach(string s in logLines)
{
mail.Body += s;
}
//send the email, if the email fails to send it simply will close this application
try
{
client.Send(mail);
}
catch(Exception e)
{
System.Console.WriteLine(e);
}
}
}
}
在运行批处理脚本时,robocopy 成功完成,然后当它尝试运行 EmailProgram.exe 时,它给了我:
未处理的异常:System.IO.FileNotFoundException:找不到文件 'C:\Users\[username]\Script\CompleteProjects\RobocopyEmail\completed'。
我绝对不是专家,但我在我的代码中看不到调用异常给出的路径的任何地方。有人可以帮我解决这个问题,因为在试图解决这个问题几个小时后我被难住了。谢谢:)
【问题讨论】:
-
日志文件的路径是什么?
-
个人 id 放弃 robocopy 并只使用 System.IO,并降低复杂性,但这可能不是您当前的问题
-
日志文件只是脚本和可执行文件所在的目录,我尝试创建路径 C:\Logs\log.log 以及相同的结果
标签: c# email batch-file robocopy