Auto update issue with drive C:/ in Vb.net - vb.net

I've created a program with Vb.net that includes an "auto-update" feature. I made it using File A Sync and it seems to work fine, But that's only when I set up the program in any drive except the "C:/" drive, But when I set up the program on the "C:/" drive, The download doesn't start at all. I do not know why. I also tried searching for this issue online but couldn't find any help.
Imports System.IO
Imports System.ComponentModel
Public Class Updater
Dim client As New WebClient
Private Sub Updater_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Downloading_Bar.EditValue = 0
Speed_lb.Visible = True
Speed_lb.Text = " 0 kb/s"
Size_lb.Visible = True
Size_lb.Text = " 0 MB's / 0 MB's"
Try
AddHandler client.DownloadProgressChanged, AddressOf Client_ProgressChanged
AddHandler client.DownloadFileCompleted, AddressOf Client_DownloadCompleted
client.DownloadFileAsync(New Uri(Program_URL), Application.StartupPath & "\Changer NewVersion.exe")
Downloading_Bar.Properties.Step = 1
Downloading_Bar.Properties.PercentView = True
Downloading_Bar.Properties.Maximum = 100
Downloading_Bar.Properties.Minimum = 0
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Try
Speed_lb.Text = String.Format("{0} kb/s", (e.BytesReceived / 1024.0).ToString("00"))
Downloading_Bar.EditValue = e.ProgressPercentage.ToString()
Downloading_Bar.PerformStep()
Downloading_Bar.Update()
Size_lb.Text = String.Format("{0} MB's / {1} MB's", (e.BytesReceived / 1024.0 / 1024.0).ToString("0.00"), (e.TotalBytesToReceive / 1024.0 / 1024.0).ToString("0.00"))
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
client.CancelAsync()
client.Dispose()
End Try
End Sub
Private Sub Client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If Downloading_Bar.EditValue = 100 Then
Try
My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Changer.exe", "Changer OlderVersion.exe")
My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Changer NewVersion.exe", "Changer.exe")
Process.Start(Application.ExecutablePath)
Process.GetCurrentProcess.Kill()
Me.Dispose()
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub Updater_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
client.CancelAsync()
client.Dispose()
End Sub
End Class

Related

How to print an access report protected by password?

I have an password-protected Access database, and I need to print a report in this database from my VB.Net code. How can I do it? I have this so far:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAfdrukken.Click
Dim moApp As Access.Application
Dim acViewPreview As Access.AcView
Try
moApp = DirectCast(GetObject(, "Access.Application"), Access.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
Else
MessageBox.Show(ex.Message, "VB/Office Guruâ„¢ Access Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
moApp.Visible = True
moApp.OpenCurrentDatabase("D:\media\urine (1).ACCDB")
moApp.RunCommand(Access.AcCommand.acCmdAppMaximize)
moApp.DoCmd.Maximize()
moApp.DoCmd.SetWarnings(False)
moApp.DoCmd.OpenReport("report1bw", acViewPreview)
moApp.CloseCurrentDatabase()
End Sub
Just use the password when opening the file.
moApp.OpenCurrentDatabase("D:\media\urine (1).ACCDB", bstrPassword := "somepassword")

How to delete a file used by another Process

Public Class frmMainCetegory
Private Sub frmMainCetegory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PosDatabaseDataSet.tblItemMainCategory' table. You can move, or remove it, as needed.
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.CenterToParent()
Me.btnAddNew.Select()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
Try
Me.TblItemMainCategoryBindingSource.AddNew()
Me.txtMainCategory.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
If Me.txtMainCategory.Text = "" Then
MsgBox("Please Enter Main Category Name!", vbInformation)
Exit Sub
End If
Me.imgMainCategoryImage.Image.Save(Application.StartupPath & "\Images\" & Me.txtMainCategory.Text & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.TblItemMainCategoryBindingSource.EndEdit()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.btnAddNew.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
Dim MainCatName As String
MainCatName = Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value
If Me.TblItemMainCategoryDataGridView.Rows.Count - 1 > 0 Then
'check before delting, if this main category is used in sub category then It can not be deleted.
Dim I As Integer
I = frmSubCategories.TblItemSubCategoryTableAdapter.qryCountMainCategoryUnderSubCategory(MainCatName)
If I > 0 Then
MsgBox(MainCatName & " is used under Sub Category(s), Please Delete Sub Category(s) First!", vbOK)
Exit Sub
End If
End If
Me.TblItemMainCategoryBindingSource.RemoveCurrent()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
'Delete the Main Category Image if this exists
If System.IO.File.Exists(Application.StartupPath & "\Images\" & MainCatName & ".JPEG") Then
System.IO.File.Delete(Application.StartupPath & "\Images\" & MainCatName & ".JPEG")
MsgBox("Image Deleted Successfull")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Try
Me.OpenFileDialog1.Filter = "Bitmaps(*.JPG, *.PNG, *.JPEG, *.BMP, *.TIF, *.GIF, *.TIFF)|"
Me.OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
Dim img As String = Me.OpenFileDialog1.FileName
Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(img)
Me.btnSave.Select()
Else
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TblItemMainCategoryDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles TblItemMainCategoryDataGridView.SelectionChanged
Try
Me.imgMainCategoryImage.Image = DirectCast(Image.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG").Clone(), Image)
' Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG")
Catch ex As Exception
Me.imgMainCategoryImage.Image = My.Resources.imgEmpty 'if image path not found then select empty image
End Try
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
Above code give me an error message:
image is used in another process, can't be deleted
when I delete the image file. The image is assigned to a PictureBox with selection event of the DataGridView. Since I have deleted the DataGridView row, as shown in the above code, it changes the image in the PictureBox.
Here I am unable to delete the image file related to the removed row.
Please guide and let me know if you need any further details.

Random error thrown on downloading a file

I would really appreciate if someone could help me with this. If I click on the send button, it can't download the log.txt after that...
Imports System
Imports System.IO
Imports System.Collections
Imports System.Security.Principal
Imports System.Runtime.InteropServices
Imports System.Net
Public Class Form1
Public sent As Boolean = False
Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Int32, _
ByVal uFlags As Int32, _
ByVal dwitem1 As Int32, _
ByVal deitem2 As Int32) As Int32
Private Sub send_Click(sender As Object, e As EventArgs) Handles send.Click
'If a message has been sent
sent = True
Try
Dim writer As New System.IO.StreamWriter(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
sent = False
writer.Write(input.Text)
Try
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://***/c/log.txt"), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.DeleteFile
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
ftpResponse = ftp.GetResponse()
ftpResponse.Close()
Catch ex As Exception
End Try
My.Computer.Network.UploadFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt", New Uri("ftp://***/c/log.txt"), "user", "pass", False, 30000, FileIO.UICancelOption.DoNothing)
input.Text = ""
Catch ex As Exception
End Try
sent = False
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c") Then
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c")
Else
My.Computer.FileSystem.DeleteDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c", FileIO.DeleteDirectoryOption.DeleteAllContents)
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c")
End If
firstcheck()
Threading.Thread.Sleep(500)
End Sub
Private Sub taskbarcheck_Tick(sender As Object, e As EventArgs) Handles taskbarcheck.Tick
If File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\chax.txt") Then
Me.Opacity = 100
Me.Show()
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Desktop & "\chax.txt")
Catch ex As Exception
End Try
Threading.Thread.Sleep(50)
SHChangeNotify(&H8000000, &H1000, 0, 0)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_hide.Click
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Desktop & "\chax.txt")
Catch ex As Exception
End Try
Me.Opacity = 0
Me.Hide()
Threading.Thread.Sleep(50)
SHChangeNotify(&H8000000, &H1000, 0, 0)
End Sub
Private Sub ftpstuff_Tick(sender As Object, e As EventArgs) Handles ftpstuff.Tick
'Try
'delete file if exists
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
Catch ex As Exception
End Try
'download file
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
Catch ex As Exception
End Try
'My.Computer.Network.DownloadFile("http://***/c/log.txt", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt", "", "", False, 30000, False)
Dim myWebClient As New WebClient()
If Not sent = True Then
myWebClient.DownloadFile("http://***/c/log.txt", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
log.Text = File.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
End If
End Sub
Private Sub firstcheck()
If Not File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt") Then
My.Computer.Network.DownloadFile(New Uri("ftp://***/c/log.txt"), My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt", "user", "pass", False, 30000, True, FileIO.UICancelOption.DoNothing)
End If
End Sub
I tried it every way that i knew of... The "An unhandled exception of type 'System.Net.WebException' occurred in System.dll" is thrown when it tries to download the file after i click the send button.
Thanks for your help in advance!
Yes, it was the stream not closing. Thanks!
If you get an exception before the stream is closed, the code jumps straight to the catch block and the stream is never closed. I presume that the file is locked until the program terminates.
Try adding a command to close the file in the Catch block as well as in your Try block

Multiple Definition with Identical Signatures

Dim WithEvents client As New WebClient
Private Sub DirectX9ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirectX9ToolStripMenuItem.Click
Try
client.DownloadFileAsync(New Uri("http://download1588.mediafire.com/y9phz64wph4g/aqlp1m71mvuwo74/Direct+X+11+Update.rar"), "C:\Documents and Settings\All Users\Documents\Direct X 11 Update.RAR")
Catch ex As Exception
MsgBox("File already exists or is corrupted!")
End Try
End Sub
Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles client.DownloadFileCompleted
MsgBox("Direct X 9 has been successfully downloaded!", MsgBoxStyle.Information)
Label48.Visible = False
Label49.Visible = False
ProgressBar3.Visible = False
Label48.Text = "0 &"
Label49.Text = "0 / 0"
ProgressBar3.Value = 0
End Sub
Private Sub client_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles client.DownloadProgressChanged
Label48.Visible = True
Label49.Visible = True
ProgressBar3.Visible = True
Label48.Text = ProgressBar3.Value & "%"
Label49.Text = e.BytesReceived & " / " & e.TotalBytesToReceive
ProgressBar3.Value = e.ProgressPercentage
End Sub
Private Sub GeForceExperienceV2000ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GeForceExperienceV2000ToolStripMenuItem.Click
Try
client.DownloadFileAsync(New Uri("http://download903.mediafire.com/w91mlqdf6clg/a540a35f5ddcpbn/GeForce_Experience_v2.2.2.0.exe"), "C:\Documents and Settings\All Users\Documents\GeForce_Experience_v2.2.2.0.exe")
Catch ex As Exception
MsgBox("File already exists or is corrupted!")
End Try
End Sub
Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles client.DownloadFileCompleted
MsgBox("GeForce_Experience_v2.2.2.0 has been successfully downloaded!", MsgBoxStyle.Information)
Label48.Visible = False
Label49.Visible = False
ProgressBar3.Visible = False
Label48.Text = "0 &"
Label49.Text = "0 / 0"
ProgressBar3.Value = 0
End Sub
End Class
You got your answer in comments but just going to explain to make sure you understand what you can and can't do.
When you create a method you name it so that when you call it your program knows which one it is you want to call. When you name 2 methods the same way it does not know which one to use when it is called.
The only way to have 2 methods with the same name is if they have a different "signature" that means it can't be 100% identical. Different parameters will make your method's signature different even if they are called the same

Why isn't my vb.net code saving a row to my Access database? (Error)

I'm trying to add a 'coupling' to my database with the following code. It give me the error "Update requires a valid InsertCommand when passed DataRow collection with new rows."
Private Sub AddCoupling_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CouplingsTableAdapter.Fill(TestDatabaseDataSet.Couplings)
End Sub
Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim newCoup As DataRow = TestDatabaseDataSet.Tables("Couplings").NewRow
newCoup.Item("Type") = ComboBoxType.Text
newCoup.Item("Part Number") = TextPartNumber.Text
newCoup.Item("Description") = TextDescription.Text
newCoup.Item("Rubber Type") = ComboBoxRubberType.Text
newCoup.Item("TKN") = Integer.Parse(TextTKN.Text)
newCoup.Item("TKmax") = Integer.Parse(TextTKmax.Text)
newCoup.Item("TKW") = Integer.Parse(TextTKW.Text)
newCoup.Item("Ambient Temp") = Integer.Parse(TextTemp.Text)
newCoup.Item("PKW") = Integer.Parse(TextPKW.Text)
newCoup.Item("Max Speed") = Integer.Parse(TextSpeed.Text)
Try
TestDatabaseDataSet.Tables("Couplings").Rows.Add(newCoup)
CouplingsTableAdapter.Adapter.Update(TestDatabaseDataSet) ' ERROR HERE '
MessageBox.Show("Saved!")
Close()
Catch ex As Exception
MessageBox.Show("Failed to save to database. E:" & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub