Why does this command sometimes throw array bounds exceptions when it contains no arrays - vb.net

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?

Related

VB.net SQL output loop rename file error on next loop

new to the stack here. I am developing some tools for work that allow us to pull information from a database and then operate on it with additional custom code. Since I need the output to not have a header, I want to have one file (as defined by schema.ini) that I will temporarily write the data into then copy and rename it before starting again. Using VS2017, when I debug it spits out the error that it cannot find RawC.txt after the first iteration. I cannot seem to figure out why. It is probably something simple but I have been unable to locate online. Can anyone help me out here? Below is the block I am having trouble with:
For each of the three files, output the sorted list. Schema has the correct format
For index = 0 To 2
If index = 0 Then
whicharr = arrSt(1)
ElseIf index = 1 Then
whicharr = arrSe(1)
ElseIf index = 2 Then
whicharr = arrVi(1)
End If
stopFile = "SELECT " & complist(index) & " INTO [Text;Database=" & TMPath & "].[RawC.txt] FROM [" & whicharr & "] ORDER BY " & complist(index)
cmd = New OleDbCommand(stopFile, conn)
cmd.ExecuteNonQuery()
'Now Copy and rename this file
My.Computer.FileSystem.CopyFile(TMPath & "\RawC.txt", TMPath & "\" & whicharr & ".txt")
'And delete the old RawC.txt file
My.Computer.FileSystem.DeleteFile(TMPath & "\RawC.txt")
Next
The final output from this loop should be three unique files that I will pass to another code that will perform some math on it.

Download File and Extract Immediately - File is being used by another process

I'm looping through a list of zip files and downloading them from a WebClient. I'm trying to extract them immediately after they download, but getting an error that the file is being used by another process.
Error: The process cannot access the file 'c:\temp\test.zip' because it is being used by another process.
The only thing that I can think of is that maybe the file is still "in use" for a short time after being downloaded? DownloadFile does block ("This method blocks while downloading the resource." - http://msdn.microsoft.com/en-us/library/ez801hhe(v=vs.110).aspx), so I can't see it being an issue with the file still downloading.
Any ideas on a workaround?
Using client As New WebClient
oLog.Info("Downloading " & strFileServer & " to " & strFileLocal & "...")
If Not Directory.Exists(strPathLocal) Then Directory.CreateDirectory(strPathLocal)
client.DownloadFile(strFileServer, strFileLocal)
End Using
Using archive As ZipArchive = ZipFile.OpenRead(strFileLocal)
For Each entry As ZipArchiveEntry In archive.Entries
oLog.Info("Extracting " & entry.FullName & " to " & strPathLocal & "...")
entry.ExtractToFile(strFileLocal, True)
Next
End Using

Try-catch issue

I am running across an issue in my program. I am reading a folder containing csv files and entering the data into a list of arrays. I have data enclosed within quotes.
However, the data is not pristine, and there are fields that have spare quotes that are causing issues.
I have set up a try/catch function that shunts the bad data into a manual verification routine. However, today the try/catch has started clearing the stack and reprocessing all the files from the top of the folder.
I have been using the messageboxes to trace exactly when the stack is getting cleared. It's happening at the start of the try/catch statement. It's not making it to the first messagebox.
Does anyone have any tips for continuing the program without losing the stack data?
files_ = Directory.GetFiles(path_, "*.ldf") 'picks out only the .ldf files
For i = 0 To files_.Length - 1 'i is the index for the number of ldf files in the folder. Number is i+1
i_string = CStr(i)
Using filereader1 As New Microsoft.VisualBasic.FileIO.TextFieldParser(files_(i), System.Text.Encoding.Default)
filereader1.TextFieldType = FieldType.Delimited
filereader1.Delimiters = New String() {","}
filereader1.HasFieldsEnclosedInQuotes = True
While Not filereader1.EndOfData
Try
split_string = filereader1.ReadFields()
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MessageBox.Show(ex.Message & " : " & FileName & " ; " & filereader1.ErrorLine & " LIST")
error_throw = filereader1.ErrorLine
MsgBox("LIST error throw")
Throw_Error = True
End Try

Copy Resources to disk

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.

Open Word Document From Dynamic Directory VB.Net

I have a program for which I have developed a user guide. I have placed this user guide within the project directory. I created a MenuStrip Item by which to open the user guide in Word on the user's machine. I was successfully able to do this with the following code:
Try
userGuide = MSWord.Documents.Open("C:Users\administrator\Documents\VisualStudio2010\Project3\UserGuide.doc")
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
Catch ex As Exception
MsgBox("An error has prevented the document from opening. The document may not be available." & vbCrLf & vbCrLf & _
"Please try one of the following options:" & vbCrLf & _
"- Check to see if the document is already open" & vbCrLf & _
"- Restart the program")
End Try
The problem is, the path used to open the file will not exist on the users machine. This is a standalone system, so no file share can be created in which to place the document, therefore no common path can be coded.
Is there a way to code dynamic paths? Perhaps something like:
userGuide = MSWord.Documents.Open("%windir%\UserGuide.doc")
Thanks!
if the document will be stored relative to the install path of the application executable, then start with the path of the exe:
Dim path As String
path = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim docPath as String;
docPath = Path.Combine(path,"UserGuide.doc");