In my WpfAPP I use a button to select file to read the fileName the code is
private void Select_File(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "|*.csv;*.xls;*.xlsx",
Title = "Read File",
Multiselect = false
};
bool? res = ofd.ShowDialog();
if (res != null && res.Value)
{
Debug.WriteLine("==========================");
string strOpenFile = ofd.FileName;
Debug.WriteLine(strOpenFile);
}
}
but the fileName is ""
How to read the fileName successfully?
your filter syntax is wrong. Try this
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "CSV files (*.csv)|*.csv|Excel (*.xls)|*.xls";
ofd.Title = "Read File";
ofd.Multiselect = false;
bool? res = ofd.ShowDialog();
if (res != null && res.Value)
{
Debug.WriteLine("==========================");
string strOpenFile = ofd.FileName;
Debug.WriteLine(strOpenFile);
}
Related
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
MessageBox.Show("Files Selected");
foreach(string filename in ofd.FileNames)
{
}
}
}
in this situation, i just wanna kill process which is selected by OpenFileDialog so that I can't use selectedprogram during this code is running... help me plz
As you can see from comments, the solution is partial, but you could search if among the running processes there are some with the same name as the selected files
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "All files(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
MessageBox.Show("Files Selected");
foreach (string filename in ofd.FileNames)
{
var proc = System.Diagnostics.Process.GetProcesses().Where(w => w.ProcessName == System.IO.Path.GetFileNameWithoutExtension(filename)).FirstOrDefault();
if (proc != null)
proc.Kill(); // proc.Kill(true); to kill entire process tree
}
}
At the moment I have three buttons on a form, each opens a different form (form2 with a textbox to display the text from the textfile, form3 with a picturebox to display the image)
What I am trying to do is put the two together for my last button so the user can filter which type to open (TXT Files or Image files). I am not sure how I can put the two together and get them to work.
The code I used to just open text files:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = #"C:\";
ofd.Filter = "TXT Files(*.txt;)|*.txt;";
if(ofd.ShowDialog() == DialogResult.OK)
{
using(StreamReader rdText = new StreamReader(ofd.FileName))
{
string info = File.ReadAllText(ofd.FileName);
TextDocumentForm newTextDocument = new TextDocumentForm();
newTextDocument.TextFileName = info;
newTextDocument.Show();
}
}
}
What I use to open my image files
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofdi = new OpenFileDialog();
ofdi.InitialDirectory = #"C:\";
ofdi.Filter = "Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;";
if (ofdi.ShowDialog() == DialogResult.OK)
{
Image image = Image.FromFile(ofdi.FileName);
ImgDoc newImageDoc = new ImgDocumentForm();
newImageDoc.ImageShow = image;
newImageDoc.Show();
}
}
Any help is appreciated as I am trying to develop my understanding of how OpenFileDialog still works.
Combining filters:
var openFile = new OpenFileDialog
{
InitialDirectory = #"C:\",
Filter = "TXT Files(*.txt;)|*.txt;|Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;"
};
Then use Path.GetExtension() to see which route you should take:
if (openFile.ShowDialog() == true)
{
var ext = System.IO.Path.GetExtension(openFile.FileName);
if (ext == ".txt")
{
// Open text file
}
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".bmp")
{
// Open image file
}
}
I tried complete my program, with this code:
private void btnBuscar_Click(object sender, RoutedEventArgs e)
{
FileInfo f = null;
OpenFileDialog dlg;
dlg = new OpenFileDialog();
dlg.Filter = "Document Files (*.doc;*pdf)|*.doc;*pdf";
dlg.InitialDirectory = Environment.SpecialFolder.UserProfile.ToString();
dlg.Title = "Seleccione su archivo de cotizaciĆ³n.";
//Open the Pop-Up Window to select the file
bool? result = dlg.ShowDialog();
if (result == true)
{
f = new FileInfo(dlg.FileName,);
using (Stream s = dlg.OpenFile())
{
TextReader reader = new StreamReader(s);
string st = reader.ReadToEnd();
txtPath.Text = dlg.FileName;
}
File.Copy(dlg.FileName,#"C:\Formatos\m1");
}
}
The problem is when select the file with OpenFileDialog the program Crash automatically. I need copy the files only, and save the path of the copy file in the DB.
Thank you for you help!
Change bool? result = dlg.ShowDialog(); to if (dlg.ShowDialog() == DialogResult.OK)
Also, remove the extra comma from f = new FileInfo(dlg.FileName,);
so what I'm trying to do is load a .txt file and once the .txt file is loaded it will show the contents from the .txt file in a listView.
Here is my load code.
List<String> proxies = new List<string>();
private void loadProxiesToolStripMenuItem_Click(object sender, EventArgs e)
{
loadProxies();
}
private void loadProxies()
{
this.Invoke(new MethodInvoker(delegate
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT files|*.txt";
ofd.Title = "Load Proxies";
var dialogResult = ofd.ShowDialog();
if (dialogResult == DialogResult.OK)
{
proxies = new List<string>();
Parallel.ForEach(System.IO.File.ReadLines(ofd.FileName), (line, _, lineNumber) =>
{
if (line.Contains(":"))
{
//loadedCombo.Add(line);
proxies.Add(line);
}
else
{
//MessageBox.Show("Hmm, thats not a combolist - please try again");
}
});
}
txt_proxies.Text = "Proxies Loaded: " + proxies.Count.ToString();
}));
}
and I'm wanting it to show in the listView which is named "proxyView".
So what I'm trying to say it, I can get the .txt to load and it changes the count but it's not adding the contents from the .txt file into the listview.
Thanks much appreciated.
To add an item to a ListView you can use yourListView.Items.Add(text)
For example:
private void loadProxies()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT files|*.txt";
ofd.Title = "Load Proxies";
var dialogResult = ofd.ShowDialog();
if (dialogResult == DialogResult.OK)
{
foreach (var line in System.IO.File.ReadLines(ofd.FileName))
{
if (line.Contains(":"))
proxyView.Items.Add(line);
}
}
}
I am trying to code a quick program and the error shows up when I assign Properties.Settings to variable s.
I am trying to assign it to this variable because a lot of my text boxes need to be assigned with a value of the setting and also because there is a lot of settings which need saving.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpDock
{
public partial class SettingsWindow : Form
{
public Properties.Settings s = new Properties.Settings(); // This is the error.
public SettingsWindow()
{
InitializeComponent();
}
private void Settings_Load(object sender, EventArgs e)
{
app1.Text = s.app1;
app2.Text = s.app2;
app3.Text = s.app3;
app4.Text = s.app4;
app5.Text = s.app5;
app6.Text = s.app6;
ico1.Text = s.ico1;
ico2.Text = s.ico2;
ico3.Text = s.ico3;
ico4.Text = s.ico4;
ico5.Text = s.ico5;
ico6.Text = s.ico6;
}
private void abutton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app1.Text = ofd.FileName;
s.app1 = ofd.FileName;
}
}
private void Accept_Click(object sender, EventArgs e)
{
s.Save();
MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void abutton2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app2.Text = ofd.FileName;
s.app2 = ofd.FileName;
}
}
private void abutton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app3.Text = ofd.FileName;
s.app3 = ofd.FileName;
}
}
private void abutton4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app4.Text = ofd.FileName;
s.app4 = ofd.FileName;
}
}
private void abutton5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app5.Text = ofd.FileName;
s.app5 = ofd.FileName;
}
}
private void abutton6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app6.Text = ofd.FileName;
s.app6 = ofd.FileName;
}
}
private void ibutton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico1.Text = ofd.FileName;
s.ico1 = ofd.FileName;
}
}
private void ibutton2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico2.Text = ofd.FileName;
s.ico2 = ofd.FileName;
}
}
private void ibutton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico3.Text = ofd.FileName;
s.ico3 = ofd.FileName;
}
}
private void ibutton4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico4.Text = ofd.FileName;
s.ico4 = ofd.FileName;
}
}
private void ibutton5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico5.Text = ofd.FileName;
s.ico5 = ofd.FileName;
}
}
private void ibutton6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico6.Text = ofd.FileName;
s.ico6 = ofd.FileName;
}
}
}
}
And the error is:
Error 1 Inconsistent accessibility: field type 'SharpDock.Properties.Settings' is less accessible than field 'SharpDock.SettingsWindow.s' C:\Users\Lewis\Documents\Visual Studio 2013\Projects\SharpDock\SharpDock\SettingsWindow.cs 14 36 SharpDock
Please help! I am stuck with this error.
~Lewis
You need to use Properties.Settings.Default and you don't need to instantiate a variable for it. If you want an alias to make the code smaller, remove this code:
public Properties.Settings s = new Properties.Settings();
And add this before the class declaration:
using s = Properties.Settings.Default;
And as deathismyfriend commented, you could reduce a lot your code by creating methods for the repeated code.
Your Settings are saved inside Properties.Settings.Default so you should use it instead of declaring your own settings. Also, you can subscribe multiple controls to the same event. What you should do is, subscribe all of your abuttons to aButton_Click event and ibuttons to iButton_Click event. Here is your code fixed:
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpDock
{
public partial class SettingsWindow : Form
{
public SettingsWindow()
{
InitializeComponent();
LoadSettings();
}
private void LoadSettings()
{
app1.Text = Properties.Settings.Default.app1;
app2.Text = Properties.Settings.Default.app2;
app3.Text = Properties.Settings.Default.app3;
app4.Text = Properties.Settings.Default.app4;
app5.Text = Properties.Settings.Default.app5;
app6.Text = Properties.Settings.Default.app6;
ico1.Text = Properties.Settings.Default.ico1;
ico2.Text = Properties.Settings.Default.ico2;
ico3.Text = Properties.Settings.Default.ico3;
ico4.Text = Properties.Settings.Default.ico4;
ico5.Text = Properties.Settings.Default.ico5;
ico6.Text = Properties.Settings.Default.ico6;
}
private void Accept_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void aButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (sender == abutton1)
{
app1.Text = ofd.FileName;
Properties.Settings.Default.app1 = ofd.FileName;
}
else if (sender == abutton2)
{
app2.Text = ofd.FileName;
Properties.Settings.Default.app2 = ofd.FileName;
}
else if (sender == abutton3)
{
app3.Text = ofd.FileName;
Properties.Settings.Default.app3 = ofd.FileName;
}
else if (sender == abutton4)
{
app4.Text = ofd.FileName;
Properties.Settings.Default.app4 = ofd.FileName;
}
else if (sender == abutton5)
{
app5.Text = ofd.FileName;
Properties.Settings.Default.app5 = ofd.FileName;
}
else if (sender == abutton6)
{
app6.Text = ofd.FileName;
Properties.Settings.Default.app6 = ofd.FileName;
}
}
}
}
private void iButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (sender == ibutton1)
{
ico1.Text = ofd.FileName;
Properties.Settings.Default.ico1 = ofd.FileName;
}
else if (sender == ibutton2)
{
ico2.Text = ofd.FileName;
Properties.Settings.Default.ico2 = ofd.FileName;
}
else if (sender == ibutton3)
{
ico3.Text = ofd.FileName;
Properties.Settings.Default.ico3 = ofd.FileName;
}
else if (sender == ibutton4)
{
ico4.Text = ofd.FileName;
Properties.Settings.Default.ico4 = ofd.FileName;
}
else if (sender == ibutton5)
{
ico5.Text = ofd.FileName;
Properties.Settings.Default.ico5 = ofd.FileName;
}
else if (sender == ibutton6)
{
ico6.Text = ofd.FileName;
Properties.Settings.Default.ico6 = ofd.FileName;
}
}
}
}
}
}
You also need items that you're referencing from your settings to be set inside your settings: