PageRenderTime 16ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/FreeTranslator/TextExtractors/Txt/TxtFileReader.cs

#
C# | 70 lines | 47 code | 9 blank | 14 comment | 5 complexity | 3d0c9ae68e66ee8fb4586b4df997e39c MD5 | raw file
 1/*
 2 * Please leave this Copyright notice in your code if you use it
 3 * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
 4 */
 5namespace TextExtractors
 6{
 7    using System;
 8    using System.IO;
 9    using System.Text;
10    using System.ComponentModel.Composition;
11    using CommonUtils;
12
13    [Export(typeof(ITextImport))]
14    //[ExportMetadata("SpecialFormat", false)]
15    [ExportMetadata("Description", "text files")]
16    [ExportMetadata("FileExtension", "*.txt")]
17    [ExportMetadata("ContextMenuText", "&Translate")]
18    sealed class TxtFileReader : ITextImport
19    {
20
21        #region ITextImport Members
22
23        public string GetAllTextFromFile(string fileName,out Encoding encoding)
24        {
25            if (string.IsNullOrEmpty(fileName))
26            {
27                throw new ArgumentException(null, "fileName");
28            }
29            if (!File.Exists(fileName))
30            {
31                throw new ArgumentException("File '" + fileName + "' is not found");
32            }
33            string str = string.Empty;
34            try
35            {
36                var arg = new ProgressEventArgs();
37                arg.Description = "Opening txt file";
38                arg.Progress = 0;
39                if (ProgressChanged != null && ProgressChanged.GetInvocationList().Length > 0)
40                    ProgressChanged(this, arg);
41                using (StreamReader reader = new StreamReader(fileName, true))//GetEncoding(0x4e3)))
42                {
43                    encoding = reader.CurrentEncoding;
44                    return reader.ReadToEnd();
45                }
46            }
47            catch (Exception exception)
48            {
49                throw new Exception("Error during text extraction from Text-file: " + fileName + Environment.NewLine + exception.Message);
50            }
51            //return str;
52        }
53
54
55        //public string FileDescription
56        //{
57        //    get { return "Txt files"; }
58        //}
59
60        //public string FileExtension
61        //{
62        //    get { return "*.txt"; }
63        //}
64
65        public event EventHandler ProgressChanged;
66
67        #endregion
68    }
69}
70