Filtering text files in C# - c#

How do I just open files with the .txt extension, I want to my program to pop up an error message if the file is not a .txt file I want a code that can modify this code below
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
Can someone help let's say I want to put this loop
if fileextension is .txt then
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
else show error message(like can not open this file)

As I understood correctly, you want to see only txt files in your dialog?
If so, use Filter property.
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Text files (*.txt)|*.txt";

Can use Path.GetExtension method for this
OpenFileDialog of = new OpenFileDialog();
if(of.ShowDialog() == DialogResult.OK)
{
if(Path.GetExtension(of.FileName).Equals("txt",
StringComparison.InvariantCultureIgnoreCase))
textBox1.Text = of.FileName;
}

You shouldn't allow all extensions if you only allow txt extension.
of.Filter = "Text Files|*.txt";
Will make the OpenFileDialog accept only txt extension files.

Related

How to use FileDialog to select either specific files names extensions or all the files?

private void button1_Click(object sender, EventArgs e)
{
VistaOpenFileDialog dialog = new VistaOpenFileDialog();
{
dialog.Filter = "Images (*.jpg, *.bmp, *.gif)|*.jpg;*.bmp;*.gif";
dialog.Filter = "All Files (*.*)|*.*";
};
if (dialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = dialog.FileName;
Bitmap bmp = new Bitmap(Image.FromFile(dialog.FileName),
pictureBox2.Width, pictureBox2.Height);
pictureBox2.Image = bmp;
}
}
I'm using ookii package with VistaOpenFileDialog but i guess the idea should be the same as with the regular OpenFileDialog class.
now when i click it's showing the All Files option when i expand it i can't see the first option Images.
i want that it will show first the Images option and then to be able to switch the All Files.
You are overwriting your filter property. To add multiple file type filters:
dialog.Filter = "Images (*.jpg, *.bmp, *.gif)|*.jpg;*.bmp;*.gif|All Files (*.*)|*.*";

Open Excel File using OpenFileDialog in C# Windows Form

I am able to choose an Excel file, but after I clicked on Open, the excel file doesn't appear. What should I do? I'm still new with OpenFileDialog, it will be good if anyone can tell what I should add to make the excel file appear after clicking on Open.
Modified from http://www.c-sharpcorner.com/uploadfile/mahesh/openfiledialog-in-c-sharp/
This is my code:
private void BrowseButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = #"C:\";
openFileDialog1.Title = "Browse Text Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
int size = text.Length;
}
catch (IOException)
{
}
}
}
public bool ThumbnailCallback()
{
return false;
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
After I clicked Open, only the file name appear, but not the excel file - https://i.stack.imgur.com/GXToy.jpg
You need to set the filter to select excel files.
openFileDialog1.Filter = "Excel Worksheets|*.xls";
You can refer the documentation here.
If you only want to open the Excel file using the default application that is associated to *.xlsx files (which usually is MS Excel when it is installed), then you can simply use the Process.Start(string) method. In you case it may look something like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Process.Start(openFileDialog1.FileName);
}

Beginners help C# showing a filepath in text box

I'm new to programming and am using C# and Visual Studio Express 2012. I am creating a windows form and have inserted a button which runs open file dialog when clicked. I have a text box on the form that I'd like to have show the file path of the file that the user selected. I have found some code examples on this site but struggle to understand where they should be placed in the code structure as the examples are often standalone snippets. I hope its not too dumb a question!
Thanks in advance
Lee
The answer in case it's of use to anyone was.......
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (FileDialog fileDialog = new OpenFileDialog())
{
if (DialogResult.OK == fileDialog.ShowDialog())
{
string filename = fileDialog.FileName;
textBox1.Text = fileDialog.FileName;
}
}
}
}
Your OpenFileDialog has property FileName that contains the path of the selected file, assign that to your TextBox.Text
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
yourTextBox.Text = openFileDialog.FileName;
}
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
textbox.text = openFileDialog1.FileName;

How to make a duplicate copy of the file selected in the OpenFileDialog control

// Browses file with OpenFileDialog control
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
}
}
In the code above, i browse for a file to open. What I want to do is, browse for a file, select it and then press ok. On clicking ok, i want to make a copy of the seleted file and give that duplicate file a .txt extension. I need help on achieving this.
Thanks
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
var fileName = openFileDialogCSV.FileName;
System.IO.File.Copy( fileName ,Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName)+".txt"));
}
Above code will copy selected file as txt with same name and in to same directory.
if you need to overwrite existing file with same name add another parameter to Copy method as true.
System.IO.File.Copy(source, destination, true);
You use File.Copy as follows,
File.Copy(openFileDialogCSV.FileName., openFileDialogCSV.FileName + ".txt");
Try this
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
System.IO.File.Copy(this.txtFileToImport.Text,"C://123.txt")
}
}
123 can be changed by any file name that you want.

Add prompt after user selects file

I have added an open file dialog box to my client application so that the used can select a specific file they want to send to the web service.
However the file gets sent the moment the file has been selected, whereas I would like to have a secondary prompt e.g. "Send - 'file name' Button Yes. Button No." to pop up after they have selected the file.
This would be incase the user selected the wrong file they would have a chance to see which one they selected.
So far I have the following code -
private void button1_Click(object sender, EventArgs e)
{
//Read txt File
openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader myReader = new StreamReader(openFileDialog1.FileName);
myReader.Close();
string csv = File.ReadAllText(openFileDialog1.FileName);
I need the prompt to come up after they have selected the file but not sure how to do this so any input would be greatly appreciated.
you need to add the second check manually after the first dialog:
private void button1_Click(object sender, EventArgs e)
{
//Read txt File
openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (MessageBox.Show("Message", "Title",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
StreamReader myReader = new StreamReader(openFileDialog1.FileName);
myReader.Close();
string csv = File.ReadAllText(openFileDialog1.FileName);
etc etc
Information on MessageBox.Show. You can get information on the possible results/options from here.
You could ensure that the user sees the file to be uploaded by making the message something like:
"Are you sure you want to upload " + openFileDialog1.FileName;
MessageBox.Show(...) is the method you're looking for.
You can use a message box:
if (MessageBox.Show(string.Format("Upload {0}, are you sure?", openFileDialog1.FileName), "Please Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// ...
}
A sample of the code modified.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
DialogResult dr = MessageBox.Show(message, caption, MessageBoxButtons.YesNo);
if(dr == DialogResult.Yes )
StreamReader myReader = new StreamReader(openFileDialog1.FileName);
// more code
else
// do something else

Categories

Resources