[英]File.Move only Copy the file
I've an application which after generate a file, tries to move it from a network location to a local folder using the File.Move function . 我有一个应用程序,该应用程序生成文件后,尝试使用File.Move函数将其从网络位置移动到本地文件夹。
File.Move(_backupSource + @"\" + filename, _backupDestination + @"\" + filename);
_backupSource is the network location and _backupDestintation is the local folder. _backupSource是网络位置,_backupDestintation是本地文件夹。
The file is copied to the destination location, from a few days is not deleting the file on the Source, like if I used File.Copy, but this aplication was working fine for two months now. 该文件被复制到目标位置,几天之内并没有删除源文件 ,就像我使用File.Copy一样,但是这种复制现在可以正常工作两个月了。
I also looked at the event viewer from the two servers and no exception was generated. 我还从两台服务器查看了事件查看器,没有生成异常。
¿What could happen? ¿会发生什么?
EDIT: One thing that could be important. 编辑:一件事可能很重要。 This app is triggered by the task scheduler every night.
每晚,任务调度程序都会触发此应用。 If I run the aplication manually, the file is moved (not copied)
如果我手动运行应用程序,文件将被移动(未复制)
EDIT 2: Now I've tried to copy the file and then delete the file using the following method 编辑2:现在我试图复制文件,然后使用以下方法删除文件
private static void MoveFiles(string filename)
{
FileInfo file = new FileInfo(_backupSource + @"\" + filename);
FileStream stream = null;
int iteration = 0;
file.CopyTo(_backupDestination + @"\" + filename);
bool isUnlocked = false;
while (!isUnlocked)
{
iteration++;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
isUnlocked = true;
}
catch (IOException ex)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
Thread.Sleep(60 * 1000);
if (iteration > 60)
throw ex;
else
EventLog.WriteEntry("DailyRestore", string.Format("Move File. Iteration number {0}", filename), EventLogEntryType.Information);
}
finally
{
if (stream != null)
stream.Close();
}
}
file.Delete();
}
In the log can see the app was trying by an hour each minute and the file is always "using by another process". 在日志中可以看到该应用每分钟尝试运行一个小时,并且该文件始终在“被另一个进程使用”。 Like I mention, this is when the exe is executed by the scheduler task, if I run the task that execute the exe manually (right click and execute), all the operations are successfully ended.
就像我提到的那样,这是由调度程序任务执行exe时,如果我运行手动执行exe的任务(右键单击并执行),则所有操作均成功结束。
Try to run application as administrator by create command file to run the application as administrator instead of calling the application 尝试通过创建命令文件以管理员身份运行应用程序,以以管理员身份运行该应用程序,而不是调用该应用程序
runas.exe /savecred /user:YourUser "YourAppFullPath"
runas.exe /savecred /user:YourUser "YourAppFullPath"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.