I work on my VB application on visual studio 2012
I created a button, On clicking it, it plays a file on my PC ("D:\My Project\Sound_01.wav")
so the code will be like that:
**Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
My.Computer.Audio.Play("D:\My Project\Sound_01.wav")
End Sub**
My problem is that I need to make setup file for that app (using install shield 2015) and the file location will return error because the destination PC may not have the same location("D:\My Project\Sound_01.wav")
can anyone advise me how to do that?
add the file to your application path so you can use following:
My.Computer.Audio.Play(Application.StartupPath & "\Sound_01.wav")
Related
I've been trying to connect a Microsoft access database file to my Visual Basic application. When i try to run the application i get an error: "The Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine" i am using microsoft visual basic 2012.
Public Class purchases
Private Sub Purchases_fileBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Purchases_fileBindingNavigatorSaveItem.Click
Me.Validate()
Me.Purchases_fileBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TNJ_Retail_Management_SystemDataSet)
End Sub
Private Sub purchases_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TNJ_Retail_Management_SystemDataSet.Purchases_file' table. You can move, or remove it, as needed.
Me.Purchases_fileTableAdapter.Fill(Me.TNJ_Retail_Management_SystemDataSet.Purchases_file)
End Sub
End Class
Check link for screenshot of output. 1
Does anyone have any idea?
Was able to correct the error simply by downloading Microsoft Access Database Engine and installing it in my PC. I then opened my project, rebuild it and run. Gave expected results. Click here to open site to download the Microsoft Access Database Engine
I have done a simple VB application with this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim procName As String = Process.GetCurrentProcess().ProcessName
Dim processes As Process() = Process.GetProcessesByName(procName)
If processes.Length > 1 Then
Process.GetProcessesByName("keyinput")(0).Kill()
End If
End Sub
Public Sub type(ByVal int As Double, str As String)
For Each c As Char In str
SendKeys.Send(c)
System.Threading.Thread.Sleep(int * 1000)
Next
End Sub
Sub vai()
Dim line As String = ""
If File.Exists("trans.txt") Then
Using reader As New StreamReader("trans.txt")
Do While reader.Peek <> -1
line = reader.ReadLine()
type(0.155, line)
'SendKeys.Send(line)
SendKeys.Send("{ENTER}")
Loop
End Using
File.Delete("trans.txt")
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
vai()
End Sub
Basically the timer in it check if a file exists, read it and type the content simulating the keyboard.
I want this exe to start automatically when user login, it does it, apparently, I can see the form1 pop up but doesn't really works. Everyting is fine only if I run it manually by double-clicking the icon. Why and what can I do? Thanks
ps. i already tried to execute it with windows task manager, or putting a shortcut in the windows startup folder, or calling it from a cmd
EDIT:
when app starts automatically , process is running, but windows form is showing like this
Instead starting manually is showing like this:
I don't know this for a fact but I suspect that the issue is the fact that you are not specifying the location of the file. If you provide only the file name then it is assumed to be in the application's current directory. That current directory is often the folder that the EXE is in but it is not always and it can change. DO NOT rely on the current directory being any particular folder. ALWAYS specify the path of a file. If the file is in the program folder then specify that:
Dim filePath = Path.Combine(Application.StartupPath, "trans.txt")
If File.Exists(filePath) Then
Using reader As New StreamReader(filePath)
EDIT:
If you are running the application at startup by adding a shortcut to the user's Startup folder then, just like any other shortcut, you can set the working directory there. If you haven't set the then the current directory will not be the application folder and thus a file identified only by name will not be assumed to be in that folder.
If you are starting the app that way (which you should have told us in the question) then either set the working directory of the shortcut (which is years-old Windows functionality and nothing to do with VB.NET) or do as I already suggested and specify the full path when referring to the file in code. Better yet, do both. As I already said, DO NOT rely on the current directory being any particular folder, with this being a perfect example of why, but it still doesn't hurt to set the current directory anyway if you have the opportunity.
It was a Windows task scheduler fault, that for some reason didn't executed the exe correctly at logon. I've solved the issue by using Task Till Down and everything works fine now.
So i know in html you can use ../xx/ to open files in the same folder as the code. I want to do the same with Process.Start. This is my attempt at is but it cannot find the file.
Private Sub btn_database_Click(sender As Object, e As EventArgs) Handles btn_database.Click
Process.Start(".\WindowsApp10\WindowsApp10\WindowsApp10\WindowsApp10\bin\Debug\InventoryManager2.accdb")
End Sub
This is the path file I'm trying to open. C:\Users\Michal\Downloads\WindowsApp10\WindowsApp10\WindowsApp10\WindowsApp10\bin\Debug\InventoryManager2.accdb
Thanks in advance
Application.StartupPath will give you the path of the folder the current EXE was run from, so:
Process.Start(IO.Path.Combine(Application.StartupPath, "InventoryManager2.accdb"))
I've got a form that can be loaded from USB stick, from my home computer, or from work computers. The form executable is always stored in "some path\National Payroll\Build* and references a database in "some path\National Payroll".
The some path part changes, but the database is always in the parent directory. I've tried other advice to get the form's path, but I can only seem to get the path of the Local AppData folder where the form runs from when it is open, not the path of the form's executable. How do I get that?
Application.StartupPath
And, eventually Application.StartupPath.Parent
EDIT: TEST REPORT
Make a new project. Add a form with two TextBoxes named StartupPath and ExecutablePath. In the Form's Load event put the following code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
StartupPath.Text = Application.StartupPath
ExecutablePath.Text = Application.ExecutablePath
End Sub
Compile and deploy the executable on a USB flash drive (or wherever you want) and double click. This output will be produced:
I have a little desktop application using a *.db3 database. When I run the application on other machine than mine, the database cannot be found: an absolute path is used.
Currently, in my App.Config file, I have
connectionStrings>
add name="MyProject.My.MySettings.MyProjectSQLiteConnectionString"
connectionString="data source="N:\Long path do my DB\MyDB.db3""
providerName="System.Data.SQLite" />
/connectionStrings>
I would like to replace the data source with something like this (unfortunately not working)
data source =|DataDirectory|\MyDB.db3
How can I enter the application directory (relative path) in my connection string?
Starting with Steve B's comment and later finding this article, I solved my problem doing the following:
On the user machine, the database is copied into the %AppData% directory.
The |DataDirectory| is dynamically changed between Debug and Release mode.
This allows not to change the connection string.
In the meantime, I opted for a MS Access *.accdb database, but here is my code anyway.
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
#If (Not Debug) Then
pathMyAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp"
'"DataDirectory" is used in the Connection String
AppDomain.CurrentDomain.SetData("DataDirectory", pathMyAppData)
'The application closes if no database is found
If Not File.Exists(pathMyAppData & "\MyDB.accdb) Then
MsgBox("Database not found. Program closes.", MsgBoxStyle.Critical, "Error")
Me.Close()
End If
#End If