public static void PrintAllToFile(string path, string name, ContentParser[] myFiles)
        {
            string fullPath;

            path = path.Trim(new char[] { '/', '*', '%', ':', '\n', '\t', '*', ' ' });
            if (String.IsNullOrEmpty(name))
                name = "tempName";

            if (!String.IsNullOrEmpty(path))
                fullPath = path + '/' + name;
            else fullPath = name;

            if (!String.IsNullOrEmpty(path) && !Directory.Exists(path))
                Directory.CreateDirectory(path);
            switch (Path.GetExtension(name))
            {
                /*case ".epub":
                    fullPath = fullPath.Trim();
                    PrintToFileEpub(fullPath);
                    break;*/
                case ".txt":
                    fullPath = fullPath.Trim();
                    PrintAllToFileText(fullPath, myFiles);
                    break;
                default:
                    fullPath = fullPath + ".txt";
                    fullPath = fullPath.Trim();
                    PrintAllToFileText(fullPath, myFiles);
                    break;
            }

        }
 private static void PrintAllToFileText(string fullPath, ContentParser[] myFiles)
 {
     StringBuilder myString = new StringBuilder();
     foreach(ContentParser myParser in myFiles)
     {
         myString.Append(myParser.title);
         myString.AppendLine();
         myString.Append(myParser.annotation);
         myString.AppendLine();
         myString.Append(myParser.content);
         myString.AppendLine();
     }
     myString.AppendLine();
     myString.Append(myFiles[0].linkDownloader.OriginalUrl);
     File.WriteAllText(fullPath, myString.ToString());
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
        start:
            Console.WriteLine("Please input the number of required Text URLs:");
            string myUrl;

            try
            {
                int limit = Convert.ToInt32(Console.ReadLine());
                if (limit <= 0)
                    throw new IndexOutOfRangeException("Out of Range");
                ContentParser[] myParser = new ContentParser[limit];
                for (int i = 0; i < limit; i++)
                {
                    Console.WriteLine("Please input the {0} of {1} required Text URLs:", i + 1, limit);
                    myUrl = Console.ReadLine();
                    myParser[i] = new ContentParser(myUrl);
                    myParser[i].Parse();
                }
                Console.WriteLine("Please input the name of the book(including format):");
                ContentParser.PrintAllToFile(" ", Console.ReadLine(), myParser);
            }
            catch (EmptyUrl error)
            {
                Console.WriteLine(error.Error);
                Console.WriteLine("Restart after pressing a key");
                Console.ReadKey();
                Console.Clear();
                goto start;
            }
            catch (InvalidUrl error)
            {
                Console.WriteLine(error.Error);
                Console.WriteLine("Restart after pressing a key");
                Console.ReadKey();
                Console.Clear();
                goto start;
            }
            catch (InvalidPath error)
            {
                Console.WriteLine(error.Error);
                Console.WriteLine("Restart after pressing a key");
                Console.ReadKey();
                Console.Clear();
                goto start;
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("{0}. Restart after pressing a key", e.Message);
                Console.ReadKey();
                Console.Clear();
                goto start;
            }
            catch
            {
                Console.WriteLine("Unknown Eror. Restart after pressing a key");
                Console.ReadKey();
                Console.Clear();
                goto start;
            }
        }