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.
The getName() method is a part of File class. This function returns the Name of the given file object.
To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);
Using Bash, there's also ${file%. *} to get the filename without the extension and ${file##*.} to get the extension alone. That is, file="thisfile.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With