Copy Resources to disk - vb.net

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.

Related

vb.net search directory for files containing *.G(num) but NOT *.GP(num)

I'm fairly familiar with bash, but I'm very, ***very**** new to vb.net. I'm searching for an easy way to find files in a folder that end with .G1, .G2, .G3, etc. but NOT .GP1, .GP2, .GP3, etc. Then for each file I need to copy it to another folder using a different file name but the same extension. I've managed to figure this out for the unique files, but there will be an undefined number of these depending on the project and I need to make sure that I get them all. Hard coding is possible, but very, very ugly. Any suggestions?
Here's the remnants of a failed attempt:
Public Sub FindGFiles()
FileList = IO.Directory.GetFiles(searchDir, ".G[1-99]" + , IO.SearchOption.AllDirectories)
For Each foundfile As String In FileList
If foundfile.Contains(".G#") Then
'copy file somehow and retain file extension
Else
MsgBox("No match")
End If
Next
End Sub
The GetFiles-method does only support * and ? wildcard characters.
So you have to get all files with a *.G*-extension first.
In the For Each-loop one can then use the Like-operator to check the desired pattern:
Public Sub CopyGFiles(searchDir As String, destDir As String)
Dim fileList As String() = IO.Directory.GetFiles(searchDir, "*.G*", IO.SearchOption.AllDirectories)
Dim fileName As String
Dim extension As String
For Each foundfile As String In fileList
fileName = IO.Path.GetFileNameWithoutExtension(foundfile)
extension = IO.Path.GetExtension(foundfile)
If extension Like ".G#" OrElse
extension Like ".G##" Then
'copy file to destination, append "_new" to the filename and retain file extension
IO.File.Copy(foundfile, IO.Path.Combine(destDir, fileName & "_new" & extension))
Else
'pattern not matched
End If
Next
End Sub
The method-call would then be as follows:
CopyGFiles("C:\Temp", "C:\Temp\Dest")
This should be done inside a Try/Catch as different exceptions can occur when working with files.
Try
CopyGFiles("C:\Temp", "C:\Temp\Dest")
Catch ex As Exception
MessageBox.Show("An error occured" + vbCrLf + ex.Message)
End Try

Why does this command sometimes throw array bounds exceptions when it contains no arrays

I have a process that loops through an array which is a list of PDF filenames, for each file it must write a template PDF which is stored in the same folder as the exe and copy this to a network share as "pdffilename"~001.pdf and then copy the data file from an S3 bucket to the network share as "pdffilename"~002.pdf. In some cases, the file won't exist but we would still need the separator and sometimes the code would just be writing multiple separators (~001) and no data files but when this happens I randomly get failures in writing the separator pdf.
This works fine when running on my laptop through VS but when I deploy the app up to our application server it fails when there are lots of missing files.
If sFile(0) <> "999-Envelopes" Then
Try
boxTriggerReqd = True
My.Computer.FileSystem.CopyFile("doc_sep.pdf", OutputFolder & "\" & Replace(oFileName, "~002.pdf", "~001.pdf"), True)
Log.writeline("Document Separator written:" & vbTab & OutputFolder & "\" & Replace(oFileName, "~002.pdf", "~001.pdf"))
Catch ex As Exception
Call EndTask(ex.Message, "Unable to transfer Document separator", 11)
End Try
Else
envTriggerReqd = True
End If
Try
dlResponse = s3client.GetObject(GetFile)
With dlResponse
.WriteResponseStreamToFile(OutputFolder & "\" & oFileName)
.Dispose()
End With
Log.WriteLine("Data file written:" & vbTab & vbTab & OutputFolder & "\" & oFileName)
Catch ex As Exception
results.WriteLine(sFile(1))
Call EndTask(ex.Message, targetFile, 12)
results.Flush()
End Try
The error log shows
****** S3 PULL ******
Index was outside the bounds of the array.
Unable to transfer Document separator
****** S3 PULL ******
Where it has called EndTask contains the "Unable to transfer Document separator" so it has happened in one of three commands
I'm assuming its the CopyFile command but there is no reference to an array in any of those three. Is it possibly a memory error on the server itself?

Many instances of the same process writing to the same log file

I am kicking off a number of instances of the same process and the issue is that they all write to the same log file. I know it is not a good practice and was wondering what can I do to avoid possible issues. Here is the procedure I use to write to file:
Sub WriteToErrorLog(ByVal Msg As String)
Dim path As String
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim strFile As String = System.IO.Path.Combine(path, "Log_" & DateTime.Today.ToString("dd-MMM-yyyy") & ".txt")
Dim sw As StreamWriter
Dim fs As FileStream = Nothing
Try
If (Not File.Exists(strFile)) Then
fs = File.Create(strFile)
fs.Close()
End If
sw = File.AppendText(strFile)
sw.WriteLine(Msg & vbcrlf)
Catch ex As Exception
MsgBox("Error Creating Log File")
MsgBox(ex.Message & " - " & ex.StackTrace)
Finally
sw.Close()
End Try
End Sub
I would appreciate any suggestions/improvements. thanks!
As I have said in my comment, the scenario of multiple access to the same file resource should be handled carefully and probably the best solution is to use a well tested log library like Log4Net or NLog.
In any case you could improve your code in a couple of point
Sub WriteToErrorLog(ByVal Msg As String)
Dim path As String
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim strFile As String = System.IO.Path.Combine(path, "Log_" & DateTime.Today.ToString("dd-MMM-yyyy") & ".txt")
Dim retry as Integer = 3 ' this could be changed if you experience a lot of collisions.'
Dim sw As StreamWriter = Nothing
While retry > 0
Try
Using sw = File.AppendText(strFile)
sw.WriteLine(Msg & vbcrlf)
End Using
Exit While
Catch ex as Exception
retry -= 1
End Try
End While
' If retry has reached zero then we have exausted our tentatives and give up....'
if retry = 0 Then
MessageBox.Show("Error writing to Log File")
End if
End Sub
I have removed all the part that check if file exists and then create it. This is not necessary because as the documentation explains, File.Append is the same that calling StreamWriter(file, true) and this means that if the file doesn't exist it will be created.
Next, to try to handle possible collision with other process writing to the same file concurrently, I have added a retry loop that could get access to the log file just after another process finishes.
(this is really a poor-man solution but then it is better to use a well tested library)
It is important to enclose the opening and writing of the file inside a using statement that closes and disposes the Stream also in case of exceptions. This is mandatory to be sure to leave the file always closed for the other processes to work.

Catching events after calling the FileSystem.FileCopy UI dialog

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.

ASP.NET How do I wait for file upload/release?

I've got ASP.NET intranet application written in VB. It gets a file from the user, and then depending on a few different cases it may create a few copies of the file as well as move the original.
Unfortunately I've come across a case where I get this error:
Exception Details: System.IO.IOException: The process cannot access the file
'\\some\dir\D09_03_5_180_0.000-6.788.png' because it is being used by
another process.
Which is thrown by My.Computer.FileSystem.CopyFile. And that's fine that it's being used by another process - it may still be saving/downloading from the user or trying to copy while another thread(?) is copying, I don't really care about that, what I want to know:
Is there any way that I can tell VB to wait to copy (also move) the file until the file is no longer in use?
Thanks
Test if the file is in use and the do what you need to do.
Public Sub WriteLogFile(ByVal pText As String, ByVal psPath As String, ByVal psName As String)
Dim strFullFileName As String
Dim Writer As System.IO.StreamWriter
Dim Fs As System.IO.FileStream
Try
Dim DirectoryHandler As New System.IO.DirectoryInfo(psPath)
strFullFileName = psPath & "\" & psName & Date.Today.Month.ToString & "-" & Date.Today.Day.ToString & "-" & Date.Today.Year.ToString & ".txt"
If Not DirectoryHandler.Exists() Then
Try
Monitor.Enter(fsLocker)
DirectoryHandler.Create()
Finally
Monitor.Exit(fsLocker)
End Try
End If
Try
If CheckIfFileIsInUse(strFullFileName) = True Then
Thread.Sleep(500) ' wait for .5 second
WriteLogFile(pText, psPath, psName)
If Not Fs Is Nothing Then Fs.Close()
If Not Writer Is Nothing Then Writer.Close()
Exit Sub
End If
Monitor.Enter(fsLocker)
Fs = New System.IO.FileStream(strFullFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Write)
Writer = New System.IO.StreamWriter(Fs)
Writer.WriteLine(Date.Now.ToString & vbTab & "ProcessID: " & Process.GetCurrentProcess.Id.ToString() & vbTab & pText)
Writer.Close()
Fs.Close()
Finally
Monitor.Exit(fsLocker)
End Try
Catch ex As Exception
Dim evtEMailLog As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
evtEMailLog.Source = Process.GetCurrentProcess.ProcessName.ToString()
evtEMailLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error)
Finally
If Not Fs Is Nothing Then Fs.Close()
If Not Writer Is Nothing Then Writer.Close()
End Try
End Sub
Public Function CheckIfFileIsInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
FileClose(F)
Catch
Return True
End Try
End If
End Function
Hmm... not directly.
What most implementations are doing, is making a retry of copying the file, with a small timeframe (some seconds)
if you want to make a nice UI, you check via Ajax, if the copying process went well.
Well, it turns out that waiting would not work in this case:
When trying to copy a file you cannot copy a file from one location to the same location or it will throw an error (apparently). Rather than just pretending to copy the file, VB actually tries to copy the file and fails because the copy operation is trying to copy to the file it's copying from (with overwrite:=True at least).
Whoops!