I'm trying to make it so when i click my button I can chose which .txt file i want to select and than get the Lines Count displayed on my label
string filePath = "C:\\Users\\Ristic\\Desktop\\Neki.txt";
int count = System.IO.File.ReadLines(filePath).Count();
string a = count.ToString();
label1.Text = a;
You need to use OpenFileDialog here.
For example,
var openFileDialog1 = new OpenFileDialog()
{
DefaultExt = "txt",
Filter = "txt files (*.txt)|*.txt",
}
openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog1.FileName;
}
The OpenFileDialog.Filter property allows you to filter the files by extension, which in your case is ".txt".
You can read more on OpenFileDialog here
Related
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);
}
// 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.
In the following c# class displaying a Windows.Forms.OpenFileDialog how could I store the data path into the variable called m_settings?
private SomeKindOfData m_settings;
public void ShowSettingsGui()
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
string[] filePath = ofd.FileNames;
string[] safeFilePath = ofd.SafeFileNames;
}
m_settings = //<-- ?
}
This should do the trick:
m_settings = ofd.FileName;
EDIT: Actually, now I'm not sure if you wanted the folder path. In that case:
m_settings = Path.GetDirectoryName(ofd.FileName);
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.
Here is my snippet:
private void btnBrowseCInv_Click(object sender, EventArgs e)
{
ofdBrowseVInv.Title = "Locate Customer Invoice File";
ofdBrowseVInv.Filter = "Portable Document Format (*.pdf)|*.pdf|All Files (*.*)|*.*";
ofdBrowseVInv.FileName = "";
ofdBrowseVInv.FilterIndex = 0;
ofdBrowseVInv.InitialDirectory = "";
ofdBrowseVInv.CheckFileExists = true;
ofdBrowseVInv.CheckPathExists = true;
if (ofdBrowseVInv.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//txtInvoicePathCInv.Text = ofdBrowseVInv... What property should i use?
}
}
As you see below, once a user pick a file and click open. I want the selected path to show on the pointed text box which is named "txtInvoicePathCInv". Any idea?
I'm using Windows Application...
alt text http://img708.imageshack.us/img708/54/99763211.jpg
Use the FileName property:
txtInvoicePathCInv.Text = ofdBrowseVInv.FileName;
This will give you the whole path, but you can always just use the directory part of it, using Path.GetDirectoryName:
txtInvoicePathCInv.Text = Path.GetDirectoryName(ofdBrowseVInv.FileName);
string filename = System.IO.Path.GetFileName(ofdBrowseVInv.FileName);
string path = System.IO.Path.GetDirectoryName(ofdBrowseVInv.FileName);