I have a customized OpenFileDialog (VS2008, C#, Windows Forms) with a ComboBox. The ComboBox will have a list of paths which the user can select.
My question, is there a way I can change the directory in Open File Dialog to point to the path in the combobox selected item.
InitialDirectory works only before I open the dialog, I wanted a way to change the directory programatically after the dialog is open.
Thanks
If you're using Vista or Windows 7 with .NET 3.5 SP1 I recommend you use the CustomPlaces property on OpenFileDialog rather than a custom combo box.
See this MSDN article (for WPF): http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.customplaces(v=VS.100).aspx
Or this MSDN article (for Windows Forms): http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.customplaces(v=VS.100).aspx
On Windows 2000 and XP it is also possible to customize the places side bar. But it is more difficult and requires you to use some C++ code (via CLI/C++ is probably best). The technique is described in detail in this MSDN article: http://msdn.microsoft.com/en-us/magazine/cc300434.aspx
If you're dead set on using a combo box you've added to the OpenFileDialog then you will probably just need to know what windows message to send to the dialog. I'm afraid I don't know which message you need to send. The nasty internal Win32 API details of the Common Open/Save dialog is detailed here: http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx
If you can figure out which messages to send to the window the probably way of doing things is to fill the filename text field with the directory you want to switch to simulate a OK button click. The dialog will switch to that directory if you do this.
Sending messages to this window will probably require you to not use OpenFileDialog directly but rather subclass the abstract FileDialog class upon which it is based.
Just set the InitialDirectory property of openFileDialog1
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = cmbPath.SelectedValue.ToString();
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 from disk. Original error: " + ex.Message);
}
}
}
As already said InitialDirectory works before hand but why would you change the folder afterwords? FileOpenDialog is a modal dialog, therefore the user can't use anything else of your application than the dialog. What is the benefit and reason why you wan't to set the folder? It seems your using the wrong tools to get the job done.
Related
I need to programmatically open a document from Sharepoint in Visio. But when I navigate to the network folder, select a document and click on open, I
get the following error:
The filename, directory name, or volume label syntax is incorrect
When searching for the error, I found the following documentation: https://msdn.microsoft.com/en-us/library/ms832054.aspx. So I guess that the file name contains illegal characters. I tried to use the FileOk event to overwrite the validation of the fileName:
public void openFile() {
OpenFileDialog sf = new OpenFileDialog();
sf.FileOk += openFileDialog_FileOk;
if (sf.ShowDialog() == DialogResult.OK)
{
var app =(Microsoft.Office.Interop.Visio.Application)context.Application;
app.Documents.Open(sf.FileName);
}
}
private void openFileDialog_FileOk(object sender, CancelEventArgs e)
{
var sfd = sender as OpenFileDialog;
var file = new FileInfo(sfd.FileName);
if (file.Name.Contains('#'))
e.Cancel = true;
}
but the event does not fire. Using the standard Visio interface it is possible to open files from Sharepoint but the file dialog looks a bit different:
How can I get a similar file dialog? And so my questions is: how can I programmatically open a Visio document from Sharepoint (network folder)?
Since Visio does not provide app.GetOpenFilename API, you are out of luck. But you could use another office application for the same. Like Excel for example:
var excel = new Excel.Application();
var fileName = excel.GetOpenFilename();
excel.Quit();
var visio = new Visio.Application();
visio.Documents.Open(fileName);
which provides a "similar dialog" and "normal url", that is understood by Visio API without any issues.
The problem probably is that Visio API does not understand UNC DAV file path format with #SSL part, that is provided by the default "built-in" OpenFileDialog (or may be something else as well). Check what is the value of the .FileName returned by the default dialog. BTW, to prevent error messages, it's enough to set sf.CheckFileExists = false, maybe that will be enough.
I'm creating right now an application in C# which saves data to the file upon clicking a button. The name and locations of the file is defined by the user, and i want the program to automatically add .txt extension to the name, as well as show only 1 possible extensions in "save files as" combobox. I have this code:
SaveFileDialog Dialog1 = new SaveFileDialog();
Dialog1.DefaultExt = "txt";
Dialog1.Filter = "Pliki tekstowe | *.txt";
Dialog1.AddExtension = true;
private void button1_Click(object sender, EventArgs e)
{
if (Dialog1.ShowDialog() == DialogResult.OK)
{
System.IO.Stream fileStream = Dialog1.OpenFile();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
sw.WriteLine("Writing some text in the file.");
sw.WriteLine("Some other line.");
sw.Flush();
sw.Close();
}
this.Close();
}
But whenever i click the button, i have no options to choose from in the combobox, as well as the .txt extensions are not added to the file name in case the extensions is not specified by the user himself. I know i can somehow bypass that by checking if the user gave the proper extensions, and in case he didn't add ".txt" to the name but i really wanted to know, why that piecei of code doesn't function. Any help?
The problem is the space. Just change it to this:
Dialog1.Filter = "Pliki tekstowe|*.txt";
and you should be in business.
Otherwise it's trying to match files of the pattern *.txt (subtly compared to *.txt) - which you probably don't have.
take a look at the following example and see the difference:
http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog(v=vs.110).aspx
you should try firstly to add this to your filter:
"txt files (*.txt)|*.txt|All files (*.*)|*.*"
for completeness you should add the (*.txt) string in your filter for descriptive reasons (thanks for clarifying Yuck). Try it - see what happens :)
remember it is sensitive in terms of the string. so try not to put things like a space between the file extensions
I had a similar issue where the save dialog box on firefox and safari doesn't detect the file extension even though I had Content-Type header set to "application/pdf". The issue turned out to be spaces in the name of the file. I replaced the file name with '-' (hyphens) and that fixed it.
I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer.
So, I have changed my question.
I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.
From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.
However, while using this, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure from MFC.
And OPENFILENAME has the member named "templateID".
It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.
But I don't know How can I attach this file to my C# project?
Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?
If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog x = new OpenFileDialog();
x.Multiselect = true;
x.ShowDialog();
string[] result = x.FileNames;
foreach (string y in result)
MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
For files and folders you need to use the CommonOpenFileDialog included with the WinAPI, the particular class is here.
Try this:
openFileDialog.Multiselect = true;
You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.
I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer.
So, I have changed my question.
I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.
From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.
However, while using this, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure from MFC.
And OPENFILENAME has the member named "templateID".
It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.
But I don't know How can I attach this file to my C# project?
Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?
If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog x = new OpenFileDialog();
x.Multiselect = true;
x.ShowDialog();
string[] result = x.FileNames;
foreach (string y in result)
MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
For files and folders you need to use the CommonOpenFileDialog included with the WinAPI, the particular class is here.
Try this:
openFileDialog.Multiselect = true;
You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.
I am making a software that needs to ONLY be able allow people to select files and folders using the OpenFileDialog that are in the same directory as the program and that are in deeper folders. I don't want the OpenFileDialog to be able to select stuff outside of the program's current directory. Is this possible to do in C# using the OpenFileDialog?
Please let me know
Thanks
I don't see any out of the box support by the OpenFileDialog Control. However, you can try the following,
Set the InitialDirectory property to your program path. Then if a user selects a particular path outside of your program path, use the FileOk event to check this and bring him back to the InitialDirectory.
If you want much more control then you will have to write your custom dialog.
This is how I did it.
openFileDialog1.InitialDirectory = Path.Combine(Path.GetDirectoryName(Application.StartupPath), "FolderName");
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
while(Path.GetDirectoryName(openFileDialog1.FileName) != Path.Combine(Path.GetDirectoryName(Application.StartupPath), "FolderName")){
MessageBox.Show("Please select .EXE which is in the default folder", "Wrong folder", MessageBoxButtons.OK, MessageBoxIcon.Information);
openFileDialog1.ShowDialog();
}
}
you can check if the path is correct after selected
if its just accept or send message box tell him you select different directory
I'm afraid you can't. Most people created their own custom dialog for this scenario.