将文件从源目录复制到目标C#

时间:2016-06-13 13:09:29

标签: c#

我想将我的文件从源文件夹复制到我的dest。文件夹

位置/ URL(表中列的名称)我从这里得到它GetDataByGeneralRoot();

现在我想将那个文件从那个URL复制到一个新目录。

我所做的是:

DataSet1.T_DocumentsDataTable docTab = doc.GetDataByGeneralRoot();

            string gerneralRootPath = docTab.Rows[0]["URL"].ToString();
            gerneralRootPath = gerneralRootPath.Remove(gerneralRootPath.IndexOf("PDF") + 4);

            string datadirectory = "//ch-s-0001535/G/inetpub/DocAddWeb/DataSource/";
            string final = datadirectory + gerneralRootPath;

            foreach (string path in Directory.GetFiles(final, "*.*", SearchOption.AllDirectories))
            {
                string t = path.Substring(path.IndexOf("\\") + 1);
                File.Copy(t, t.Replace(final + t, rootFolderAbsolutePath));

            }

我的问题/问题是我怎么能说我只想从我的方法GetDataByGeneralRoot获取URL中的文件,而不是现在正在发生的所有文件。

这是我的表格的样子: enter image description here

1 个答案:

答案 0 :(得分:1)

我想你想要这样的东西

        public void copyAll(DataSet ds, Doc doc, string rootPath, string rootTargetPath)
    {
        ds.T_DocumentsDataTable docTab = doc.GetDataByGeneralRoot();
        string datadirectory = "//ch-s-0001535/G/inetpub/DocAddWeb/DataSource/";
        string final = datadirectory + rootPath;

        foreach (var row in docTab.Rows)
        {
            var sourceFile = "//ch-s-0001535/G/inetpub/DocAddWeb/DataSource/" + row["URL"].ToString();
            string targetPath = rootTargetPath + row["URL"].ToString();
            File.Copy(sourceFile, rootTargetPath);
        }
    }