亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

文件未正確復制到新位置

文件未正確復制到新位置

C#
開滿天機 2022-01-15 16:49:37
我已經編寫了 ac# 代碼來將 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.12\Users";            string targetPath = @"C:\Users\Admin\source";            // 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);誰能弄清楚錯誤是什么?
查看完整描述

3 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

如果文件不存在,File.Delete實際上不會引發任何異常。因此,我將完全刪除存在的檢查。


try {

int delay = 400;


File.Delete(@"C:\Users\Admin\source\Bargstedt.csv");

Thread.Sleep(delay); // to prevent delete and copy happening at the 

// same time.


System.IO.File.Copy(s, destFile, true);                          


}catch (IOException ex) {}

您還可以查看此帖子:


C# 中的 FileStream 和 FileSystemWatcher,奇怪的問題“進程無法訪問文件” 和用戶 EventHorizon 的答案檢查文件是否已關閉。


還建議檢查目錄是否有權限 (FileIOPermissionAccess.Write)


查看完整回答
反對 回復 2022-01-15
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

我想你的意思是即使有文件也要寫文件。


您正在使用:


bool exists = info.Exists;

if (exists == true)

{

    File.Delete(@"C:\Users\Admin\source\Bargstedt.csv");

}

else

    System.IO.File.Copy(s, destFile, true); 

}

刪除其他:


bool exists = info.Exists;

if (exists == true)

{

    File.Delete(@"C:\Users\Admin\source\Bargstedt.csv");

System.IO.File.Copy(s, destFile, true);


查看完整回答
反對 回復 2022-01-15
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

當文件存在于目標路徑中并具有該現有文件的備份計劃時,使用它來復制文件。


// 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.

        }


查看完整回答
反對 回復 2022-01-15
  • 3 回答
  • 0 關注
  • 233 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號