|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace RunVistaGadget21
{
class FilePathDeterminer
{
private String FolderDialogDescription;
private String FileName;
private String PreviousFilePath;
private FileOpenDialogType FODT;
public FilePathDeterminer(string folderDialogDescription, string defaultFileName, string prevFilePath, FileOpenDialogType fodt)
{
FolderDialogDescription = folderDialogDescription;
FileName = defaultFileName;
PreviousFilePath = prevFilePath;
FODT = fodt;
}
public string GetFilePath()
{
string FilePath = GetSelectedPath();
if (!String.IsNullOrEmpty(FilePath))
{
string PrevFileName = TryGetPreviousFileName();
if (!String.IsNullOrEmpty(PrevFileName))
{
FileName = PrevFileName;
}
if (FODT == FileOpenDialogType.FolderBrowserDialog)
{
if (Path.GetPathRoot(FilePath) != FilePath)
{
FilePath += System.IO.Path.DirectorySeparatorChar;
}
FilePath += FileName;
}
}
return FilePath;
}
private string TryGetPreviousFileName()
{
string PreviousGadgetName = string.Empty;
if (!String.IsNullOrEmpty(PreviousFilePath))
{
PreviousGadgetName = System.IO.Path.GetFileName(PreviousFilePath);
}
return PreviousGadgetName;
}
private string GetSelectedPath()
{
string SelectedPath = String.Empty;
bool ThisDialogResult = false;
if (FODT == FileOpenDialogType.FolderBrowserDialog)
{
FolderBrowserDialog Dialog = new FolderBrowserDialog();
Dialog.ShowNewFolderButton = true;
Dialog.Description = FolderDialogDescription;
ThisDialogResult = (Dialog.ShowDialog() == DialogResult.OK);
SelectedPath = Dialog.SelectedPath;
}
else if (FODT == FileOpenDialogType.OpenFileDialog)
{
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = FolderDialogDescription;
Dialog.Filter = FileName + "(" + FileName + ")|" + FileName + "|All files (*.*)|*.*";
ThisDialogResult = (Dialog.ShowDialog() == DialogResult.OK);
SelectedPath = Dialog.FileName;
}
return
(ThisDialogResult == true) ? SelectedPath : string.Empty;
}
public enum FileOpenDialogType
{
FolderBrowserDialog,
OpenFileDialog
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.