Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return a filename

Tags:

c#

.net-3.5

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.

like image 641
Roger Avatar asked Aug 24 '10 23:08

Roger


People also ask

What is return Name of file?

The getName() method is a part of File class. This function returns the Name of the given file object.

How do I extract file names?

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);

How do I get filenames without an extension in Unix?

Using Bash, there's also ${file%. *} to get the filename without the extension and ${file##*.} to get the extension alone. That is, file="thisfile.


3 Answers

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".

like image 161
MartinStettner Avatar answered Oct 10 '22 07:10

MartinStettner


Try setting ValidateNames to false.

OpenFileDialog fd = new OpenFileDialog();
fd.ValidateNames = false;
like image 42
JustBoo Avatar answered Oct 10 '22 09:10

JustBoo


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

like image 22
Christopher Painter Avatar answered Oct 10 '22 07:10

Christopher Painter