开发者

Im using console application in console window i see each line twice why?

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int y;
            FileInfo fi;
            string[] newDest_files;
            string[] lines = global::EachFileDirectory.MyResource.video_formats.Split    (new Char[] {'\r','\n'});
            foreach (string line in lines)
            {
                newDest_files = Directory.GetFiles(Environment.CurrentDirectory,         line); //strFilter );
                for (y = 0; y < newDest_files.Length; y++)
                {
                    fi = new FileInfo(newDest_files[y]);
                    string newFolder = Path.Combine(fi.DirectoryName, fi.Name                 + "_Directory");
                    if (!Directory.Exists(newFolder))
                        Directory.CreateDirectory(newFolder);
                    Console.WriteLine("processed: " + line.Length + " files");
                    File.Move(fi.FullName, Path.Combine(newFolder, fi.Name));
                }
                if (newDest_files.Length == 0)
                {
                    Console.WriteLine(line + "No files found in the current directory      of format: ");
             }
        }
        Console.WriteLine("\n Done! Press any key to exit...");
        Console.ReadKey();         
    }
}

}

The problem is when i have added the line: string[] lines = global::EachFileDirectory.MyResource.video_formats.Split(new Char[] {'\r','\n'});

Since then i see in the console window all the lines double for example:

*.264 No files found in the current directory of format: No files found in the current directory of format: *.mov No files found in the current directory of format: No files found in the current directory of format:

What i need to see in the console window is:

*.264 No files found in the current directory of format: *.mov No files found in the current directory of format:

And so on but since i have the line: string[] lines = global::EachFileDirectory.MyResource.video_formats.Split(new Char[] {'\r','\n'}); And also the line in the end of the code: Console.WriteLine(line + "No files found in the current directory of format: ");开发者_StackOverflow

I see No files found in the current directory of format: also in between each line.

How can i fix it ?

On the reference of the project i added a text file wich contain many video formats extentions in this way:

*.264 *.mov *.avi

and so on.....

The problem is that the text: No files found in the current directory of format: Appears near each format and also between two formats and i want ti to be in the console window only near each format !

Thanks a lot.


You're splitting on \r and \n - which means you're turning input of something like

"foo\r\nbar"

into:

{ "foo", "", "bar" }

... so you're then trying to find all the files with an empty pattern.

There are various options:

  • Create a StringReader and call ReadLine multiple times instead of using Split
  • Replace \r with the empty string before splitting
  • Split on just \n and then trim each entry
  • Specify StringSplitOptions.RemoveEmptyEntries after splitting


Add StringSplitOptions.RemoveEmptyEntries to Split method:

string[] lines = global::EachFileDirectory.MyResource.video_formats.Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜