Add prompt after user selects file - c#

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

Related

How to close a text file that is opened through openDialogBox?

I have created a button that opens a text file through openDialogBox1 and display the content in a listBox. Then I wanted to create another button to close this file, but I couldn't figure out what code to use. Can anyone help me please? Thank you.
Here is a sample of my openFile button code :
private void openButton_Click(object sender, EventArgs e)
{
try
{
StreamReader inputFile;
string File;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Text Files (*txt)|*txt";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
inputFile = File.OpenText(openFileDialog1.FileName);
fileListBox.Items.Clear();
while (!inputFile.EndOfStream)
{
File = inputFile.ReadLine();
fileListBox.Items.Add(File);
}
inputFile.Close();
}
}
catch
{
MessageBox.Show("Invalid File!");
}
}
Declare StreamReader inputFile; globally so it is available in the second button click.
However, the file should be opened and closed as soon as you are done with it to release resources. Plus, you don't know, the second button may never get clicked.

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);
}

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.

Filtering text files in 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.

How to open save file dialog when i click on ok of a Message Box

I would like to prompt the user for a save file dialog when he clicks on Ok of a message box displayed. How can i do this...
In the event handler of the button, use the following code.
DialogResult messageResult = MessageBox.Show("Save this file?", "Save", MessageBoxButtons.OKCancel);
if (messageResult == DialogResult.OK)
{
using (var dialog = new System.Windows.Forms.SaveFileDialog())
{
dialog.DefaultExt = "*.txt";
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
string filename = dialog.FileName;
// Save here
}
}
}
Edit: If you want to get a FileStream directly you can use SaveFileDialog.OpenFile(). This requires less permissions if you run your application in partial trust.
You could google this. If you have problem with click event. I guess you are using visual studio just double click the button on the design surface and write your code inside the handler that you go.
http://www.jonasjohn.de/snippets/csharp/save-file-dialog-example.htm
MessageBox.Show() returns DialogResult
DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
"Important Question",
MessageBoxButtons.OK);
if (result1 == DialogResult.OK)
{ //Show SaveFileDialog }
private void button1_Click(object sender, EventArgs e)
{
textBox1.Enabled=false;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excell File |*.xlsx;*,xlsx";
if (ofd.ShowDialog() == DialogResult.OK)
{
string extn = Path.GetExtension(ofd.FileName);
if (extn.Equals(".xls") || extn.Equals(".xlsx"))
{
filename = ofd.FileName;
if (filename != "")
{
try
{
string excelfilename = Path.GetFileName(filename);
}
catch (Exception ew)
{
MessageBox.Show("Errror:" + ew.ToString());
}
}
}
}
I got the answer this was what i am asking for
MessageBox.Show("Save The Current File");
if (Convert.ToBoolean( DialogResult.OK ))
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = #"C:\";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
string s= saveFileDialog1.FileName;
}

Categories

Resources