Nicholas, this is what I have and it does work. How would I make one of
them
"default"?
Thanks,
private void OpenFile()
{
try
{
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
if (OpenFileDialog.FileName == "")
{
return;
}
string strExt;
strExt =
System.IO.Path.GetExtension(OpenFileDialog.FileName);
strExt = strExt.ToLower();
//if (strExt == ".rtf")
switch (strExt)
{
case ".rtf":
System.IO.StreamReader txtReader;
txtReader = new
System.IO.StreamReader(OpenFileDialog.FileName);
rtbDoc.Text = txtReader.ReadToEnd();
txtReader.Close();
txtReader = null;
rtbDoc.SelectionStart = 0;
rtbDoc.SelectionLength = 0;
break;
case ".dat":
rtbDoc.Text =
Streamer.LayoutInput(OpenFileDialog.FileName);
break;
case ".txt":
System.IO.StreamReader txtReader;
txtReader = new
System.IO.StreamReader(OpenFileDialog.FileName);
rtbDoc.Text = txtReader.ReadToEnd();
txtReader.Close();
txtReader = null;
rtbDoc.SelectionStart = 0;
rtbDoc.SelectionLength = 0;
break;
}
currentFile = OpenFileDialog.FileName;
rtbDoc.Modified = false;
this.Text = "Train Control Editor: " +
currentFile.ToString();
}
else
{
MessageBox.Show("Open File request cancelled by user.",
"Cancelled");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Nicholas Paldino said:
It doesn't matter if the filename is not static, you stated your
logic
is to be dependent on the extension, which is what the code I have
supplied
does. It only works on the extension. There is no first, second, or
third
order of processing. It chooses one course of action based on the
extension.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Nicholas, the default file would have an extension of .DAT, and would
be
the
routine I want to run first.
The program is an RichText editor that opens and formats the DAT file
normally, and can also open;
RTF, TXT, LOG files as needed.
The name of the DAT always begins with OCG, the rest would be dependent
on
what ever date and hour is current.
So the bottom line is that the name is not static.(rambled didn't I).
Thanks,
:
Brian,
If you have the full path to the file, then you can call the
static
GetExtension method on the Path class in the System.IO namespace to
get
the
extension. Then, you can call ToLower on it and switch on that:
// The filename.
string filename = ...;
// The extension.
string extension = Path.GetExtension(filename).ToLower();
// Switch on the extension.
switch (extension
{
case ".dat":
// Do Something
break;
case ".rtf":
// Do something else.
break;
}
You might want to put a "default" statement as well in there to
handle
cases where extension is not any of the ones you specify.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
New programmer lost!!!
I need some help with a switch/Case statement;
I need to use OpenFileDialog to open a file,
If the file extension is .dat, then do x.
If the file extension is .rtf, then do y.
if the file extension is .txt, then do z.
Thanks,
Brian