I have a small requirement and that is as follows:
I have opened a file using the "openFileDialog" provision and i have set the filer as (*.txt). Now when the openFileDialog is displayed, only the test files are visible, but the user can also select "All Files" and choose any other file type.
Now what is require is that, if a file type other than .txt is selected by the user, i want to display an error message.
So is there any provision by which i can get to know the file type that is selected by the user.
Regards,
George
Look at http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx
Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim extension As String
extension = Path.GetExtension(fileName)
Console.WriteLine("GetExtension('{0}') returns '{1}'", fileName, extension)
You can use the FileOK event to show the message box while the dialog is still open. Use the GetExtension method to determine the extension.
You should also look at the Filter property of the dialog. If you set it correctly "All Files" should not be shown anymore.
Example:
dlg.Filter = "Test-Files (*.txt)|*.txt"
Related
To provide our users to edit excel files without ms excel, we have made a simple app with visual studio 2012 and devexpress Spreadsheet module.
It is very simple to open excel file and use it.
But now only one excel file is being used (with multiple sheets), and I would like the file being used to be opened always on startup.
If I add the path and filename to command line arguments, noting happens...
Using devexpress components is very different then vanilla code for me, I am a complete beginner here, so I have no idea how to continue - can someone, please point me in the right direction?
I have made a procedure, to open the file dialog and load the file - I don't know how to "pass" it to devexpress, so the file actually loads to workbook.
Private Sub OpenXls()
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "xls"
ofd.FileName = "FILE"
ofd.InitialDirectory = "C:\ref_files"
ofd.Title = "Select file"
End Sub
As you have pointed out - using the dialog is not the right way.
After some googling I have find out that this should be a better way:
Dim workbook As New Workbook
workbook.LoadDocument("C:\ref_files\file.xls", DocumentFormat.xls)
I do not get any error, but the file is also not shown...
Do I have to display the document manually after loading?
If you're using openfiledialogue to open file then you must use command to load the specific file at Form.Load event.
The backslash char in ofd.InitialDirectory = "C:\ref_files"escapes the letter R to carrige return.
Change this line to ofd.InitialDirectory = "C:\\ref_files" (add another backslash).
I have a List with about 200 filepaths to be copied to a new location (replacing the root of the path from a remote, to a local). (from about 6 different directories)
I'd like the user to be prompted with the standard File Copy dialog box when conflicts occur. Specifically where it gives the option to "Skip all files" or "Replace all files"
I can use the FileIO.FileSystem.CopyFile and choose the "FileIO.UIOption.AllDialogs". This gets the dialog box I'm expecting, but because you can only pass a single sourceFilename and destinationFilename, if many/all of the files already exist, the user is then prompted with the dialog for each file.
Dim filesToBeCopied as List(Of String)
For Each remoteFile in filesToBeCopied
FileIO.FileSystem.CopyFile(remoteFile, GetLocalPath(remoteFile), FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
Next
Again, the above produces the 'right' dialog box, but for every iteration. I'm looking for it to prompt the user on conflict either
1) once. period. or,
2) once per directory.
Thanks,
I have a code to export an Excel tab and save it as a new file with a preset file name. The user has the option to review the file name before saving. This is what I have been using:
InitialName = SaveString & UniqueString
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName)
If fileSaveName <> False Then
Export.SaveAs (fileSavename)
End If
SaveString is the save folder, and UniqueString is the specific file name, which changes each month. The user clicks Export, the tab is prepared, and the Save As folder pops up in the correct folder with the suggested file name. As long as the user hits "Save," then the exported tab is saved in the SaveString folder with the UniqueString name (with .xlsx already included in UniqueString).
Since upgrading to Office 2016, the UniqueString suggested file name no longer shows up. The Save As pop-up still opens in the SaveString folder, but there is no suggested file name. If the user isn't careful to manually add .xlsx to the end of the file name, then the file type is an unusable "File."
I've opened Excel 2013 in a virtual setting and run the code side-by-side, and it works perfectly in the older version. Does anyone have insight as to why this change happened, and how to correct it?
It appears that you now need to include a file filter that matches the initial name you provide, so the following will possibly work:
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, _
FileFilter:="Excel Files (*.xlsx),*.xlsx")
I am developing a media player based on WMP.
If the user sets my media player as the default player to play .avi files for example, then clicks a random .avi file, my player will start successfully, however it will not load the file that the user opened.
So thats why I need a code that will get the path of the file the user opened, so that I can make the player load that file automatically after it has been started.
Use the My.Application.CommandLineArgs property. This gets you a list of commandline arguments. Launching a file from the explorer usually provides the path to the clicked file as the only argument, so use it like:
If My.Application.CommandLineArgs IsNot Nothing AndAlso _
My.Application.CommandLineArgs.Count > 0 Then
Dim UserFile As String = My.Application.CommandLineArgs(0)
PlayFile(UserFile)
End If
Assume that I have a Windows Forms app in which I type all the details of an employee and I need to attach his resume (which is in a PDF format) to his details. When I click the save button, all his details which he has entered can be stored into a table.
How do I attach a PDF file into the app which the user can do by clicking a button after typing all his details?
When the user clicks the save button, how do I save the file that has been attached by the user?
When I retrieve the employee details, I want the file which was attached also to be displayed and shown.
Use the OpenFileDialog to allow the user to browse to the PDF they want to attach. Store the file path of the selected PDF.
When the user saves the record, read the file the user selected into a byte[], and save that byte[] into a database field (probably a blob field). (See here)
To read it back in, do the reverse. Get the byte[] out of the database, and reassemble it into a file, save the file as a temp PDF and then start a process to load it. (See here)
Here's a simple example, with no error checking, demonstrating Michael Shimmins' good suggestion.
Dim ofd As New OpenFileDialog()
'Set ofd settings'
If ofd.ShowDialog() = DialogResult.Cancel Then
Return
End If
Dim pdfData As Byte() = File.ReadAllBytes(ofd.FileName)
'Save it to your database.'
However, I suggest you consider copying the files to a file server instead of dealing with the binary data and BLOBs if you can help it. BLOBs have been known to kill database performance on some platforms. Here's one reference. Google "blob database performance" for more.
Here's an example of copying the file to a file server.
Dim newFileName As String = UniqueFileNameGeneratingFunction()
File.Copy(odf.FileName, newFileName)
'Save newFileName to your database as a string, and retrieve the file as needed'
'from the file server'