-1

我有两个问题: 1- 当我单击打开按钮时,它会显示两次openfiledialog(当我选择我的文件并单击确定时,它会再次重新打开选择窗口,只重复一次)。2-我正在尝试从列表视图中导出和导入文本文件,到目前为止,我设法从列表视图中导出了一个文本文件,但我无法将其重新导入。

这是我的代码(对于这两种情况,因为它是同一个项目):

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();
    }
}
4

1 回答 1

1

在这里很难看出哪个菜单单击对应于哪个方法,但我猜有问题的代码是 msgmenuItem6_Click。

Dialog 出现两次的原因是因为您调用 ShowDialog 两次。

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

你应该做

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

关于您无法读取文件的原因。您确定在您尝试读取时有数据吗?为确保您正确打开它,您还可以尝试 File.ReadAllText 并确保它被正确读取。

于 2012-09-22T16:45:05.230 回答