Problem with reading (*.doc, *.rtf and *.txt) Files

Hello,

I have a problem with reading files the problem is :

I`m using IO history to read *.txt files in textbox but it cannot read some languages like Arabic

I`m using this code to read

StreamReader files = new StreamReader(openFileDialog1.FileName);

tmpbox.Text = files.ReadToEnd();

files.Close();

it reads all files correctly but not Arabic files

=================================

Second part with RichTextBox:

I`m reading files with this code

RTBox1.LoadFile(openFileDialog1.FileName);

this will read only the files that I`m creating by

RTBox1.SaveFile(openFileDialog1.FileName);

or that *.doc files that had been created on Windows XP

so I`v old files created on WINME or Win98 I cannot read them and I get error when I try to open it in my RTBox

another something...

it cannot read the *.rtf files correctly but it read it like this

{\rtf1\fbidis\ansi\ansicpg1256\deff0{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fswiss\fcharset178{\*\fname Arial;}Arial (Arabic);}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\rtlpar\qr\lang1033\f0\fs20 Mando\lang3073\f1\rtlch\par
}
=========

so plz what is the reason of those problems and how can I solve it

Thanks




Answer this question

Problem with reading (*.doc, *.rtf and *.txt) Files

  • Jason Hayes

    Thanks my friend for you help its working now

    another small question

    How can I edit the Filter of openDialogFile to be when I choose " Documents" it browses '*.doc' and '*.rtf'

    Thank you



  • JohnYG

    hi,

    Thanks shakalama for help

    thats exactly what I was looking for

    Thanks



  • Beth Massi

    hi,

    i don't know how do you save your data but i assume you use streamWriter you can use something like this


    pfd.Filter = "plain text |*.txt";
    DialogResult dr = pfd.ShowDialog();
    StreamReader sr = null;
    Encoding enc = null;
    if (dr == DialogResult.OK)
    {
    sr =
    new StreamReader(pfd.FileName, Encoding.Default);
    textBox1.Text = sr.ReadToEnd();
    enc = sr.CurrentEncoding;
    }
    sr.Close();
    StreamWriter sw = new StreamWriter("filename",true, enc);
    //ToDo : start to save


    like that you can save with the same encoding system that you used to open , the plaintext is Ansi encoding by default

    hope this helps



  • LinuxPenguin2

    ok shakalama

    and I`ll put it in a new thread

    cya



  • abhijit_ghawate

    hi,

    to set dialog filters you do that in paires, lets say you want to open a text files so the filter will follow this format

     "string appear to user | *.file extention ; *.another file extention if needed"

    so in text files it will be "plain text | *.txt"

    in webpages it will be "webpages | *.htm;*.html;*.asp;*.aspx"

     if you want to have 2 filters you simply add both of them splited by "|" like this

    "webpages | *.htm;*.html;*.asp;*.aspx | plain text |*.txt"

    if you want to add more filter pairs you can just split them with this charachter "|" 

    "webpages | *.htm;*.html;*.asp;*.aspx|plain text |*.txt | MsWord | *.doc"

    but note *.doc is a COM object so need a special way to deal with it, i didn't use this b4 so i don't know how http://msdn.microsoft.com/library/default.asp url=/library/en-us/odc_vsto2005_ta/html/OfficeVSTO2005WordOM.asp

    hope this helps



  • russlunn

    hi,

    i have tried this and its working with me i can read arabic with no problems


    public partial class Form1 : Form

    {
    //this form contains RichTextBox , textbox, and

    //2 buttons one to fill txtbox and the other to fill richtextbox

    OpenFileDialog pfd = new OpenFileDialog();
    public Form1()
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    pfd.Filter =
    "plain text |*.txt|Rich Text|*.rtf";
    DialogResult dr = pfd.ShowDialog();
    if (dr == DialogResult.OK)
    {
    switch (pfd.FilterIndex)
    {
    case 1:
    richTextBox1.LoadFile(pfd.FileName,
    RichTextBoxStreamType.PlainText);
    break;
    case 2:
    richTextBox1.LoadFile(pfd.FileName,
    RichTextBoxStreamType.RichText);
    break;
    default:
    break;
    }
    }
    }
    private void button2_Click(object sender, EventArgs e)
    {
    pfd.Filter =
    "plain text |*.txt";
    DialogResult dr = pfd.ShowDialog();
    StreamReader sr = null;
    if (dr == DialogResult.OK)
    {
    sr =
    new StreamReader(pfd.FileName, Encoding.Default);
    textBox1.Text = sr.ReadToEnd();
    }
    sr.Close();
    }
    }


    hope this helps



  • Starik

    Thanks shakalama for your help

    and I wanna to ask how can I use open code that I uesd in my project to open the file when I double click on it, exactly like text file with notepad

    plz try to explain the idea, I think it will be usefull in alot of next projects

    Thanks

    best regards



  • Turkleton

    hello,

    sorry I got a problem,

    after I added Encoding.Defualt to file name now I can read old *.txt files but that made the program cannot read the Arabic files which created by my Program

    but it can read it with Encoding.UTF8, While the (Encoding.UTF8) cannot read the old Arabic *.txt files

    so is there any way to save files with (Encoding.UTF8) , or how could I solve this problem

    Best Regards



  • bobarotti

    hi,

    this is tottaly different question i would prefer if you started a new thread for that

    best regards



  • scottcable

    hi,

    you welcome



  • Problem with reading (*.doc, *.rtf and *.txt) Files