C#从服务器

时间:2018-04-11 10:13:57

标签: c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Application2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_search_Click(object sender, EventArgs e)
        {
            //Uses the text entered by user
            //searchAndCopy(txtbox1.Text);

            //Uses file for copy process
            searchAllLines(@"D:\Icyer\liste2.txt");
        }

        private void searchAllLines(string filename)
        {
            foreach (string line in File.ReadLines(filename))
            {
                searchAndCopy(line);
            }
        }

        private void searchAndCopy(string textToSearch)
        {
//            MessageBox.Show("|" + textToSearch + "|");
            string srcfolder = @"X:\xxxx\xxxx\xxxx\xxxx";
            DirectoryInfo Ordner = new DirectoryInfo(srcfolder);
            FileInfo[] Pfad = Ordner.GetFiles("*" + textToSearch + "*", SearchOption.AllDirectories);
            foreach (var item in Pfad)
            {
                string destinationDirectory = @"D:\xxxx\Copy\";
                //Directory.CreateDirectory(destinationDirectory);
                string[] substrings = item.FullName.Split('\\');
                string folder = substrings[srcfolder.Split('\\').Length - 1];

                if(folder == textToSearch) {
                    DirectoryCopy(srcfolder + folder, destinationDirectory + folder, true);
                }
                //File.Copy(item.FullName, item.Name);

            }
        }

        private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            // If the source directory does not exist, throw an exception.
            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: " + sourceDirName);
            }

            // If the destination directory does not exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }


            // Get the file contents of the directory to copy.
            FileInfo[] files = dir.GetFiles();

            foreach (FileInfo file in files)
            {
                // Create the path to the new copy of the file.
                string temppath = Path.Combine(destDirName, file.Name);
                try
                {
                    // Copy the file.
                    file.CopyTo(temppath, false);
                }
                catch { };
            }

            // If copySubDirs is true, copy the subdirectories.
            if (copySubDirs)
            {

                foreach (DirectoryInfo subdir in dirs)
                {
                    // Create the subdirectory.
                    string temppath = Path.Combine(destDirName, subdir.Name);

                    // Copy the subdirectories.
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
                //MessageBox.Show("Done!");
            }
        }
    }
}

当我尝试从" D:复制到D:"时,当我尝试从

更改代码时,代码完全正常工作
string srcfolder = @"D:\xxxx\xxxx\";

string srcfolder = @"X:\xxxx\xxxx\xxxx\xxxx";

应用程序冻结第二个我按下按钮。有人可以告诉我是否必须这样做,因为我试图从服务器复制文件或者代码中的某个地方是否有错误。授予对服务器的访问权限,我有权复制文件。目的地应该留在" D:"。 " X:"到" X:"不起作用,应用程序冻结第二个按钮被按下。

1 个答案:

答案 0 :(得分:0)

正如您的注释所示,目录中有许多文件,您正在检索所有文件,然后在子函数中再次迭代它们。本地磁盘总是比网络位置快得多 - 也许您只是在本地磁盘中有一些测试数据而不是整个树镜像。

一般情况下,您应该将处理工作卸载到单独的帖子中 - 这样您就可以传回状态信息'到您的gui线程并显示它们(应用程序将不再陈旧)。

这一行:

 FileInfo[] Pfad = Ordner.GetFiles("*" + textToSearch + "*", SearchOption.AllDirectories);

检索所有与所有子目录中的textSearch字符串匹配的文件。这取决于文件和目录的数量,这可能需要很长时间。

如果某些东西需要被复制,也许最好迭代每个目录递归检查内容?

关于卸载到另一个帖子,您可以查看here