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);
Related
I am trying to build a small .pdf -> .txt / searchable .pdf converter, but I am having trouble to assign the first var result to the other buttons
Made myself a "solution" but the code seems too messed and exagerated.
using IronOcr;
using System;
using System.IO;
namespace ocr
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
var Ocr = new IronTesseract(); // nothing to configure
IronOcr.License.LicenseKey = "SOMELICENSEKEY";
using (var Input = new OcrInput())
{
Input.AddPdf(ofd.FileName, "password");
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
richTextBox1.Text = Result.Text;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
var Ocr = new IronTesseract(); // nothing to configure
IronOcr.License.LicenseKey = "SOMELICENSEKEY";
using (var Input = new OcrInput())
{
Input.AddPdf(ofd.FileName, "password");
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
Result.SaveAsTextFile("pdf.txt");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
var Ocr = new IronTesseract(); // nothing to configure
IronOcr.License.LicenseKey = "SOMELICENSEKEY";
using (var Input = new OcrInput())
{
Input.AddPdf(ofd.FileName, "password");
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
Result.SaveAsSearchablePdf("pdfpesquisavel.pdf");
}
}
}
}
}
Tried to assign and use the variable "Result" in the others buttons fuctions (button 2 and button 3)
But it didn't worked.
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
private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select Song";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var Path = fdlg.FileName;
var pi = new System.Diagnostics.ProcessStartInfo(Path)
{
Arguments = Path.GetFileName(Path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(Path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
}
As you can see I am trying to select a file and use that file to run another program but I don't want to use windows default.
My error is in
var Path = fdlg.FileName;
The problem is fdlg isnt in the same context and i need to use the directory chosen from before, any ideas on how to do this?
From the answers I have tried this:
private OpenFileDialog fdlg = new OpenFileDialog();
private void browsebtn_Click(object sender, EventArgs e)
{
fdlg.Title = "Select Song";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var Path = fdlg.Text;
var pi = new System.Diagnostics.ProcessStartInfo(Path)
{
Arguments = Path.GetFileName(Path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(Path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
and I get an error saying 'OpenFileDialog' doesnt have a definition or extension method for 'Text' and with the line in the class:
private OpenFileDialog fdlg = new OpenFileDialog();
I get a new error saying that 'GetFileName' and 'GetDirectoryName' don't have definitions or extension methods in the 'string'
EDIT
I renamed path to directory but same error
private void runbtn_Click(object sender, EventArgs e)
{
var directory =filedialoglbl.Text;
var pi = new System.Diagnostics.ProcessStartInfo(directory)
{
Arguments = directory.GetFileName(directory),
UseShellExecute = true,
WorkingDirectory = directory.GetDirectoryName(directory),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
You are using the dialog in your runbtn_Click method only for the FileName. You saved it into a label called filedialoglbl.Text. Use it.
private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select Song";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var path = filedialoglbl.Text;
var pi = new System.Diagnostics.ProcessStartInfo(path)
{
Arguments = Path.GetFileName(path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
Edit
You can't use Path with uppercase because your are shawoding the Path class and you can't use their methods.
Declare OpenFileDialog fdlg outside of your methods as a class member (field), e.g.
private OpenFileDialog fdlg = new OpenFileDialog();
In my app, the user can browse and select a text file. I'm saving the path like this:
private void nacitanie_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Otvoriť Textový súbor.";
dialog.Filter = "TXT files|*.txt";
dialog.InitialDirectory = #"C:\";
if (dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.FileName;
}
}
Then I need to work with that path in other buttons. How can I return the path of the text file from the button handler method?
You should create path outside nacitanie_Click:
class SomeClass
{
private string path;
.....
private void nacitanie_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Otvoriť Textový súbor.";
dialog.Filter = "TXT files|*.txt";
dialog.InitialDirectory = #"C:\";
if (dialog.ShowDialog() == DialogResult.OK)
{
path = dialog.FileName;
}
}
....
}
and the use it in another methods/handlers.
You need to declare a variable outside of the button handler method:
private string path = string.Empty;
private void nacitanie_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Otvoriť Textový súbor.";
dialog.Filter = "TXT files|*.txt";
dialog.InitialDirectory = #"C:\";
if (dialog.ShowDialog() == DialogResult.OK)
{
path = dialog.FileName;
}
}
That way, you'll be able to access the variable from other button handlers.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog newOpen = new OpenFileDialog();
DialogResult result = newOpen.ShowDialog();
this.textBox1.Text = result + "";
}
It just returns "OK"
What am I doing wrong? I wish to get the PATH to the file and display it in a text box.
The ShowDialog method returns whether the user pressed OK or Cancel. This is useful information, but the actual filename is stored as a property on the dialog
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog newOpen = new OpenFileDialog();
DialogResult result = newOpen.ShowDialog();
if(result == DialogResult.OK) {
this.textBox1.Text = newOpen.FileName;
}
}
You need to access the filename:
string filename = newOpen.FileName;
or filenames, if you allowed multiple file selection:
newOpen.FileNames;
Ref.: OpenFileDialog Class
private void button1_Click(object sender, System.EventArgs e) {
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file. Error: " + ex.Message);
}
}
}
You need to read the FileName property of the OpenFileDialog instance. This will get you the path of the selected file.
Here is an example of using an existing file as a default, and getting a new file back:
private string open(string oldFile)
{
OpenFileDialog newOpen = new OpenFileDialog();
if (!string.IsNullOrEmpty(oldFile))
{
newOpen.InitialDirectory = Path.GetDirectoryName(oldFile);
newOpen.FileName = Path.GetFileName(oldFile);
}
newOpen.Filter = "eXtensible Markup Language File (*.xml) |*.xml"; //Optional filter
DialogResult result = newOpen.ShowDialog();
if(result == DialogResult.OK) {
return newOpen.FileName;
}
return string.Empty;
}
Path.GetDirectoryName(file) : Return path
Path.GetFileName(file) : Return filename