News:

11 March 2016 - Forum Rules

Main Menu

Help with closing file in Visual Studio

Started by Meowanator, July 26, 2017, 10:27:29 AM

Previous topic - Next topic

Meowanator

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.

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)
        {

        }
    }
}

Lenophis

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:

if(!OpenFileDialog()) return;
else
// your block of code that continues


https://ff6randomizer.codeplex.com/ - Randomize your FF6 experience!