I want to choose a file from an already opened OpenFileDialog which is shown by the browser.
I want to use code similar to the following example:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.InitialDirectory = "C:\\";
The problem is, that I cant access dialog. I can't reference it.
Edit:
I use the selenium-webdriver for automated testing. But the fileDialog is presented by the website I want to test.
without an OpenFileDialog you say?
you have a few options for this..
one being a method i used here -
https://github.com/Ricky310711/RDExplorer
You would need to create a new panel with a ListView and populate it with local folders and files, add a double click event handler to select the item selected by storing it in a string.
Related
I have some basic code for opening an OpenFileDialog.
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".csv";
dlg.Filter = "FileType|*.csv";
dlg.Multiselect = false;
bool? result = dlg.ShowDialog();
It works fine, but my Windows 7 users are complaining that the mouse scroll wheel doesn't scroll the list of files. I think the reason for this is that the initial focus is on the text field for the file name. If I click on the list of files, they become scrollable. How can I set the initial focus in the OpenFileDialog to be the list of files instead of the textbox?
I have a folder browser dialog (simple enough) but I only want it to be 'ok' if the folder contains files of a certain extension. What I have so far:
FolderBrowserDialog pDlg = new FolderBrowserDialog();
if (pDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (Directory.GetFiles(pDlg.SelectedPath, "*.ext").Length > 0)
{
SrcDir.Text = pDlg.SelectedPath;
}
else
{
DBox.Items.Insert(0, "Not a suitable folder");
}
}
This works in limiting the population of the textbox SrcDir if the selected folder does not contain any of the right sort of files (*.ext). It would be preferable if I could disable the 'ok' button when the folder is selected in the dialog if GetFiles(..).Length == 0 so an unsuitable folder just cannot be selected; as you cannot 'see' the files in the FolderBrowserDialog it's kind of hard for the user to know if it is the right folder, so by changing the enabled state of the OK button would indicate to the user if the folder is suitable.
I could use a OpenFileDialog to browse for one file in the directory and then use FileInfo.DirectoryName to extract the folder that its in, but I am under pressure not to do it that way (others think it's sloppy).
I am fairly sure this can't be done with a standard FolderBrowserDialog; is there another in-built dialog class that I can control this behavior or should I create a new form with a DirectorySearcher or similar TreeView and .ShowDialog() on a custom dialog form?
With folderBrowseDialog you can show the top level of the folder which will be shown initially. However that feature is not there on openFileDialog.
I want to let the user open files from a specific folder only. How do I specify the folder?
Just set the openFileDialog.InitialDirectory
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = #"C:\";
openFileDialog.ShowDialog();
Set InitialDirectory property:
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = #"D:\SomeFolder";
dialog.ShowDialog();
Gawjus, answering your question "How can I restrict the user from accessing other folders? can I lock them?", you can use Environment Special Folder as per code below, but as far I know you cannot define a custom folder, there are some options available. Another way it's to create an UserControl that list only files from a specific folder. Doing that you can have more control how it will behave.
ofd.RootFolder = Environment.SpecialFolder.MyDocuments;
I have a small application that needs to save to contents of a rich text box as a text file upon a button click. What I currently have is
//save button logic
private void saveBtn1_Click(object sender, EventArgs e)
{
//set up new SaveFileDialog
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text Files (*.txt)|*.txt";
saveFileDialog1.Title = "Save a text File";
//prompt the user
DialogResult result = saveFileDialog1.ShowDialog();
//if they select save write the contents of the RTB to a text file
if (result == DialogResult.OK)
{
System.IO.File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
}
}
It seems like a pretty straight forward piece of code, but I can't figure out why the dialog opens twice.
Just expanding Servy's comment into an answer because I suspect his diagnosis is correct. I'm going to give some simple suggestions for diagnosing/finding the problem. Firstly, you can put a break point in the event handler, if you're taken into that event handler twice then that's obviously the problem. The other solution (without even running) is simply to right click on the method name and select "find all references" this will give you list of all the references to the this method within your code. I'm guessing you will find 3; the method itself, the place where you expect to be registering it, and the place you were you don't. Remove the one and your problem will go away.
Shortest answer: you called the save dialog() twice in your code. Remove the second one and your issue will clear up.
Because you are used twice.
Thisa is simple excample for you
SaveFileDialog save = new SaveFileDialog();
save.DefaultExt = "zip";
save.Filter = "zip faylı (*.zip)|*.7z|Hamısı(*.*)|*.*";
save.ShowDialog(); // this is first using
if (save.ShowDialog() == DialogResult.OK) // and second using save.ShowDialog()
{
// yours code here
}
I'm trying to make this program in C# using WPF in Visual Studio. This is basically what it has to do.
When a button called "Browse" is clicked on the main form, it will open up a new form/window that let's the user browse to any directory that he chooses. After he selects the folder and clicks "Open" (or some other button on that form), the path of that directory, for example, "C:\temp" will be stored in a string variable so it can used later.
My first problem is, what do I write in the even handler of the "Browse" button that will open up a window that let's the user browse and select a folder? Is there a default window I can use or do I have to create a new form for it? Please note, the user has to select a folder, not a file like the default "Open" window.
Secondly, how do I reference a string variable so that it stores the path of the directory that the user selected?
The type you are looking for is the OpenFileDialog
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
The basic usage is the following
using (FileDialog fileDialog = new OpenFileDialog()) {
if (DialogResult.Ok == fileDialog.ShowDialog()) {
string fileName = fileDialog.FileName;
...
}
}
EDIT
Comments clarified OP is looking to open a directory vs. a file. For this you need the FolderBrowseDialog
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
For choosing directory you should use FolderBrowserDialog. It's a control from WinForms. WPF doesn't have it's own.
For example:
var dialog = new FolderBrowserDialog();
var result = dialog.ShowDialog();
if (result == DialogResult.OK)
// ...
Don't forget to add reference to the System.Windows.Forms.
Simply do this on Button Click
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
(folderpathTB is name of TextBox where I wana put the file path, OR u can assign it to a string variable too)