简体   繁体   English

.NET 控制台应用程序按计划抛出错误。 手动执行时运行良好

[英].NET Console App throws error when scheduled. Runs fine when executed manually

So I decided to make a console application to automate some repetitive jobs that I generally do on a daily basis.所以我决定制作一个控制台应用程序来自动化一些我通常每天都会做的重复性工作。 The objective is to move some files to a network location as backup.目标是将一些文件移动到网络位置作为备份。 The application runs fine when executed manually.手动执行时,应用程序运行良好。 But when scheduled, it throws an error.但是在计划时,它会引发错误。 On examining the log file (to which all program output is mapped), I found the following stack trace and exception message:在检查日志文件(所有程序 output 都映射到该文件)时,我发现了以下堆栈跟踪和异常消息:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
   at System.IO.File.Copy(String sourceFileName, String destFileName)
   at automateDump.Program.Main(String[] args) in D:\software\automateDump\Program.cs:line 78
 The user name or password is incorrect.

I'm using the File.Copy(src, dest) method in System.IO .我在System.IO中使用File.Copy(src, dest)方法。

And for context, the file paths are:对于上下文,文件路径是:

Source: D:\u01\test.file .来源: D:\u01\test.file Destination: \\NetDrive\test.file目的地: \\NetDrive\test.file

Any help would be appreciated.任何帮助,将不胜感激。

EDIT 1编辑 1

Code:代码:

if (File.Exists(movePath + et1))
{
     Console.WriteLine($"Copying {et1} to network location. Size - {sizeInMegaBytes(new FileInfo(movePath + et1).Length)}");
     File.Copy(movePath + et1, networkPath + et1);
     Console.WriteLine("Done Copying");
}

movePath and networkPath are source and destination paths, and et1 is the file. movePathnetworkPath是源路径和目标路径, et1是文件。

sizeInMegaBytes() is a utility function that I wrote. sizeInMegaBytes()是我编写的实用程序 function。 It's irrelevant in this context.在这种情况下是无关紧要的。

EDIT 2编辑 2

Exception Handling code:异常处理代码:

catch (Exception ex)
{
    using (logger = File.AppendText(@"D:\u01\logs.txt"))
    {
         logger.WriteLine($"{ex.StackTrace} \n {ex.Message}");
    }    
}

Theory: you have not checked the option to save the user password for the task:理论:您尚未选中保存任务用户密码的选项:

在此处输入图像描述

Note the explanatory text: using a UNC path means you are trying to access a non-local resource!请注意说明文本:使用 UNC 路径意味着您正在尝试访问非本地资源!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET控制台应用中的预定事件? - Scheduled Events in .NET Console App? 发布.NET Core 2控制台应用程序时出现SQLite错误 - SQLite error when publishing .NET Core 2 Console App 程序将无法按计划任务正常运行。 手动执行程序运行良好 - Program will not run properly as scheduled task. Manually executing the program runs fine 由.NET GUI应用程序的BackgroundWorker执行时,Console.Write不起作用 - Console.Write is not working when executed by BackgroundWorker of .NET GUI Application ASP.NET Core 2.0 Web 应用程序在将项目部署到 Azure Web 服务时失败并出现 SQLite 错误,但在本地环境中运行良好 - ASP.NET Core 2.0 Web Application fails with a SQLite error when deploying the project to Azure Web Service but runs fine in local environment ASP.NET Core 1.1 在本地运行良好,但在发布到 Azure 时显示“启动应用程序时出错”。 - ASP.NET Core 1.1 runs fine locally but when publishing to Azure says “An error occurred while starting the application.” Winforms中使用的STA线程,但作为控制台应用程序执行时不使用 - STA thread used in Winforms but not when executed as a Console app 从任务计划程序运行时,Windows服务器上的计划的C#控制台应用程序不显示控制台 - Scheduled c# console app on windows server does not show console when ran from task scheduler 当 exe 从启动运行时,log4net 不记录日志,但在手动启动时工作正常 - log4net not logging when the exe is running from startup but works fine when started manually 在Windows中安排时,控制台应用程序未启动进程 - Console application not starting processes when scheduled in Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM