/FreeTranslator/TextExtractors/Txt/TxtFileReader.cs
# · C# · 70 lines · 47 code · 9 blank · 14 comment · 5 complexity · 3d0c9ae68e66ee8fb4586b4df997e39c MD5 · raw file
- /*
- * Please leave this Copyright notice in your code if you use it
- * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
- */
- namespace TextExtractors
- {
- using System;
- using System.IO;
- using System.Text;
- using System.ComponentModel.Composition;
- using CommonUtils;
-
- [Export(typeof(ITextImport))]
- //[ExportMetadata("SpecialFormat", false)]
- [ExportMetadata("Description", "text files")]
- [ExportMetadata("FileExtension", "*.txt")]
- [ExportMetadata("ContextMenuText", "&Translate")]
- sealed class TxtFileReader : ITextImport
- {
-
- #region ITextImport Members
-
- public string GetAllTextFromFile(string fileName,out Encoding encoding)
- {
- if (string.IsNullOrEmpty(fileName))
- {
- throw new ArgumentException(null, "fileName");
- }
- if (!File.Exists(fileName))
- {
- throw new ArgumentException("File '" + fileName + "' is not found");
- }
- string str = string.Empty;
- try
- {
- var arg = new ProgressEventArgs();
- arg.Description = "Opening txt file";
- arg.Progress = 0;
- if (ProgressChanged != null && ProgressChanged.GetInvocationList().Length > 0)
- ProgressChanged(this, arg);
- using (StreamReader reader = new StreamReader(fileName, true))//GetEncoding(0x4e3)))
- {
- encoding = reader.CurrentEncoding;
- return reader.ReadToEnd();
- }
- }
- catch (Exception exception)
- {
- throw new Exception("Error during text extraction from Text-file: " + fileName + Environment.NewLine + exception.Message);
- }
- //return str;
- }
-
-
- //public string FileDescription
- //{
- // get { return "Txt files"; }
- //}
-
- //public string FileExtension
- //{
- // get { return "*.txt"; }
- //}
-
- public event EventHandler ProgressChanged;
-
- #endregion
- }
- }