Thursday, December 23, 2010

C# FileSystemDialog

Now that you know how to read from and write to the hard drive (or if you don't, see C# Read/Write From File), we need a way to get the name of the file to be loaded from or saved to.  You could use my C# InputBox, but it's not as professional-looking as this C# FileSystemDialog.

I created this class, which takes two classes already made and puts it into one.  It uses the SaveFileDialog and the OpenFileDialog.  If you don't remember how to add a class to your project, see (C# InputBox).

The use of this class is pretty self explanatory.

To get the name of a save file, use:
FileSystemDialog.SaveDialog();

It gives you the name of the file or an empty string (as in "") if the user presses cancel.

To get the name of a load file, use:
FileSystemDialog.LoadDialog();

It gives you the name of the file or an empty string if the user presses cancel.

If you want to make an absolute path into a relative path (from the .EXE file), use:
FileSystemDialog.MakePathRelative("full_path");

Obviously you have to replace "full_path" with a string containing the path that you want to make relative.

Finally, after adding this class to your project, you should change the properties (i.e. DefaultExt, Filter, etc.) to the stuff that you want, such as a unique file extension for your save files.  In the init() method, Change all the instances of "clk" to your file extension of choice and "Click file" to the name of your type of file.  The Title is whatever you want to be displayed at the top of the respective dialog windows.

Here's the code:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


static class FileSystemDialog
{


private static SaveFileDialog saver;
private static OpenFileDialog loader;


// for the first time the FileSystemDialog methods are called
// change the strings in this method to fit your needs
private static void init()
{
saver = new SaveFileDialog();
loader = new OpenFileDialog();


saver.DefaultExt = "clk";
saver.Filter = "Click file (*.clk)|*.clk";
saver.AddExtension = true;
saver.RestoreDirectory = true;
saver.Title = "Save As...";
saver.InitialDirectory = Directory.GetCurrentDirectory();


loader.DefaultExt = "clk";
loader.Filter = "Click file (*.clk)|*.clk";
loader.AddExtension = true;
loader.RestoreDirectory = true;
loader.Title = "Open...";
loader.InitialDirectory = Directory.GetCurrentDirectory();
}


public static string SaveDialog()
{
if (saver == null)    // only when first time
{
init();
}


saver.ShowDialog();
return saver.FileName;    // return empty string ("") if cancel button is pressed
}


public static string LoadDialog()
{
if (loader == null)    // only when first time
{
init();
}


loader.ShowDialog();
return loader.FileName;    // return empty string ("") if cancel button is pressed
}


public static string MakePathRelative(string path)
{
return path.Replace(Directory.GetCurrentDirectory() + "\\", "");
}
}

As always, you can download it here ("FileSystemDialog.cs").

No comments:

Post a Comment