Delete relative path in Visual Basic - vb.net

I'm essentially trying to figure out a simple method for deleting a relative path in vb. I want a user to be able to run the program and delete a specific folder within the AppData/Local folder on Windows. The purpose of the program is to remove Google Chrome user data when the user is finished with their session. I'm running in to trouble with:
My.Computer.FileSystem.DeleteDirectory
I'm trying to use this method to remove all folders within the \Google folder. The problem is, that since the \Google folder is relative, if anybody besides me tries to use it, it simply wont work since I would use: My.Computer.FileSystem.DeleteDirectory("C:\Users\Erik\AppData\Local\Google", FileIO.DeleteDirectoryOption.DeleteAllContents). How can I modify this program to remove the \Google folder under whichever user profile the program happens to be executed under?

You can do this with the Environent Paths:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\Google")
End Sub
End Class
This gets the Local Appdata Path for the current User.

Related

The audio file isn't being found/recognised in my My.Resources but it is there

This is for a quiz game that I am doing for a class assignment and I am a beginner in using both Visual Studio and the language vb.net.
I am trying to get this sound to play when the form comes up, that's all. The file itself plays fine when I click on it.
Error: 'crowdbooing' is not a member of 'VS_BeatTheBrain_JKEM.My.Resources'.
My Code:
Public Class frm_loser
Friend WithEvents player As New System.Media.SoundPlayer
Private Sub frm_loser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
player.Stream = My.Resources.crowdbooing
player.Play()
End Sub
End Class
I have checked and the audio file (.wav) is definitely in the resource folder, I have already tried:
Removing it and re-adding it
Re-downloaded it
Tried another computer
Teacher tried it on theirs and it worked (the code, I believe they used a different audio file but from the same source that I got mine from)
Renamed it
Searched the internet for a solution
Tried changing 'Build Action' property on audio file to either 'None' or 'Embedded Resource' or 'Resource'. (Didn't Work)
Proof Audio File is in Resources, As Shown
Proof 2
What it shows when I hover over the code
Update: Problem has not been solved on the original project. Created a new project, copy & pasted forms from original into the new one and the sound now works.

How can I handle application startup without using default form mybase.load

I am having trouble figuring out how to run code on application startup. The scenario is, I need to check if a user setting exists, if it does not, then I need to open a company configuration form. I tried creating a module to store application level events, but get an error:
BC31418 Visual Basic AND VB.NET in modules must specify a 'WithEvents' variable qualified with a single identifier.
Module ApplicationEvents
Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If My.Settings.xmlpath = "" Then
Dim f As New CompanySetup()
f.ShowDialog()
f.Dispose()
End If
End Sub
End Module
I really do not want to put the IF statement into the default form mybase.load handler, since this application is in early stages, and the default form may change, leading me to have to move this code around. Any help on this error, or suggestions of how to get the IF statement to occur on application startup without tying it into a sub on the default form would be greatly appreciated.
Get rid of that module. Open the Application page of the project properties and click the View Application Events button. That will open the proper code file in which to create a Startup event handler. You can do that using the second and third drop-downs in the Navigation Bar at the top of the code window.

Automatic login when run the program second time

Recently try to use a webbrowser component.
when I click button, it will navigate to facebook.com. after I login on first time and stop the program. and then when I run the program second time I don't need to fill the email and password. why I don't need to fill an email and password textbox ? thanks
Here's the code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate("m.facebook.com")
End Sub
End Class
Note: I will delete this post, because it doesn't help the community. Just my curiosity. Thanks
how do i need to re-type the email password again when 2nd starting the program
You have two options:
The first is to save the CookieContainer associated with the WebBrowser to disk and reload it when you restart your program. There is no straightforward way to serialize cookies to disk or other storage method - that is up to you.
Instruct the WebBrowser object to make use of the user's Windows profile Internet Explorer cookie collection which is shared with Internet Explorer and other programs that also opt-in to sharing state (I do not know if this also applies to the Edge browser, I suspect it does not). See here for instructions on how to do this: Use cookies from CookieContainer in WebBrowser

Exception thrown on bitmap declaration under weird circumstances

I encountered this problem and through much frustration isolated it to this.
When I drag a file on my exe from another folder, a bitmap declaration that has nothing to with the command line arguments throws an exception "System.ArgumentException: Parameter is not valid".
This does not happen when:
The file is dragged from the same folder as exe
The file is dragged on a shortcut of the exe
Here is the minimum code that produces this error:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim logo As New Bitmap("logo.png") 'an image in the same dir as exe
Me.BackgroundImage = logo
Catch ex As Exception
MessageBox.Show("Exception: " & ex.Message)
End Try
End Sub
End Class
If you provide just a file name then it is assumed that that file resides in the current directory. The current directory is not always the same at startup and it can change while an application is running too.
I'm guessing that that file is in the same folder as your EXE. In that case, when the code works the current directory must be the application folder and in cases where it fails the current directory must be some other folder.
We often see similar issues when Process.Start is used run a game. Many games do as you have and assume that the current directory is the application folder. If you run the game from the commandline or the like then it will be. If you use Process.Start to run the game from code, the new process will actually inherit the current directory of your application and that sort of code will fail.
The solution is to ALWAYS be explicit about the path of a file. If you want to open a file that is in your application folder then specify exactly that:
Dim logo As New Bitmap(IO.Path.Combine(Application.StartupPath, "logo.png"))
That code doesn't rely on the current directory being a specific folder so will be unaffected if it isn't.

Linking to internal html pages with LinkLabel

Visual Basic:
Trying to get my VB program to run an internal HTML file without it needing to navigate back the the C: drive.
Using:
Private Sub frmMalphite_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LinkLabel1.Links.Add(6, 4, "C:\Users\User\Desktop\Test\Test1\Test2.html")
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start(e.Link.LinkData.ToString())
End Sub
I can get the html page to run, however it would not work if I opened the project on another computer, because the absolute path given. Looking for a work around so I don't have to change the path on every computer I use to work on the project.
If the file exists on a drive on MachineA, then the only way you can open this from MachineB is to put the file on a shared drive and access the file via this share.
This will work from any machine that has permissions to read that share.
Something like:
LinkLabel1.Links.Add(6, 4, "\\MachineA\SharedDrive\Test2.html")
You may have access to the hidden administrative share on MachineA (\\MachineA\c$) from MachineB but you can't rely on it.