Romhacking.net

Romhacking => Programming => Topic started by: Meowanator on July 26, 2017, 10:27:29 am

Title: Help with closing file in Visual Studio
Post by: Meowanator on July 26, 2017, 10:27:29 am
Right now, if you click Cancel when opening the file, it still continues to the part where it writes to the ROM and crashes. I need it to not continue if you don't open a ROM.

Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Test_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button2.Enabled = false;
        }
        OpenFileDialog ofd = new OpenFileDialog();
        private void button1_Click(object sender, EventArgs e)
        {
            ofd.Filter = "Zelda 1 File(*.nes)|*.nes"; //Makes a filter for the open file dialog.
            ofd.ShowDialog();//Shows the open file dialog
            label1.Text = "Loaded a Rom"; //Changes label1's text.
            button1.Enabled = false;
            button2.Enabled = true;
           
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(ofd.FileName)); //Makes a new integer for the BinaryWriter. It also "tells" the BinaryWriter what file it should write.
            for (int x = 0x18611; x <= 0x18611; x++) //Makes another integer that holds the first and the last offset you want the BinaryWriter to change.
            {

                bw.BaseStream.Position = x;
                bw.Write(0x3F); //0x3F - Swordless


            }
            bw.Close();
            label1.Text = "Removed Sword"; //Changes label1's text.
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
Title: Re: Help with closing file in Visual Studio
Post by: Lenophis on July 26, 2017, 12:55:16 pm
Your OpenFileDialog has no check to see if opening the file succeeded or not. In other words, it's always continuing as though it succeeded when it may not have.
Something like this may work:
Code: [Select]
if(!OpenFileDialog()) return;
else
// your block of code that continues