VB.NET - How to Drag a File From Outlook 2010 to My Form? - vb.net

I'm developing an application to read XML files, get its data and populate some field. SO, I've implemented two ways to get the file: through a command button and through the drag and drop form event. Everything is OK if I use a file stored in a folder in my computer. I can drag the file, the cursor changes its appearance to the copy effect etc. However, when I try doing it dragging a file as an attachment of Outlook (my version is the 2010), I see that the cursor assumes the forbidden effect and the file is not read.
Please, see below the implementation I did, which is working only for files stored in a folder in my computer. I would like to know what more I have to implement to allow drag a file from Outlook.
Thank you in advance.
Private Sub FrmJIG_DragDrop(sender As Object, e As DragEventArgs) _
Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
Dim filestype() As String
filestype = e.Data.GetData(DataFormats.FileDrop)
Dim sReader As New StreamReader(filestype(0))
'get the filename from the file without the path
Dim file_name As String = Path.GetFileName(filestype(0))
'check the extension of the file
If Path.GetExtension(filestype(0)).ToLower() = ".xml" Then
'Read the xml file
For Each path In files
ReadXMLFile(path)
Next
Else
'warning about the file type
MessageBox.Show("Only XML files are supported!", "Warning!", _
MessageBoxButtons.OK, _
MessageBoxIcon.Warning)
End If
End Sub
Private Sub FrmJIG_DragEnter(sender As Object, e As DragEventArgs) _
Handles Me.DragEnter
'change the cursor type to drag and drop type
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub

Please try with
Private Sub FrmJIG_DragEnter(sender As Object, e As DragEventArgs) _
Handles Me.DragEnter
'change the cursor type to drag and drop type
If e.Data.GetDataPresent(DataFormats.FileDrop) or e.Data.GetDataPresent("FileGroupDescriptor") Then
e.Effect = DragDropEffects.Copy
End If
End Sub

Related

How to overwrite a file in specific folder with a file in my program's resources

I have a file named "something.a binary extension" "not a txt extension" in my program's resources and the same file exists in another folder and named "Something.a binary extension" too.
I want to copy my resources file and replace the existing file with it.
My program has 2 button controls, one for browsing to get the path from OpenFileDialog1 and the other to copy my resources file and replace the existing file "Which have the same name" by it, and 1 TextBox to display the path.
My program has a manifest file to run as Adminstrator with level="highestAvailable.
I use this code:
Public Class Form1
Dim Path_of_folder As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
'getting the path
OpenFileDialog1.ValidateNames = False
OpenFileDialog1.CheckFileExists = False
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.FileName = "somthing.exe"
OpenFileDialog1.ShowDialog()
TextBox1.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)
TextBox1.Text = TextBox1.Text.Replace("somthing.exe", "").Trim()
Path_of_folder = TextBox1.Text 'we got the path
End Sub
Private Sub BtnCopy_Click(sender As Object, e As EventArgs) Handles BtnCopy.Click
FileCopy(My.Resources.somthing, Path_of_folder) ' this got Error when i use it, but the program can Run
End Sub
End Class

How to display open file name in "Save As" dialogue box with Visual Basic?

I'm learning Visual Basic and I need to know how when I click "Save" on my GUI and the Save Dialogue box pops up, the current open file name is already being displayed in "File Name: xxxxxx"
This is what I have so far:
Imports System.IO
Public Class Form1
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim dr As DialogResult
dr = dlgSave.ShowDialog()
If dr = Windows.Forms.DialogResult.OK Then
Dim writerVar As StreamWriter
writerVar = New StreamWriter(dlgSave.FileName, False)
writerVar.Write(txtEdit.Text)
writerVar.Close()
End If
End Sub
Cheers,
Alex
This should help you out:
' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button2.Click
' Set the properties on SaveFileDialog1 so the user is
' prompted to create the file if it doesn't exist
' or overwrite the file if it does exist.
SaveFileDialog1.CreatePrompt = True
SaveFileDialog1.OverwritePrompt = True
' Set the file name to myText.txt, set the type filter
' to text files, and set the initial directory to the
' MyDocuments folder.
SaveFileDialog1.FileName = "myText"
' DefaultExt is only used when "All files" is selected from
' the filter box and no extension is specified by the user.
SaveFileDialog1.DefaultExt = "txt"
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
SaveFileDialog1.InitialDirectory = _
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Source: https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filename(v=vs.110).aspx
I recommend you read more into the FileName property there in order to find what you're looking for :-) hopefully that solves your problem!
Edit: Changed code to VB. Sorry, should have read the question more clearly. Been doing too much C# today..

Creating an application that moves a collection files from one place to another using VB

So this is an application I have to create that moves files from one directory to another, both directories being specified by the user. It seems easy enough, but the problem is that there are millions of files that have to be searched through. These files are all named with 8 digits for example, "98938495.crt". The directory where all these files are has multiple folders. These folders within the main one are named with the first two digits of all the files that are in the folder. And then in that folder there are roughly ten zipped folders that contain 100,000 files each. The name of those folders are the minimum and maximum names of the files. For example, I go into the main folder, then click on the "90xx" folder. In that one there are 10 zipped folders which are named with the minimum and maximum names of the files, like "90000000_90099999.zip". That zip folder contains 100000 files. Now, this app is supposed to find all the files that the user inputs and then move them to a folder specified by the user. I have posted my code so far below, any help at all is greatly appreciate!!
FYI: The STID is the name of the file.
GUI for the app
Edit: I realized there is no way to answer this question because there really isn't a question, just a broken app. Basically, how do i search for the items in the directory and then copy them into a new directory?
Imports System
Imports System.IO
Public Class CertFinder
Private Sub SourceDirectoryTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceDirectoryTB.TextChanged
End Sub
Private Sub DestinationDirectoryTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationDirectoryTB.TextChanged
End Sub
Private Sub SearchTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchTB.TextChanged
End Sub
Private Sub SourceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceButton.Click
Dim fbd As New FolderBrowserDialog
fbd.RootFolder = Environment.SpecialFolder.MyComputer
If fbd.ShowDialog = DialogResult.OK Then
SourceDirectoryTB.Text = fbd.SelectedPath
End If
End Sub
Private Sub DestinationButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationButton.Click
Dim fbd As New FolderBrowserDialog
fbd.RootFolder = Environment.SpecialFolder.MyComputer
If fbd.ShowDialog = DialogResult.OK Then
DestinationDirectoryTB.Text = fbd.SelectedPath
End If
End Sub
Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
'Array of stids
Dim stidsToFind As String()
'get text in the searchTB textbox
Dim searchTBText As String = Me.SearchTB.Text.Trim()
'splits stids into seperate lines
stidsToFind = searchTBText.Split(vbCrLf)
'gets text from source directory text box
Dim sourceDirectory As String = Me.SourceDirectoryTB.Text.Trim()
'gets text from destination directory text box
Dim destinationDirectory As String = Me.DestinationDirectoryTB.Text.Trim()
Dim fullPathToFile As String = sourceDirectory
'Go through each stid in the search text box and continue if nothing
For Each stidToFind As String In stidsToFind
If String.IsNullOrWhiteSpace(stidToFind) Then
Continue For
End If
'Find the first two digits of the stid
Dim firstTwoDigitsOfSTID As String = stidToFind.Substring(0, 2)
'In the specified directory, find the folder with the first two digits and "xx"
fullPathToFile = fullPathToFile & "\" & firstTwoDigitsOfSTID & "xx"
Dim allFileNames As String() = Nothing
allFileNames = Directory.GetFiles(sourceDirectory, "*.crt*", SearchOption.AllDirectories)
Next
'-------------------------------------------------------------------------------
Try
If File.Exists(fullPathToFile) = False Then
Dim FS As FileStream = File.Create(fullPathToFile)
FS.Close()
End If
File.Move(fullPathToFile, destinationDirectory)
Console.WriteLine("{0} moved to {1}", fullPathToFile, destinationDirectory)
Catch ex As Exception
MessageBox.Show("File does not exist")
End Try
Dim sc As New Shell32.Shell()
'Declare the folder where the files will be extracted
Dim output As Shell32.Folder = sc.NameSpace(destinationDirectory)
'Declare your input zip file as folder .
Dim input As Shell32.Folder = sc.NameSpace(stidsToFind)
'Extract the files from the zip file using the CopyHere command .
output.CopyHere(input.Items, 4)
'-------------------------------------------------------------------------------
' 1. Get the names of each .zip file in the folder.
' 2. Assume that the .zip files are named as such <minimum STID>_<maximum STID>.zip
' 3. For each .zip file name, get the Minimum STID and Maximum STID values.
' 4. Check the range of 'stidToFind' against the STID ranges of each .zip file.
' 5. Once the .zip file is located,
' a. Copy the .zip file to the local computer OR
' b. Just leave it where it is.
' 6. Unzip the file.
' 7. Create the full file name path. ../<stidToFind>.crt
' 8. Copy the file to the destination directory.
' 9. Delete the unzipped folder
' 10. Delete the .zip file (IF you've copied it to your local computer).
End Sub
End Class
In answer to the .ZIP unpacking, use the System.IO.Packaging (more can be found at CodeProject - Zip Files Easy)
Don't you think using a database would be easier to store that data?

vb..NET Pause/Resume a file copy (like windows 8 button)

Can we PAUSE and RESUME a file copy ?
Using :
3 Button_
1 OpenFileDialog_
1 FolderBrowserDialog
Imports :
Imports System.IO
Code :
Private Sub BTN_filedialog_Click(sender As Object, e As EventArgs) Handles BTN_filedialog.Click
OpenFileDialog1.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" 'Cette clé de registre ouvre "Ordinateur"
OpenFileDialog1.Title =
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = _
"All files (*.*)|*.*|txt files (*.txt)|*.txt"
Dim dlgResult = OpenFileDialog1.ShowDialog()
If dlgResult <> System.Windows.Forms.DialogResult.OK Then
MessageBox.Show("File Error " & dlgResult)
End If
End Sub
Private Sub BTN_folderbrowserdialog_Click(sender As Object, e As EventArgs) Handles BTN_folderbrowserdialog.Click
Dim dlgResult = FolderBrowserDialog1.ShowDialog()
If dlgResult <> System.Windows.Forms.DialogResult.OK Then
MessageBox.Show("Folder Error " & dlgResult)
End If
End Sub
Private Sub BTN_copyfile_Click(sender As Object, e As EventArgs) Handles BTN_copyfile.Click
My.Computer.FileSystem.CopyFile( _
OpenFileDialog1.FileName, _
FolderBrowserDialog1.SelectedPath & "\" & _
OpenFileDialog1.SafeFileName, _
FileIO.UIOption.AllDialogs)
End Sub
I use My.Computer.FileSystem.CopyFile
With Windows 8, inside the dialog box "copy", there is a pause button.
How to call this "event" ?
I do not know if this is possible, but can we send parameters to a certain Lib /. Dll to simulate the action of pressing the pause button ?
Ps : Sorry for my bad english, i'm french.
The best way to approach this would be to ignore the FileSystem.CopyFile command completely. Instead, use a FileStream to open the file you wish to copy, read the bytes, create a new file to the target destination, and write the bytes to that location.
You can read and write the file in smaller chunks so that you can check to see if a pause variable is set to true. For example, if you create a pause button, set a global variable "bCopyPaused = true". In the custom copy function you can use a simple if statement and check if bCopyPaused == true and pause the operation. You'll need to keep track of the last copied or written bytes to allow the operation to be resumed.
Here's some examples of using FileStream: http://vb.net-informations.com/files/vb.net_FileStream.htm

VB.NET - Choose folder by browsing and copy file from application directory

Hello StackOverflow users. I am trying to create an application where you can browse to a folder, press install button and it will copy some files to the directory of your choosing?
I found some example code but
how do i go on with my code from here? Cant figure out how to copy the files. You can see at last in the code i tried to copy files but its not really working, how do i use the function? I want the files to come from the application directory. And copy to the browsed folder.
Public Class Installer
Private Sub Installer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
End Sub
Private Sub BrowseButton_Click_1(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click
' Declare a variable named theFolderBrowser of type FolderBrowserDialog.
Dim theFolderBrowser As New FolderBrowserDialog
' Set theFolderBrowser object's Description property to
' give the user instructions.
theFolderBrowser.Description = "Please browse to your GTAIV directory."
' Set theFolderBrowser object's ShowNewFolder property to false when
' the a FolderBrowserDialog is to be used only for selecting an existing folder.
theFolderBrowser.ShowNewFolderButton = False
' Optionally set the RootFolder and SelectedPath properties to
' control which folder will be selected when browsing begings
' and to make it the selected folder.
' For this example start browsing in the Desktop folder.
theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
' Default theFolderBrowserDialog object's SelectedPath property to the path to the Desktop folder.
theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' If the user clicks theFolderBrowser's OK button..
If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's
' SelectedPath property.
Me.FolderTextBox.Text = theFolderBrowser.SelectedPath
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles InstallButton.Click
My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, theFolderBrowser.SelectedPath)
End Sub
End Class
SnoX
When you call this line of code
My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, _
theFolderBrowser.SelectedPath)
the theFolderBrowser object cannot be referenced because it was a local variable inside another method. However, before exiting, you have copied the selected path into a textbox. You could use that textbox as destination of your copy
My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath, _
Me.FolderTextBox.Text )
And also keep in mind that CopyFile copies only one file, if you intend to copy an entire folder you need the CopyDirectory method. There is another important detail in this method:
If your files exist in the destination directory and you don't use the overload with the overwrite flag you will get an exception
My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath, _
Me.FolderTextBox.Text, True )