I copy some files to some destination and in the meantime I want to log the file names in a text file.
I'd like to use the Windows 7 Copy/Replace dialog in the case the files are already present in the destination folder (to keep users in a "known" environment).
But my problem is that I am not able to catch the 4 different events depending on the user's choice :
Replace
Do not copy
Copy with a different name
Cancel (for this one I can throw an exception and catch it)
Dim oFile As New StreamWriter(strTextFile)
For Each p In Me.Files ' List of custom class with file information like Path, Extension, etc...
Dim strFileName = Path.Combine(strDestinationFolder, p.FileName)
If File.Exists(strFileName) Then
Try
My.Computer.FileSystem.CopyFile(p.Path, strFileName, UIOption.AllDialogs, UICancelOption.ThrowException)
Catch ex As Exception
' I would like to catch user's choices here to react accordingly in the text file
End Try
Else
oFile.WriteLine(p.Path & ";" & p.FileName)
End If
Next
oFile.Close()
Thanks in advance.
Related
I know how to export to pdf using Crystal Report, but I don't want to preset file path, I want users can choose file path (and file name) when clicking Export button.
Do you know how to do that ?
Thank you very much,
Tai
Prompting the user for a save location would be something you'd implement as part of your app - this related question provides some answers on how to do that. Then, you'd just supply the path from the dialog to the Crystal Reports export API so it saves to that location.
Here is a partial code in my project. I hope it helps.
Dim saveFileDialog1 As New SaveFileDialog()
'saveFileDialog1.InitialDirectory = My.Computer.FileSystem.CurrentDirectory
saveFileDialog1.Filter = "PDF files (*.PDF)|*.PDF"
saveFileDialog1.FilterIndex = 1
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
'Here is where you write the file
Catch Ex As Exception
'MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
End Try
End If
Hello I have a program that imports an excel file using the oledb in vb.net. This program imports the excel spreadsheet into a datagridview. On opening the program will ask the user to choose an excel file to open, however if the user inputs nothing or presses the cancel button the program will crash. I'm trying to find a way to prevent a user from canceling or leaving the excel filename blank. I'm hoping this will be done using a try catch block but I'm not exactly familiar with try catch in vb.net. If anyone has any suggestions or solutions for this I would greatly appreciate it. This is what I found on MSDN.
If System.IO.File.Exists(filePath) = False Then
Console.Write("File Not Found: " & filePath)
Else
' Open the text file and display its contents.
Dim sr As System.IO.StreamReader =
System.IO.File.OpenText(filePath)
Console.Write(sr.ReadToEnd)
sr.Close()
End If
You could try a Try....Catch loop. Check this site out, should give you what you are looking for.
You could possibly use the Try....Catch loop and on the catch have an error message saying they must fill in the blank textbox.
I would suggest to take care of this at the source, when opening the file, and catching errors after the fact should be a last resort.
If filePath <> String.Empty And System.IO.File.Exists(filePath) Then
Try
'handle the opening of the file
Catch ex As Exception
Console.Write(ex.Message)
End Try
ElseIf filePath = String.Empty Then
Console.Write("Nothing Entered")
Else
Console.Write("File Not Found: " & filePath)
End If
First attempt at a VB.NET application. I am trying to create an application to copy standard configurations for an application then customize certain settings for that user. I have most of the application working fine but I'm having an issue with resource files.
Because the number of files may change I'm using a sub-folder for my resources. I have the files set to content and always copy. I can get the following code to work in debug but it seems I'm not doing it properly for build.
For Each strFile In Directory.GetFiles(".\Files")
If Path.GetExtension(Path.GetFileName(strFile)) = ".vbs" Then
strDestination = strDataPath & "Scripts\"
Else
strDestination = strDataPath
End If
If Not Directory.Exists(strDestination) Then
Directory.CreateDirectory(strDestination)
End If
If My.Settings.chkForceOverwrite = True Then
Try
File.Copy(strFile, strDestination & Path.GetFileName(strFile), True)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Else
Try
File.Copy(strFile, strDestination & Path.GetFileName(strFile), False)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End If
Next
I have attempted several ways of copying the data but I can find nothing compatible, like the following.
Dim strFileName As String = Path.GetFileNameWithoutExtension(strFile)
File.WriteAllBytes(strDestination & strFile, My.Resources.strFileName, True)
Can someone point me in the proper and correct direction to do this? Currently the file types are 1 .vbs and 1 .exe.config file but I may need to add different file types in the future.
About this code ..
File.WriteAllBytes(strDestination & strFile, My.Resources.strFileName, True)
If you access some special folder such as Desktop and retrieve from app resource , you make it like this
File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & strFile, My.Resources.ResourceManager.GetObject(strFileName), True)
It seems the issue is this line For Each strFile In Directory.GetFiles(".\Files")
.\Files is referring to a relative path that doesn't exist when you move the .exe.
Okay, I still havenot figured out a way to resolve this problem. As simple as my program is, I am trying to list the directories in a treeview and then when an user clicks on it, I am listing all the files in an listview box. I am getting "Unauthorized Access Exception" when I click on 'My Document' folder and also on some other folders. How to resolve this? I am not even opening any files, just listing files from a directory I click on treeview.
When I use FolderBrowserDialog, I don't get this exception and am able to browse to MyDocuments Folder and even open a file.
Try
Dim strdirs() As String = IO.Directory.GetDirectories(ds)
tv1_temp(arr_ind(0)).Nodes.clear()
For Each d As String In strdirs
Dim ss = d.Substring(d.LastIndexOf(IO.Path.AltDirectorySeparatorChar) + 1)
Dim tn = New TreeNode(ss)
tn.Name = ss
tv1_temp(arr_ind(0)).Nodes.Add(tn)
Next
tv1_temp(arr_ind(0)).ExpandAll()
Catch ex As IO.IOException
Comment.Text = "Device not ready!"
Catch ex As System.UnauthorizedAccessException
Comment.Text = ("Unauthorized Access to this directory!")
End Try
Thank you!
We have to learn VB.NET for the semester, my experience lies mainly with C# - not that this should make a difference to this particular problem.
I've used just about the most simple way to save a file using the .NET framework, but Windows 7 won't let me save the file anywhere (or anywhere that I have found yet). Here is the code I am using to save a text file.
Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
... Build up output string ...
Try
' Try to write the file.
My.Computer.FileSystem.WriteAllText(saveLocation, output, False)
Catch PermissionEx As UnauthorizedAccessException
' We do not have permissions to save in this folder.
MessageBox.Show("Do not have permissions to save file to the folder specified. Please try saving somewhere different.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch Ex As Exception
' Catch any exceptions that occured when trying to write the file.
MessageBox.Show("Writing the file was not successful.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
The problem is that this using this code throws an UnauthorizedAccessException no matter where I try to save the file. I've tried running the .exe file as administrator, and the IDE as administrator.
Is this just Windows 7 being overprotective? And if so, what can I do to solve this problem? The requirements state that I be able to save a file!
Thanks.
This code:
Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
Is giving you the location of a folder. Then you're trying to save a file with the same name as the folder. Instead, I assume you want to save a file inside that folder:
Dim saveLocation As String = dialog.SelectedPath
saveLocation = Path.Combine(saveLocation, "SomeFile.txt")
That will create a file called "SomeFile.txt" inside the selected folder.
Alternatively, instead of using FolderBrowserDialog to choose a folder, use SaveFileDialog to select the actual file instead.