Search code examples
c#listviewtextopenfiledialogsavefiledialog

import/export text to/from listview


i am having two problems: 1- when ever i click on open button, it shows me the openfiledialog twice (when i select my file and click ok, it reopens the selection windows again, only repeats it once). 2- i am trying to export and import text files from a list view, so far i managed to export a text file from the list view, but i failed at importing it back in.

here is my code (for both situations since it's the same project):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    string path;
    //string fname;
    private void abtmenuItem10_Click(object sender, EventArgs e)
    {
        MessageBox.Show("DB Kai UB Text Extractor\n by Omarrrio 2012", "About...", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0, "http://gbatemp.net/user/245642-omarrrio/"); 
    }

    private void exitmenuItem4_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void sbtmenuItem5_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open Sbt File";
        ofd.Filter = "Sbt Files (*.sbt)|*.sbt|All Files (*.*)|*.*";
        //if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    }

    private void msgmenuItem6_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();
        pntrsmenuItem4.Text = "Number of Pointer = ";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open Msg File";
        ofd.InitialDirectory = Application.StartupPath;
        ofd.Filter = "Msg Files (*.msg)|*.msg|All Files (*.*)|*.*";
        DialogResult result = ofd.ShowDialog();
        if (result == DialogResult.Cancel)
            return;
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            path = ofd.FileName;
            BinaryReader br = new BinaryReader(File.OpenRead(path), Encoding.GetEncoding("Shift_JIS"));
            br.BaseStream.Position = 0x4;
            int num_pointers = br.ReadInt16();
            if (num_pointers == 0x56C)
            {
                MessageBox.Show("This File is not supported as it's pointer system is somehow F*cked up, please use another file, thank you.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            else
            {
            MessageBox.Show("File opened Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            pntrsmenuItem4.Visible = true;
            pntrsmenuItem4.Text += num_pointers.ToString();
            List<int> offsets = new List<int>();
            for (int i = 2; i <= (num_pointers * 2); i += 2)
            {
                br.BaseStream.Position = i * 4 + 4;
                offsets.Add(br.ReadInt32());
                //listView1.Items.Add(br.ReadUInt32().ToString("X"));
            }
            Dictionary<int, string> values = new Dictionary<int, string>();
            for (int i = 0; i < offsets.Count; i++)
            {
                int currentOffset = offsets[i];

                int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length;

                int stringLength = (nextOffset - currentOffset - 1) / 2;

                br.BaseStream.Position = currentOffset;

                var chars = br.ReadChars(stringLength);
                values.Add(currentOffset, new String(chars));
            }

            foreach (int offset in offsets)
            {
                listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]);
            }

            br.Close();
            br = null;
            }
        }
        ofd.Dispose();
        ofd = null;
    }

    private void EtxtmenuItem8_Click(object sender, EventArgs e)
    {

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Title = "Save Text File";
        sfd.DefaultExt = ".txt";
        sfd.InitialDirectory = Application.StartupPath;
        sfd.Filter = "Text Files (*.txt)|*.txt";
        DialogResult result = sfd.ShowDialog();
        if (result == DialogResult.Cancel)
            return;
        StreamWriter wwrite = new StreamWriter(sfd.FileName, false, Encoding.Unicode);
        for (int i = 0; i < listView1.Items.Count; ++i)
        {
            string name = listView1.Items[i].SubItems[1].Text;
            wwrite.WriteLine("-" + name);
        }
        wwrite.Close();
    }

    private void ItxtmenuItem4_Click(object sender, EventArgs e)
    {
        OpenFileDialog ifd = new OpenFileDialog();
        ifd.Title = "Open Text File";
        ifd.Filter = "Text Files (*.txt)|*.txt";
        ifd.InitialDirectory = Application.StartupPath;
        DialogResult result = ifd.ShowDialog();
        if (result == DialogResult.Cancel)
            return;
        StreamReader sr = new StreamReader(ifd.FileName);
        int aa = 0;
        while (sr.Peek() >= 0)
        {
            string[] a2 = sr.ReadLine().Split('-');

            if (a2.Length == 2)
            {
                aa = int.Parse(a2[0].ToString());
                listView1.Items[aa].SubItems[1].Text = a2[1].Replace("~", "\n");
            }
            else
            {
                listView1.Items[aa].SubItems[1].Text += "\n" + a2[0];
            }

        }
        sr.Close();
    }
}

Solution

  • It's a bit hard to see which Menu click corresponds to which method here, but I'll I'm guessing that the offending piece of code is msgmenuItem6_Click.

    The reason that the Dialog is showing up twice is because you call ShowDialog twice.

    DialogResult result = ofd.ShowDialog();
    if (result == DialogResult.Cancel)
        return;
    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    

    You should be doing

    if (result == System.Windows.Forms.DialogResult.OK)
    

    Regarding why you aren't able to read your file. Are you certain that there is data in the while you're trying to read? To ensure that you are opening it correctly, you can also try File.ReadAllText and make sure that it's being read in correctly.