using openFileDialog will not return a filename in use, I want the filename only I don't care if it's in use
The file will most likely be in use, I just want to be able to find the file and retrieve its name and location to perform a connection.
OpenFileDialog works until I select the file, then it has a popup that says "File in Use". I don't want it to check for that, just return the filename.
It seems that setting the ValidateNames property to false solves the problem (but don't ask why :) ...)
Here's the code I used to try out:
var f=File.OpenWrite(#"C:\test.txt");
var ofd = new OpenFileDialog();
ofd.ValidateNames = false;
ofd.ShowDialog();
f.Close();
Commenting out the third line gave me the described error "file in use".
Try setting ValidateNames to false.
OpenFileDialog fd = new OpenFileDialog();
fd.ValidateNames = false;
I googled and found a thread that suggests this is a bug in the control:
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56fbbf9b-31d5-4e89-be85-83d9cb1d538c/
The suggested workaround is to call the API directly as found here:
http://www.codeproject.com/KB/dialog/customize_dialog.aspx?print=true
Related
I was just at Open directory dialog, and they said "get this package, and do this and this to get a folder select window to show up". Well, that all works great, using the Windows API Code Pack-Shell package. However, now I want to get the actual folder that is selected. I didn't notice them mentioning this anywhere.
I tried to do string folderLocation = Convert.ToString(dialog); (dialog is the variable for opening the folder window), but that only gave me like the property of the variable. I also tried this: CommonFileDialogResult result = dialog.ShowDialog();
string folderLocation = Convert.ToString(result);
But that just gave me "Ok" - which I take it is the result of it, instead of the actual folder.
The result of ShowDialog just indicates wether the user clicked OK, cancel, or just closed the window.
CommonOpenFileDialog can be used for both files and folders, so it's a bit surprising when used as a folder picker, but the path is stored in FileName.
var dlg = new CommonOpenFileDialog();
dlg.IsFolderPicker = true;
if(dlg.ShowDialog() == CommonFileDialogResult.Ok) {
Console.WriteLine(dlg.FileName);
}
If I understood correctly, you want to get Folder for a selected file? If that's the case, you can take FileInfo for that file, and extract folfer from it. Like this:
System.IO.FileInfo fInfo = new System.IO.FileInfo(oFD1.FileName);
MessageBox.Show(fInfo.DirectoryName);
PS. oFD1 is OpenFileDialog
Similar question: How to avoid of file name validation in SaveFileDialog C#
I am writing an application that has an option to support opening executables and running them with given parameters, and I am trying to use OpenFileDialog as a user-friendly way to achieve this. After disabling AddExtension, ValidateNames, CheckFileExists and CheckPathExists, I can pass parameters to executables and the application runs them using the arguments as intended.
However, when I attempt to pass parameters that include "invalid" filename characters (such as '/'), I am stopped and get this example message:
and am not allowed to continue, even though ValidateNames is set to false.
Here is the code concerning the dialog:
OpenFileDialog dialog = new OpenFileDialog();
dialog.AddExtension = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = false;
dialog.ValidateNames = false;
if (dialog.ShowDialog() == DialogResult.OK) {
//Parse text manually here (parsing is fully handled on the developer side)
}
Is there any way to resolve this and completely disable input validation, or do I have to write a custom file dialog implementation?
You can't override the default validation. As such you will either have to ask for the parameters in a separate dialogue or re-implement the file open dialogue yourself.
VERY VERY new to C Sharp, as it's not part of my study path, but I have to edit small codes in C Sharp in order for my app to work. I'm using Ogama, a open source gaze tracker, which I need for my project. The heatmap, to be more specific. Now, I want to save the heatmap to a directed folder, and managed to find the code. The initial code was
public static bool ExportImageToFile(Image image)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "Please enter filename for image...";
dlg.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
So I thought I could change it, by following other tutorials online, and this was my code.
public static bool ExportImageToFile(Image image)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "Please enter filename for image...";
dlg.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "heatmapimages");
But it doesn't work. When I click the save file button, it brings me to where my Ogama projects are. The 'heatmapimages' folder is in my Desktop. Any advice? Thank you in advance.
EDIT: Managed to make it work, by changing Special.Personal to Special.DesktopDirectory. Is there a way to make it autosave the image? Such that I don't need to click save?
You need to set RestoreDirectory true and set InitialDirectory to SpecialFolder.Desktop. Here are all SpecialFolders for your reference.
It restores the current directory to its original value if the user changed the directory while searching for files.
I have an OpenFileDialog with the filter set to *.wav. However, when I execute the OpenFileDialog it also shows other files that includes .wav but the true extension is not .wav but e.g. png. Why is that and how can I avoid this?
Right now I take care of it when loading the file(s) for processing but I would like to avoid getting them in the OpenFileDialog list in the first place. Is this a bug in the control or is it me?
Background: I had by accident renamed a picture file to TheFile.wav.png - stupid, true, but these kind of things happens also for other users.
Thanks in advance
Try this to set the file type in the dialog:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "wav files (*.wav)|*.wav";
dialog.InitialDirectory = #"C:\"; // You may not need this.
if (dialog.ShowDialog() == DialogResult.OK) // Or this; I was just being thorough.
{
// Your code can go here.
}
Just make sure that when using the .Filter property, you follow the pattern I have above, or else it won't work. Also, as was mentioned above, you may want to do some validation after the user selects something.
I use this code to save an avi file. When I create a new file, it's no problem.
But When I choose an existing file, It does not work and saveFileDialog still shows.
I have set saveDialog.OverwritePrompt and saveDialog.CheckFileExists is true, but it is not ok. If I set saveDialog.OverwritePrompt is false it runs, but it does show overwrite warning
How can I solve this?
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.OverwritePrompt = true;
DialogResult dgResult = saveDialog.ShowDialog();
if (dgResult == DialogResult.OK)
{
exportAvi(saveDialog.FileName);
}
This code works - if I choose to overwerite an existing file it shows me the Prompt:
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.OverwritePrompt = true;
DialogResult dgResult = saveDialog.ShowDialog();
if (dgResult == DialogResult.OK)
{
//exportAvi(saveDialog.FileName);
}
[Window Title]
Confirm Save As
[Content]
XYZ.txt already exists.
Do you want to replace it?
[Yes] [No]
Your issue is in the exportAvi() function. You are not allowing for the file to be overwritten. I cannot see your exportAvi() function so I cannot tell you what is wrong exactly. If you post your exportAvi() function I can help you further.
saveDialog.OverwritePrompt - will only prompt the user if they want to overwrite. It will not overwrite the file. You have to handle this in your code.
saveDialog.CheckFileExists - will only check if the file exists.
Check your export function and make sure you are setting the overwrite parameter to True.