Hi, I'm pretty new to using C# and have come across a problem that I thought would be pretty simple, and it probably is but I'm stuck.

I'm trying to get a user to select a file using a file dialog box and once that has been selected to get the details of the file added to a list view (set to the detail mode).

So far I have it showing the dialog box and when the user selects the file the file path is added to the first column of the list view.

OpenFileDialog openSingleFile = new OpenFileDialog(); 

                openSingleFile.Title = "Open a Single File";
                openSingleFile.InitialDirectory = "C:";
                openSingleFile.Filter = "All Files|*.*";
                                
                openSingleFile.ShowDialog();

               fileinfoViewer.Items.Add(openSingleFile.FileName);

I thought that "FileName" would add only the name like "Worddocument.doc" but instead it adds "C:\Test Folder\Worddocument.doc"

I've come across the code "System.IO.Path.GetFileName" but can't get it to work. I've also tried getting anything to add in to the next column along such as "GetCreatedDate" but no luck with those either.

Has anyone any suggestions?

Recommended Answers

All 6 Replies

Define doesnt work, what was your code, how did you use it - it works for me

Here's and example of a few of the ways I've tried it. None of them have worked, but I could well not be using it in the right way though.

Here are a few of the ways I tried

fileinfoViewer.Items.Add(openSingleFile.GetFileName);
String fileNameOnly;
fileNameOnly = System.IO.Path.GetFileName((openSingleFile));
fileinfoViewer.Items.Add(fileNameOnly);

I'm sure that I'm not calling it in the correct manner if none of these work. I did think that it would be possible to get file info straight from the dialog box and put it in to the list view.

Well the first doesnt use the getfilename from system.io.path so no that wouldnt work

the second, would work more, but again and it doesnt ask for the filename, from the class so you probably got somethng like (Object)

this

String fileNameOnly;
fileNameOnly = System.IO.Path.GetFileName((openSingleFile));
fileinfoViewer.Items.Add(fileNameOnly);

should be

String fileNameOnly;
fileNameOnly = System.IO.Path.GetFileName((openSingleFile.FileName));
fileinfoViewer.Items.Add(fileNameOnly);

If i'm not wrong (IF), fileopendialog will have a properties call SafeFileName or something like that. It would return the file name. To get extra information.

System.IO.FileInfo Fs = new System.IO.FileInfo(thisfile);

Then from here get the information from that object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.