如何将所有输入文件复制到临时目录中

时间:2015-12-03 23:27:31

标签: c# html

如何在我创建的临时目录中提供所有输入文件。只存在最后一个文件..我希望所有文件从我的文件的开头到结尾。 希望有人能在这里找到我的问题。

以下是我的编码。

var partText = Global.PartText;
            Global.PartText = string.Empty;
            Global.TmpFileCount = 0;
            var tempfile = Global.TempContent;
            Global.TempContent = string.Empty;
            var temp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp");
            if (Directory.Exists(temp))
            {
                Directory.Delete(temp, true);
            }
            Directory.CreateDirectory(temp);
            var content = File.ReadAllText(inputfile);
            tempfile = Path.Combine(temp, string.Concat(Path.GetFileNameWithoutExtension(inputfile), ".html"));
            File.WriteAllText(tempfile, content);
            var text = File.ReadAllText(tempfile);
            text = text.Replace("&", "&");
            File.WriteAllText(tempfile, text);
            Global.TmpFileCount++;
            if (tempfile.Contains("<h1"))
            {
                _chapterName = inputXDoc.XPathSelectElement("//h1").Value;
            }
            else if (tempfile.Contains("<h2"))
            {
                _chapterName = inputXDoc.XPathSelectElement("//h2").Value;
            }
            else if (tempfile.Contains("<h3"))
            {
                _chapterName = inputXDoc.XPathSelectElement("//h3").Value;
            }
            else if (tempfile.Contains("<h4"))
            {
                _chapterName = inputXDoc.XPathSelectElement("//h4").Value;
            }
            //Directory.Delete(temp, true);
            //var source = inputXDoc.Descendants(GetNamespace(ref namespace2, "").GetName("body"));
            //if (source.Descendants(GetNamespace(ref namespace2, "").GetName("div")).Any())
            //{
            //  var introduced5 = inputXDoc.Descendants(GetNamespace(ref namespace2, "").GetName("body"));
            //  if (introduced5.Descendants(GetNamespace(ref namespace2, "").GetName("div")).First().Attributes("id").Any())
            //  {
            //      var introduced6 = inputXDoc.Descendants(GetNamespace(ref namespace2, "").GetName("body"));
            //      _chapterName = introduced6.Descendants(GetNamespace(ref namespace2, "").GetName("div")).First().Attributes("id").First().Value;
            //  }
            //}
            if (Regex.IsMatch(ipName, "cover", RegexOptions.IgnoreCase))
            {
                return "cover";
            }
            if (Regex.IsMatch(ipName, "bibliography", RegexOptions.IgnoreCase))
            {
                return "bibliography";
            }
            if (Regex.IsMatch(ipName, "foreword", RegexOptions.IgnoreCase))
            {
                return "foreword";
            }
            if (Regex.IsMatch(ipName, "toc", RegexOptions.IgnoreCase))
            {
                return "toc";
            }
            if (Regex.IsMatch(ipName, "cop(yright)?", RegexOptions.IgnoreCase))
            {
                return "copyright-page";
            }
            if (Regex.IsMatch(ipName, "halftitlepage", RegexOptions.IgnoreCase))
            {
                return "title-page";
            }
            if (Regex.IsMatch(ipName, "titlepage", RegexOptions.IgnoreCase))
            {
                return "title-page";
            }
            if (Regex.IsMatch(ipName, "ded(ication)?", RegexOptions.IgnoreCase))
            {
                return "dedication";
            }
            if (Regex.IsMatch(ipName, "part", RegexOptions.IgnoreCase))
            {
                Global.PartText = _chapterName;
                return "text";
            }
            Global.PartText = partText;
            const string str = "text";
            Global.FileCount++;

1 个答案:

答案 0 :(得分:1)

您每次都要删除目录。改变这个:

        if (Directory.Exists(temp))
        {
            Directory.Delete(temp, true);
        }
        Directory.CreateDirectory(temp);

对此:

        if (!Directory.Exists(temp))
        {
            Directory.CreateDirectory(temp);
        }