I am fairly new to c#.
My question is what is strFileName in open file dialog?
I have this code currently:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It brings up an error on strFileName. i cant find an explanation as to what it does in this code.
Any help will be appreciated and i apologise if this question has been asked before.
Without knowing what the error is, just by looking at your code, you are probably getting a compile error on strFileName because it is not declared:
You can either change your code to this:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
input = open.FileName;
if (input == String.Empty)
return;
Or this:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It doesn't give me an error - though I did have to declare it. Did you?
string strFileName = ""; // added this line - it compiles and runs ok
private void TestFunc()
{
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
}
you need declare strFileName first
string strFileName = string.empty();
then use it.
You need to declare the variable strFilename as a string:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
{
strFileName = open.FileName;
}
/* you probably don't want this unless it's part of a larger block of code
if (strFileName == String.Empty)
{
return;
}
*/
return strFileName;
Related
Hello all I wanted to ask how I can add function to button in dataGridCell, so when click button it open file explorer, I select file and it return file path in other cell.
The function I want add under button of datagrid cell "Browse":
public string test5()
{
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\users\\Desktop";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
}
return filePath;
}
}
then I wrote under button load data with this button function I want use above:
private void button1_Click(object sender, EventArgs e)
{
//xDxD();
//xDxD2();
//textBox1.Text = xDxD2();
//this.dataGridView1.Rows.Add("five", "six", "seven", "eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", test5());
}
But when I click button1, then it auto start function from this cell.
Sorry for my english.
You were very close to succeeding
If this code is not helpful you should check this out: Extracting Path from OpenFileDialog path/filename
But here is my code:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\users\\Desktop";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var filePath = Path.GetDirectoryName(openFileDialog.FileName);
textBox1.Text = filePath;
}
Hope you found what you need!
Is there a way to set filenames from OpenFileDialog through code?
string[] files = {"D:\\test1.txt","D:\\test2.txt"};
System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
dialog.Multiselect = true;
dialog.FileNames = files;
Error while trying to set the FileNames:
Error CS0200 Property or indexer 'FileDialog.FileNames' cannot be assigned to -- it is read only
Actually, you can use InitialDirectory to set the default address
openFileDialog.InitialDirectory = "D:\\";
and use openFileDialog.Filter to set specific type
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
and you can use openFileDialog1.FileName to select a file
openFileDialog1.FileName = "test1"
or muliplefile
openFileDialog1.FileName = "\"test1\" \"test2\"";
For example:
string message = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.FileName = "\"test1\"\"test2\"";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (string file in openFileDialog1.FileNames)
{
message += Path.GetFileName(file) + " - " + file + Environment.NewLine;
}
MessageBox.Show(message);
}
Iam using VS2015 - Windows Forms. When I click my Browse Button the OpenFileDialog Works good. But Suppose once I Re-click the button after to refresh the form data's, the OpenFileDialog simply hang-up.
I can't understand my problem.. Any of the superiors can guide me?
MyFileNameStr = String.Empty;
openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "(*.xlsx)|*.xls| All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "Select Your Attachment File :- ";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFileDialog1.FileName.Length>0) {
String MyDrawingFile = Path.GetFileName(openFileDialog1.FileName);
myDataGrid1.CurrentRow.Cells["MyExcel_file"].Value = Path.GetFileName(openFileDialog1.FileName);
MyFileNameStr = openFileDialog1.SafeFileName.ToString();
MyFileNameStrs = openFileDialog1.SafeFileName.ToString().Split('_');
}
Thanks Again
I have added the few more below codes for file descriptions, then it works good.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Title = "Select Your Attachment File :- ";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.Filter = "exe files | *.exe|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
MyDrawingFile = System.IO.Path.GetFileName(openFileDialog1.FileName).ToString();
MyFileNameStr = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName).ToString();
}
Thanks
this is due to
if you click on button and browse a file then the process is running on your excel file.
if you again click on button the process is busy with your excel file and app will get hang.
I am trying to make a try catch that will check if the file in filelocation exists or not.
If it doesnt i want the open file dialog to open and allow user to be able choose the text file (and only a text file) and read the file location of the selected file and use that as the file location sting for the sream reader
any time i use this the file location will be right until the end. instead of the file name it will have bin/debug/ok
try
{
if (!File.Exists(filelocation))
{
throw new FileNotFoundException();
}
else
{
StreamReader question = new StreamReader(filelocation);
}
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("File containing the questions not found");
OpenFileDialog OFD = new OpenFileDialog();
DialogResult result = OFD.ShowDialog();
string filelocation = result.ToString();
StreamReader question = new StreamReader(filelocation);
}
add this
OFD .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
the result will be:
try
{
if (!File.Exists(filelocation))
{
throw new FileNotFoundException();
}
else
{
StreamReader question = new StreamReader(filelocation);
}
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("File containing the questions not found");
OpenFileDialog OFD = new OpenFileDialog();
OFD .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
DialogResult result = OFD.ShowDialog();
string filelocation = result.ToString();
StreamReader question = new StreamReader(filelocation);
}
This takes into account whether User did not select a File from the File Dialog or whether invalid file is selected...
StreamReader ReadME;
if (!File.Exists(termfilelocation))
{
MessageBox.Show("File containing the questions not found");
OpenFileDialog OFD = new OpenFileDialog();
OFD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
// this will filter out any file that isnt a text file
ofd.CheckFileExists = true;//this will not allow invalid files.
DialogResult dr = OFD.ShowDialog();
if (dr == DialogResult.Cancel)
{
//User did not select a file.
return;
}
String result = OFD.FileName;
ReadME = new StreamReader(result);
termfilelocation = result;
}
else
{
ReadME = new StreamReader(termfilelocation);
}
I fixed it myslef so anybody having similar problems heres how to fix it
try
{
if (!File.Exists(termfilelocation))
{
throw new FileNotFoundException();
}
else
{
StreamReader reader = new StreamReader(termfilelocation);
}
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("File containing the questions not found");
// this will display a message box sying whats in ("")
OpenFileDialog OFD = new OpenFileDialog();
// this will create a new open file dialog box
OFD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
// this will filter out any file that isnt a text file
DialogResult = OFD.ShowDialog();
String result = OFD.FileName;
// this will give the result to be the file name
// that was choosen in the open file dialog box
StreamReader reader = new StreamReader(result);
termfilelocation = result;
}
In my case the SaveFileDialog will not write any file, but I want to use to specify the path for a command line app which will create the logfile on the same location as "saved" in the sf dialog.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
Path.GetFileName(sfd.FileName);
}
startInfo.Arguments = "--log=" + Path.GetFileName(sfd.FileName);
You can use
Path.GetFullPath(sfd.FileName);
Instead of
Path.GetFileName(sfd.FileName);
Complete version...
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
Path.GetFullPath(sfd.FileName);
}
startInfo.Arguments = "--log=" + Path.GetFullPath(sfd.FileName);
Just remove the Path.GetFileName:
startInfo.Arguments = "--log=\"" + sfd.FileName + "\"";
I think you are using the wrong dialog form based on what you are describing.
Try using the FolderBrowserDialog class:
string folderPath = string.Empty;
using (FolderBrowserDialog fdb = new FolderBrowserDialog()) {
if (fdb.ShowDialog() == DialogResult.OK ){
folderPath = fdb.SelectedPath;
}
}
if (folderPath != string.Empty) {
startInfo.Arguments = "--log=" + folderPath;
}
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Image files | *.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
employee_dp.Image = Image.FromFile(openFileDialog1.FileName);
string path = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
string onlyFileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
filepath = Path.GetFullPath(path).Replace(#"\", #"\\");
filepath = filepath + "\\\\" + onlyFileName;
MessageBox.Show(filepath);
Perhaps Path.GetFullPath would help?
The problem might be using the wrong FileSaveDialog.
The one in Win32.dll doesn't provide the full path, but the one in System.Windows.Forms does.