文件未正确复制到新位置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件未正确复制到新位置相关的知识,希望对你有一定的参考价值。
我编写了一个c#代码将csv文件复制到新位置。
如果文件已存在于目标位置文件中,则应删除该文件并将其重新粘贴。
此过程应重复进行并在后台运行,因为csv文件每5分钟更新一次。
当前问题甚至是文件在目标路径中被删除,新文件将不会被写回。
我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace filemove
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
public void Start()
{
mysql();
}
static void mysql()
{
string fileName = "data.csv";
string sourcePath = @"\192.168.16.12Users";
string targetPath = @"C:UsersAdminsource";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
File.Delete(@"C:UsersAdminsourceBargstedt.csv");
}
else
{
System.IO.File.Copy(s, destFile, true);
}
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
//Console.ReadKey();
}
public void OnStop()
{
}
}
}
任何人都可以弄清楚错误是什么?
答案
如果文件不存在,File.Delete实际上不会抛出任何异常。所以,我将完全删除检查存在。
try {
int delay = 400;
File.Delete(@"C:UsersAdminsourceBargstedt.csv");
Thread.Sleep(delay); // to prevent delete and copy happening at the
// same time.
System.IO.File.Copy(s, destFile, true);
}catch (IOException ex) {}
你也可以查看这篇文章:
FileStream and a FileSystemWatcher in C#, Weird Issue "process cannot access the file"和用户EventHorizon的答案检查文件是否已关闭。
还建议检查目录是否具有权限(FileIOPermissionAccess.Write)
另一答案
我认为你的意思是即使有文件也要写文件。
你正在使用:
bool exists = info.Exists;
if (exists == true)
{
File.Delete(@"C:UsersAdminsourceBargstedt.csv");
}
else
{
System.IO.File.Copy(s, destFile, true);
}
删除其他:
bool exists = info.Exists;
if (exists == true)
{
File.Delete(@"C:UsersAdminsourceBargstedt.csv");
}
System.IO.File.Copy(s, destFile, true);
另一答案
使用此选项可在文件存在于目标路径中时复制该文件,并使用该文件的备份计划。
// To copy a file to another location and
// overwrite the destination file if it already exists.
if (!File.Exists(destFile))
{
System.IO.File.Copy(sourceFile, destFile, true);
}
else
{
System.IO.File.Move(destFile, existingFilePath); //if file is existing and then move it to specific folder
try
{
System.IO.File.Copy(sourceFile, destFile, true);
}
catch (Exception)
{
System.IO.File.Move(existingFilePath, destFile); //If anythig went wrong, old file is relocated correctly
}
System.IO.File.Delete(existingFilePath); // Delete old file, all is ok now.
}
以上是关于文件未正确复制到新位置的主要内容,如果未能解决你的问题,请参考以下文章