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

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

Related

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?

File doesn't zip using 7zip command in vb.net

I am trying to zip a file in vb.net. I am using 7zip to do this. I am using the Process.Start method.
Here is the zip line of my code:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip" + (ChosenFile & "\" & "SavedFiles") + NewFileName1)
No error that I know of happens, however when I look through the path, I cannot find the zipped files.
ChosenFile & "\" & "SavedFiles" is the destination folder.
NewFileName1 is the file to be zipped
you must do a space after "-tzip" and u forgot use " to folders with spaces like that:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip " & (ControlChars.Quote & ChosenFile & "\" & "SavedFiles" & ControlChars.Quote) & " " & ControlChars.Quote & NewFileName1 & ControlChars.Quote &)
Probably its obvious, but you have the rights to access those files with the user who runs the script?
Else, can you run the script manually step by step to debug where it fails?

My VB.NET code to download a file doesnt download the file entirely

so i have been having some issues with downloading a file in vb.net. its for an auto-updater but it doesn't download it entirely. It's file size is only a few KB when the program is about 2 MB. This is the code i have currently:
Try
My.Computer.Network.DownloadFile("dropboxlink directly to the file 'dl=1'", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/Updated/" & LatestVersion & ".exe")
MsgBox("The update has been downloaded!" & vbNewLine & "The application will now Exit and Re-open with the new version! :D")
Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/Updated/" & LatestVersion & ".exe")
Application.Exit()
Catch ex As Exception
MsgBox("OH MY! An error occured while trying to download the update.")
End Try
I just don't understand why it does this. its very aggravating!
anyone have the same problem at some point and know what to do to fix it or what im doing wrong?
-thanks, Nick

killing process used by vb.net

I need some help with my program. I want to rewrite the data on my .txt file but an error occurs:
The process cannot access the file 'C:\Users\AARVIII\Documents\Visual Studio 2010\Projects\PROJECT\WindowsApplication3\bin\Debug\ORDERS\aa.txt' because it is being used by another process.
Here is the code:
Sub WRITEDATA()
Dim write As New System.IO.StreamWriter("ORDERS\" & TBFNAME.Text + "" + TBLNAME.Text & ".txt", False)
write.WriteLine(TBFNAME.Text)
write.WriteLine(TBLNAME.Text)
write.WriteLine(TBEADD.Text)
write.WriteLine(TBEADD2.Text)
write.WriteLine(TBADDRESS.Text)
write.WriteLine(TBCONTACT.Text)
write.close()
End Sub
I used a StreamReader to get the data which had already been put in that text file. Please help me figure out how to kill that process so that I can rewrite my data.
It is very possible that your app (on another thread?) is the culprit. First, to make sure you release the resource, make sure to wrap your code in a using block:
Using Dim write As New System.IO.StreamWriter("ORDERS\" & TBFNAME.Text + "" + TBLNAME.Text & ".txt", False)
write.WriteLine(TBFNAME.Text)
write.WriteLine(TBLNAME.Text)
write.WriteLine(TBEADD.Text)
write.WriteLine(TBEADD2.Text)
write.WriteLine(TBADDRESS.Text)
write.WriteLine(TBCONTACT.Text)
End Using
Additionally, you may want to see this thread: .NET Asynchronous stream read/write

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");