C# 如何在Web应用程序中移动文件过程中提醒用户重复

C# 如何在Web应用程序中移动文件过程中提醒用户重复,c#,exception,exception-handling,server-side,C#,Exception,Exception Handling,Server Side,我有一个具有相同文件夹和文件名的源和目标路径(源有一些额外的文件)。我的问题是当我剪切源位置文件和文件夹并粘贴目标位置时 如何最初复制额外文件(目标没有文件) 如何通过粘贴额外文件后的错误“文件夹和文件已存在,是否要替换它”消息 收到响应后,如何移动和删除源文件 有人可以帮我,伙计们,我被这个逻辑困扰了将近两天 注意:我是C#服务器端代码的初学者 谢谢,预支。大家好,谢谢你们的回复,我已经用#RezaNoei写了相同的结构,我提到我的代码是 private void DirectoryCopy(

我有一个具有相同文件夹和文件名的源和目标路径(源有一些额外的文件)。我的问题是当我剪切源位置文件和文件夹并粘贴目标位置时

  • 如何最初复制额外文件(目标没有文件)
  • 如何通过粘贴额外文件后的错误“文件夹和文件已存在,是否要替换它”消息
  • 收到响应后,如何移动和删除源文件
  • 有人可以帮我,伙计们,我被这个逻辑困扰了将近两天

    注意:我是C#服务器端代码的初学者


    谢谢,预支。大家好,谢谢你们的回复,我已经用#RezaNoei写了相同的结构,我提到我的代码是

    private void DirectoryCopy(string sourceDirName, string destDirName, bool replace, string action)
        {
            try
            {
                // Gets the subdirectories for the specified directory.
                var dir = new DirectoryInfo(sourceDirName);
    
                var dirs = dir.GetDirectories();
                // If the destination directory doesn't exist, creates it.
                if (!Directory.Exists(destDirName))
                {
                    Directory.CreateDirectory(destDirName);
                }
    
                // Gets the files in the directory and copy them to the new location.
                var files = dir.GetFiles();
                foreach (var file in files)
                {
                    var oldPath = Path.Combine(sourceDirName, file.Name);
                    var temppath = Path.Combine(destDirName, file.Name);
                    var fileExist = File.Exists(temppath);
                    if (!fileExist)
                    {
                        if (action != "paste")
                        {
                            file.CopyTo(temppath, true);
                        }
                        else
                        {
                            File.Move(oldPath, temppath);
                        }
                    }
                    else if (fileExist && replace)
                    {
                        File.Delete(temppath);
                        if (action != "paste")
                        {
                            file.CopyTo(temppath, true);
                        }
                        else
                        {
                            File.Move(oldPath, temppath);
                        }
                    }
                }
                if (action == "paste")
                {
                    DeleteDirectory(sourceDirName);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    
    使用此功能:

    注意:如果您正在开发一个web应用程序,并且希望通过Html发出警告,那么它对您没有帮助。请阅读下一节“适用于Web应用程序”
    注意2:此函数不检查内部文件夹。所以如果你有一个嵌套的路径,我们应该写一个递归函数

        public void Copy(string Source, string Destination)
        {
            string[] SourceFiles = System.IO.Directory.GetFiles(Source);
            for (int i = 0; i < SourceFiles.Length; i++)
            {
                string DestinationFilePath = System.IO.Path.Combine(Destination, System.IO.Path.GetFileName(SourceFiles[i]));
                if (System.IO.File.Exists(DestinationFilePath))
                {
                    var DialogResult = MessageBox.Show($"File `{System.IO.Path.GetFileName(SourceFiles[i])}` Exists in the Destination. Are you want to overwrite this file ?", "File Exist !", MessageBoxButtons.YesNo);
                    if (DialogResult == DialogResult.Yes)
                        System.IO.File.Copy(SourceFiles[i], DestinationFilePath, true);
                }
                else
                {
                    System.IO.File.Copy(SourceFiles[i], DestinationFilePath);
                }
            }
        }
    
    因此:

  • 下一步是复制非重复文件并在会话中添加重复:

     1-1 **Destination Folder**
     1-2 **List of Duplicated SourceFiles** 
    
    public void Copy(string Source, string Destination)
    {
        /// Set Session ....
        Session["Destination"] = Destination;
    
        List<string> DuplicatedFiles = new List<string>();
    
        string[] SourceFiles = System.IO.Directory.GetFiles(Source);
        for (int i = 0; i < SourceFiles.Length; i++)
        {
            string DestinationFilePath = System.IO.Path.Combine(Destination, System.IO.Path.GetFileName(SourceFiles[i]));
    
            if (System.IO.File.Exists(DestinationFilePath))
            {
               // Add into Duplication List
               DuplicatedFiles.Add(SourceFiles[i]);
            }
            else
            {
                System.IO.File.Copy(SourceFiles[i], DestinationFilePath);
            }
        }
        /// Set Session .....
        Session["DouplicatedFiles"] = DuplicatedFiles;
    }
    
    公共无效副本(字符串源、字符串目标)
    {
    ///设置会话。。。。
    会话[“目的地”]=目的地;
    列表重复文件=新列表();
    字符串[]SourceFiles=System.IO.Directory.GetFiles(源);
    对于(int i=0;i
  • 以上代码是psudo,目标很明确

  • 下一步是显示复制或复制的结果:
  • 我不知道您想如何实现这样一个复制错误视图,而这并不是答案的一部分。无论如何,您可能希望让用户分别对每个文件或同时对所有文件选择操作

    根据您的偏好,您将有一个ActionMethod(在MVC中)或WebForm中的其他东西来完成以下任务: (如果用户不想替换文件,很容易忘记该操作)

    公共无效复制(bool覆盖)
    {
    如果(!覆盖)
    返回“OK”;
    其他的
    {
    string Destination=会话[“Destination”]作为字符串;
    var DuplicatedFiles=Session[“DouplicatedFiles”]作为列表();
    对于(int i=0;i
    请显示您的代码,这样可以更容易地找到错误并进行解答。2天后您一定要编写一些代码吗?请分享,我们可以帮助你。不经意间,我会想这样的事情可能会起作用:
    foreach(sourceFilePath中的var sourceFilePath){if(File.Exists(Path.Combine)(destinationDirectory,Path.GetFileName(sourceFilePath)){//show File Exists message and get response;if(response==“no”)continue;}File.Copy(sourceFilePath,destinationDirectory,true);}
    大家好,谢谢你们的回复,我已经用#Rezanoe写了相同的结构我提到我的代码是谢谢,但我的情况是哪一个是你们的第一句话“如果你们正在开发一个web应用程序,并且你们想通过Html提醒这个问题,它对你们没有帮助。”我需要这样做caseHi@RezaNoei,谢谢,我认为这段代码只在MVC中有效。如果这在Core means中可行,请告诉我需要安装和引用哪些名称空间和NuGet包。使用此功能。
     public void CopyDuplications(bool Overwrite)
     {
        if (!Overwrite)
          return "OK";
        else 
        {
            string Destination = Session["Destination"] as string;
            var DuplicatedFiles = Session["DouplicatedFiles"] as List<string>();
            for (int i = 0; i< DuplicatedFiles.Count; i++) 
            {
                string DestinationFilePath = System.IO.Path.Combine(Destination, System.IO.Path.GetFileName(DuplicatedFiles[i]));
    
                System.IO.File.Copy(DuplicatedFiles[i], DestinationFilePath, true);
            }
        }  
     }