visual studio 2019 how do I know what References to import and path settings - vb.net

I created a Visual Studio 2019 project that uses FileSystem.FileExists and both StreamWriter and StreamReader
I also created a folder named Resource with the intention of creating a txt file in this folder
Knowing I need to tell the Writer and Reader where to find the file I used these lines of code
Dim path As String = "C:/Users/Me/source/repos/TestForms/TestForms/Resource/"
If Not My.Computer.FileSystem.FileExists(path & "Check.txt") Then
Because I do not full understand how to deal with a SQLite database yet lets say I put the database in the folder Resource. And if I make a EXE package that will run on another computer that string path is by my best guess is not going to work
In the process of leaning I keep seeing this line of code. I see no path to the database
m_dbConnection = New SQLiteConnection("Data Source=MyDatabase.sqlite; Version=3;")
Granted I am dealing with a txt file now but if it was a SQLite database file
My Question is how does the connection know where the database is ?
I also need to import this reference Imports System.IO
Coming from NetBeans I got spoiled with Auto Import
Second Question Does VS 2019 not have an Auto Import feature?
I am adding a screen shot of Solution Explore
Tried to add Resource folder to Resources that did not work real well
Stream Reader Code below without error
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
readDATA()
End Sub
Private Sub readDATA()
Dim line As String
Using reader As New StreamReader(path & "Check.txt", True)
line = reader.ReadToEnd.Trim
tbHaveOne.Text = line
End Using
End Sub
Code that creates Check.txt
Private Sub frmThree_Load(sender As Object, e As EventArgs) Handles MyBase.Load
haveFILE()
'tbHaveTwo.Text = frmOne.vR'KEEP see frmOne
'tbHaveOne.Select()
End Sub
Public Sub haveFILE()
'If My.Computer.FileSystem.FileExists(path & "Check.txt") Then
' MsgBox("File found.")
'Else
' MsgBox("File not found.")
'End If
If Not My.Computer.FileSystem.FileExists(path & "Check.txt") Then
' Create or overwrite the file.
Dim fs As FileStream = File.Create(path & "Check.txt")
fs.Close()
tbHaveTwo.Text = "File Created"
tbHaveOne.Select()
Else
tbHaveTwo.Text = "File Found"
tbHaveOne.Select()
End If
End Sub

You should pretty much never be hard-coding absolute paths. If you want to refer to a path under the program folder then you use Application.StartupPath as the root and a relative path, e.g.
Dim filePath = Path.Combine(Application.StartupPath, "Resource\Check.txt")
Then it doesn't matter where you run your program from. For other standard folder paths, you should use Environment.GetFolderPath or My.Computer.FileSystem.SpecialDirectories. For non-standard paths, you should let the user choose with a FolderBrowserDialog, OpenFileDialog or SaveFileDialog and then, if appropriate, save that path to a setting or the like.
When it comes to database connection strings, some ADO.NET providers support the use of "|DataDirectory|" in the path of a data file and that gets replaced at run time. What it gets replaced with depends on the type of app and how it was deployed. For Web Forms apps, it resolves to the App_Data folder. For ClickOnce Windows apps it resolves to a dedicated data folder. For other Windows apps, it resolves to the program folder, just like Application.StartupPath. I think the SQLite provider supports it but I'm not 100% sure. If it does, you could use something like this:
m_dbConnection = New SQLiteConnection("Data Source=|DataDirectory|\Resource\MyDatabase.sqlite; Version=3;")
EDIT:
If you add data files to your project in the Solution Explorer and you want those to be part of the deployed application then you need to configure them to make that happen. Select the file in the Solution Explorer and then set the Build Action to Content and the Copy to Output Directory property to Copy Always or, if you intend to make changes to the file when the app is running, Copy if Newer.
When you build, that file will then be copied from your project source folder to the output folder along with the EXE. You can then access it using Application.StartupPath at run time. That means while debugging as well as after deployment, because it will be copied to the "\bin\Release" output folder as well as the "\bin\Debug" output folder. If you add the file to a folder in the Solution Explorer, that file will be copied to, hence the reason I said earlier to use this:
Dim filePath = Path.Combine(Application.StartupPath, "Resource\Check.txt")

Here is the working code to Create a Text file if it does not exist and if it does exist the user is notified.
We also were able to Write & Read from the Text file
One of the disappointments is we were not able to use StreamReader
We did solve this error where the file name was created like this "Check.txtCheck.txt This line of code below created the File this way in the folder Bin > Debug
If Not My.Computer.FileSystem.FileExists(filePath & "Check.txt") Then See Code for correct format
One other BIG lesson Do NOT create a folder and place your Text file in that folder
I am not sure using the code "Using" was a good idea further research needed on that issue
Working Code Below
Private Sub frmThree_Load(sender As Object, e As EventArgs) Handles MyBase.Load
haveFILE()
End Sub
Public Sub haveFILE()
If Not System.IO.File.Exists(filePath) Then
System.IO.File.Create(filePath).Dispose()
tbHaveTwo.Text = "File Created"
tbHaveOne.Select()
Else
My.Computer.FileSystem.FileExists(filePath) ' Then
tbHaveTwo.Text = "File Found"
tbHaveOne.Select()
End If
'This line of code created the File this was in the Bin > Debug folder Check.txtCheck.txt
'If Not My.Computer.FileSystem.FileExists(filePath & "Check.txt") Then
End Sub
Sub PlaySystemSound()
My.Computer.Audio.PlaySystemSound(
System.Media.SystemSounds.Hand)
End Sub
Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
If tbHaveOne.Text = "" Then
PlaySystemSound()
'MsgBox("Please enter a username.", vbOKOnly, "Required Data")
'If MsgBoxResult.Ok Then
' Return
'End If
Const Title As String = "To EXIT Click OK"
Const Style = vbQuestion
Const Msg As String = "Enter Data" + vbCrLf + vbNewLine + "Then Write Data"
Dim result = MsgBox(Msg, Style, Title)
If result = vbOK Then
'MsgBox("Enter Data")
tbHaveOne.Select()
Return
End If
End If
writeDATA()
End Sub
Private Sub writeDATA()
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(filePath, True)
file.WriteLine(tbHaveOne.Text)
file.Close()
tbHaveOne.Clear()
tbHaveTwo.Text = "Data Written"
End Sub
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
readDATA()
End Sub
Public Sub readDATA()
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(filePath)
tbHaveOne.Text = fileReader
End Sub

Related

How to WriteAllText but restrict to overwrite the existing file?

Jan 8. 2023:
The AppendAllText can append into existing file and can create a file if no file exist.
the WriteAllText can Write into new created file and overwrite the existing file.
I'm trying to find another alltext for what I want to happen.
What I want to do is to save my textboxcontent.text into txt file.
I want to save 3 different content that will be displayed into my textboxcontent.text
And I only have one button.
That one button will open savefiledialog but with the code I have, I can only do 2 things, Write and Append.
Now, This is what suppose to happen.
*If I save the textcontent.text to an existing txt file, it will prompt message box "Do you want to overwrite this file?" And even if I click Yes, it will not allow to.
I must be able to create new txt file since I was not able to overwrite the file.
The reason is because I don't want to delete or overwrite the existing file with important information saved in it.
I hope somebody can help me.
This is the code I have.
```Imports System.io
Private lastSaveFileName As String = String.Empty
Private Function GetSaveFileName3(ByVal suggestedName As String) As String
Using sfd3 As New SaveFileDialog()
sfd3.Filter = "Text Files (*.txt) |*.txt"
sfd3.FileName = suggestedName
sfd3.OverwritePrompt = True
If DialogResult.OK Then
End If
If sfd3.ShowDialog() = DialogResult.OK Then
MessageBox.Show(
Me, "Your activity is not saved! This file have records from your last session, you cannot overwrite this file. Please create new file to save new records.",
"Save error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
)
Else
End If
Return String.Empty
End Using
End Function
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
lastSaveFileName = GetSaveFileName3(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.AppendAllText(lastSaveFileName, TextContent.Text)
End If
End Sub ' This code above includes IMPORTS.IO
Jan 9, 2023: Update
This is what I've done so far.
I tried to use the File.Exist but I don't know where to place it to make it run in the way I wanted.
Please see this code and help me fix it.
This code is running well in almost the way I want. I'm missing something.
Imports System.IO
Private lastSaveFileName As String = String.Empty
Private Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click
If Not File.Exists(lastSaveFileName) Then
lastSaveFileName = GetSaveFileName(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.WriteAllText(lastSaveFileName, txtdisplay1.Text)
End If
ElseIf File.Exists(lastSaveFileName) Then
lastSaveFileName = GetSaveFileName2(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.WriteAllText(lastSaveFileName, txtdisplay1.Text)
End If
End If
End Sub
Private Function GetSaveFileName2(ByVal suggestedName As String) As String
Using sfd As New SaveFileDialog()
sfd.Filter = "Text Files (*.txt) |*.txt"
sfd.FileName = suggestedName
sfd.OverwritePrompt = True
If sfd.ShowDialog() = DialogResult.OK Then
'If File.Exists(lastSaveFileName) Then
MessageBox.Show(
Me, "Your activity is not saved! This file have records from your last session, you cannot overwrite this file. Please create new file to save new records.",
"Save error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
)
End If
Return String.Empty
End Using
End Function
Private Function GetSaveFileName(ByVal suggestedName As String) As String
Using sfd As New SaveFileDialog()
sfd.Filter = "Text Files (*.txt) |*.txt"
sfd.FileName = suggestedName
sfd.OverwritePrompt = True
If sfd.ShowDialog() = DialogResult.OK Then
Return sfd.FileName
End If
Return String.Empty
End Using
End Function
With this code, I was able to save the textdisplay to a txtfile but it's like, it's bypassing the Elseif function.
Sometimes, poeple forgot to avoid important files and accidentally deleted it. This is what I'm preventing to happen.
I let the overwriteprompt true to let it ask the user if they want to replace. It accidentally click the yes, this will show message "This file have records from your last session, Please create new file to save new records." means that even the user want to replace it, the program will not allow it. I don't want to remove that scenario.
(Scenario 1)
What happen in this code is this, when I click the button, savefiledialog pop up and giving me choice how I want to save the textdisplay.
I can create new file or replace existing file.
First, I choose to replace, and a messagebox shows and saying, I can't replace the file.
Then I create new file, it lets me save the txt display normally.
(scenario 2)
That's what I want. The code runs that way at first, but if you click the button again, and try to create new file first, the message box will show saying I can't replace the file. then when I choose to replace, no message box shows and the file was replace. I lost the file.
That's where I need help. I only want the Scenario 1.
Please try on your own I you don't get what I mean.
I tried this and this code works the way I want.
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
If File.Exists(saveFileDialog1.FileName) Then
MessageBox.Show("A file with that name already exists. Please select a different file name or choose a different location to save the file.")
Else
File.WriteAllText(saveFileDialog1.FileName, Txtdisplay1.Text)
End If
End If
End Sub
Answered by: schoemr

Location to save custom settings text file?

I have a VB.Net program that has a few custom settings that are saved in a text file. Right now the file is saved in "settings.txt" which saves in the bin folder. The problem is that this program gets published to my network so my coworkers can use it, and every time I roll out a new update their settings they've saved get deleted; it overwrites the file with a blank version of it. Is there another location that would be better for me to save the file? or is there a way (maybe through code?) to prevent the contents for each of my coworkers from getting deleted every time I publish an update?
Public Class Program
'global variables
Dim fileName As String = "settings.txt"
Private Sub Program_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'code
Try
If File.Exists(fileName) = False Then
File.Create(fileName)
End If
' Open the file using a stream reader.
Using sr As New StreamReader(fileName)
Dim line As String
While (sr.EndOfStream = False)
line = sr.ReadLine()
End While
End Using
Catch ex As Exception
MsgBox(ex.ToString())
End Try
'rest of program
End Sub
End Class
If this is a ClickOnce app, try this:
Friend fileName As String = String.Empty
With My.Application.Info
fileName = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), .CompanyName, .ProductName, .Version.ToString, "Settings.txt")
End With

Delaying the use of code while a file is created?

Okay, so my program is checking to see is a textfile exist (this works) then if it doesn't exist it's creating the directory and file as well as saving the data to the file. I receive the error when it drops into the second test statement to create the text file.
The program will create the directory and file, but fail to write to it. "unhandeled exception: file is being used by another process. If I click to ignore the error and run the program, I can click the save button again and it works properly.
So I guess my question is, can I somehow delay the code that writes to the file long enough for the other operation to finish, or is there a better way to structure it, so it's not an issue.
Relevant Code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
'create necessary directory
'create text file
'if file exist write to file
If My.Computer.FileSystem.FileExists("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") = True Then
MsgBox("Data Saved")
Using sr As New IO.StreamWriter("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt")
For Each line In lstColors.Items
sr.WriteLine(line)
Next
sr.Close()
End Using
ElseIf My.Computer.FileSystem.FileExists("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt") = False Then
My.Computer.FileSystem.CreateDirectory("C:\Documents and Settings\All Users\Documents\NailPolishSelector")
Dim fs As FileStream = File.Create("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt")
'error occurs here
Using srr As New IO.StreamWriter("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt")
For Each line In lstColors.Items
srr.WriteLine(line)
Next
srr.Close()
End Using
MsgBox("File Created")
End If
End Sub
Your problem is that the file is locked by the fs FileStream object, and so the srr StreamWriter cannot write to it as it is trying to get a reference to the file by its name
So replacing
Using srr As New IO.StreamWriter("C:\Documents and Settings\All Users\Documents\NailPolishSelector\polishColors.txt")
with
Using srr As New IO.StreamWriter(fs)
should fix your problem.
Your code can be re-written somewhat to make it simpler and more robust. I'm guessing that if the file exists then you want to replace it, but if not then please remove the 'OPTIONAL piece of code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
' Get the All Users documents folder
Dim docsFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)
' Combine it with the desired directory and filename
Dim theFile As String = Path.Combine(docsFolder, "NailPolishSelector\polishColors.txt")
'OPTIONAL
' Remove the previous version of the file
If My.Computer.FileSystem.FileExists(theFile) Then
File.Delete(theFile)
End If
' REQUIRED: Create the directory if it doesn't exist
If Not Directory.Exists(Path.GetDirectoryName(theFile)) Then
Directory.CreateDirectory(Path.GetDirectoryName(theFile))
End If
' Create the data file
File.AppendAllLines(theFile, lstColors.items)
MsgBox("Data saved.")
End Sub
(Thanks go to Magnus for pointing out File.AppendAllLines(path, lstColors.Items).)
There are a couple of things there which you may which to look up in the documentation: the use of the SpecialFolders enumeration and Path.Combine.

Opening an Empty Directory from a Windows Form - VB.net

I'm trying to open a directory via my Windows Form created in VB.Net but every solution I've found doesn't seem to work.
Currently I'm using-
Dim path As String = Directory.GetCurrentDirectory()
Private Sub logDirBTN_Click(sender As Object, e As EventArgs) Handles logDirBTN.Click
Process.Start(path + "\Resources\Logs")
End Sub
Which returns "The system cannot find the file specified" exception. That's interesting because I know the folder is there. Furthermore this button's functionality works without any issue and from what I can tell the only difference is I'm opening a text file rather than an empty directory-
Private Sub stationListBTN_Click(sender As Object, e As EventArgs) Handles stationListBTN.Click
Process.Start("notepad.exe", path + "\Resources\StationList\StationList.txt")
End Sub
Here are all the other things I've tried-
Private Sub logDirBTN_Click(sender As Object, e As EventArgs) Handles logDirBTN.Click
'Process.Start("explorer.exe", path + "\Resources\Logs")
'Shell("explorer.exe", path + "\Resources\Logs", vbNormalFocus)
'Application.StartupPath & path + "\Resources\Logs"
'Shell(path + "\Resources\Logs", vbNormalFocus)
End Sub
Any help is greatly appreciated.
Dim MyProcess As New Process()
MyProcess.StartInfo.FileName = "explorer.exe"
MyProcess.StartInfo.Arguments = "C:\Blah"
MyProcess.Start()
MyProcess.WaitForExit()
MyProcess.Close()
MyProcess.Dispose()
Or just...
Process.Start("explorer.exe", "C:\FTP\")
Application.StartupPath is going to get you to your bin\Debug or bin\Release folder by the way, whatever folder the *.exe is in.
I'm guessing this is what you're looking for:
Process.Start("explorer.exe", Application.StartupPath & "\Resources\Logs")
Also, don't use + for joining strings. Use &
I assume you are trying to invoke Windows Explorer.
Add a trailing \ in the call to .Start
IO.Directory.CreateDirectory("C:\temp\temp")
Process.Start("c:\temp\temp\")
In the OP first example you were trying to open a file 'Logs'

import image to picture box from desktop user

I want to import a picture from folder which created by project when installed on user desktop but each user have different user name , how can i import from picture from dsektop user
Here is My code
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button2.Click
PictureBox1.Image = Image.FromFile("(My.Computer.FileSystem.SpecialDirectories.Desktop, "New folder") \" + ID.Text + ".png")
end sub
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
this will resolve to the desktop folder for the current user. Are you really creating folders on the desktop? Usually data and subfolders are stored in AppData.
EDIT
I SUSPECT you might have need of this folder in other places and even if not it can be saved and 'fixed' before hand. Elsewhere, like when the app starts:
Friend mUserFolder As String
mUserFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
' your code was not adding the required backslash
mUserFolder &= "\Data\" ' append the sub folder name
Now to load the file in button click the code is simpler to read and debug:
PictureBox1.Image = Image.FromFile(muserFolder & ID.Text & ".png")
Also use & for concatenating strings instead of +