PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. */
  5. namespace TextExtractors
  6. {
  7. using System;
  8. using System.IO;
  9. using System.Text;
  10. using System.ComponentModel.Composition;
  11. using CommonUtils;
  12. [Export(typeof(ITextImport))]
  13. //[ExportMetadata("SpecialFormat", false)]
  14. [ExportMetadata("Description", "text files")]
  15. [ExportMetadata("FileExtension", "*.txt")]
  16. [ExportMetadata("ContextMenuText", "&Translate")]
  17. sealed class TxtFileReader : ITextImport
  18. {
  19. #region ITextImport Members
  20. public string GetAllTextFromFile(string fileName,out Encoding encoding)
  21. {
  22. if (string.IsNullOrEmpty(fileName))
  23. {
  24. throw new ArgumentException(null, "fileName");
  25. }
  26. if (!File.Exists(fileName))
  27. {
  28. throw new ArgumentException("File '" + fileName + "' is not found");
  29. }
  30. string str = string.Empty;
  31. try
  32. {
  33. var arg = new ProgressEventArgs();
  34. arg.Description = "Opening txt file";
  35. arg.Progress = 0;
  36. if (ProgressChanged != null && ProgressChanged.GetInvocationList().Length > 0)
  37. ProgressChanged(this, arg);
  38. using (StreamReader reader = new StreamReader(fileName, true))//GetEncoding(0x4e3)))
  39. {
  40. encoding = reader.CurrentEncoding;
  41. return reader.ReadToEnd();
  42. }
  43. }
  44. catch (Exception exception)
  45. {
  46. throw new Exception("Error during text extraction from Text-file: " + fileName + Environment.NewLine + exception.Message);
  47. }
  48. //return str;
  49. }
  50. //public string FileDescription
  51. //{
  52. // get { return "Txt files"; }
  53. //}
  54. //public string FileExtension
  55. //{
  56. // get { return "*.txt"; }
  57. //}
  58. public event EventHandler ProgressChanged;
  59. #endregion
  60. }
  61. }