AllTheTimeWorld.com

csharp variablesUsing the local directory

Using the local directory

This shows how to store pictures or other files in the local directory

Using the local directory

This shows how to store pictures or other files in the local directory


Text of video
//Programmer: J Joy
//Display pictures from the local directory
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;

namespace Cat_Mouse
{
  
    public partial class Form1 : Form
    {
        String localDirectory = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); 
        // For my program this is c:\MyCsharp\Cat-Mouse\bin\debug
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Display the local directory for debugging purposes
            this.Text = localDirectory;
        }

        private void btnCat_Click(object sender, EventArgs e)
        {
            //Display the image of the cat in the picture box
            this.Text = localDirectory + "/cat.gif";
            //The slash is important! Without it it would be c:\MyCsharp\Cat-Mouse\bin\debugcat.gif
            picAnimal.Image=  Image.FromFile(localDirectory + "/cat.gif");
        }

        private void btnMouse_Click(object sender, EventArgs e)
        {
            //Display the image of the mouse in the picture box
            //This is the same as above except we use a variable for "mouse"
            String pic="mouse";
            this.Text = localDirectory + "/" + pic + ".gif";
            // We need to concatenate all the parts of the filename
            picAnimal.Image = Image.FromFile(localDirectory + "/" + pic+ ".gif");
        }
       
    }
}