How to WriteAllText but restrict to overwrite the existing file? - vb.net

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

Related

What to do to reject a replace txt file request, and offer them to create new txt file instead?

Using this codes, I don't know what's wrong, I still can't solve my problem
Please check and correct me.
What's wrong?
What I want is when they click the savefile button, savefiledialog will show to save the data to a file. If they try to replace an existing file, Msgbox will show to reject their replace request. So they have to create new file.
With this code, this is what's happening.
1st click, you can create or replace without getting reject messagebox.
2nd click, you will receive rejection either you create or replace.
3rd click is like the 1st click
4th click is like the 2nd click
And so on.
Private lastSaveFileName As String = String.Empty
Private Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click
lastSaveFileName = GetSaveFileName2(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.AppendAllText(lastSaveFileName, txtdisplay1.Text)
End If
End Sub
Private Function GetSaveFileName2(ByVal suggestedName As String) As String
Using sfd3 As New SaveFileDialog()
sfd3.Filter = "Text Files (*.txt) |*.txt"
sfd3.FileName = suggestedName
sfd3.OverwritePrompt = False
If sfd3.ShowDialog() = DialogResult.OK Then
If Not 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
)
Else
Return sfd3.FileName
End If
Else
End If
Return String.Empty
End Using
End Function
This is related to my first post,
How to WriteAllText but restrict to overwrite the existing file?
Like this,
Private Function GetSaveFileName2(suggestedName As String) As String
Dim rv As String = String.Empty 'String.Empty is do not save
Dim sfd3 As New SaveFileDialog()
sfd3.Filter = "Text Files (*.txt) |*.txt"
sfd3.FileName = suggestedName
sfd3.OverwritePrompt = False
Dim dr As DialogResult
Do
dr = sfd3.ShowDialog
If dr = DialogResult.OK Then
If IO.File.Exists(sfd3.FileName) Then
MessageBox.Show("Not saved! .... Please create new file to save new records or Cancel to Exit.",
"Save error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
rv = sfd3.FileName
End If
End If
Loop While dr = Windows.Forms.DialogResult.OK AndAlso rv = String.Empty
Return rv
End Function

visual studio 2019 how do I know what References to import and path settings

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

Saving listbox's file not just its name

Im working on my first injector in VB.NET.
Im trying to save the loaded dll in listbox, but it only saves the name.
I select the dll, inject, it saves my.settings, but once I reopen the injector it only saves the dll's name, not its path, so I have to browse and select it again
I was thinking about maybe I have to save openfiledialog or something but really got no clue
Inject button:
My.Settings.dll = New Specialized.StringCollection
My.Settings.dll.AddRange(dll.Items.Cast(Of String).ToArray)
My.Settings.Save()
My.Settings.process = SteamTextBox2.Text
My.Settings.Save()
On form load:
If My.Settings.dll IsNot Nothing Then dll.Items.AddRange(My.Settings.dll.Cast(Of String).ToArray)
The problem with this the injector only needs the dll's name without path
Dim ExeName As String = IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)
Private Sub Inject()
pszLibFileRemote = OpenFileDialog1.FileName
End Sub
OpenFileDialog1.Filter = "DLL (*.dll) |*.dll"
OpenFileDialog1.ShowDialog()
OpenFileDialog1.ToString()
If IO.File.Exists(OpenFileDialog1.FileName) Then
Dim TargetProcess As Process() = Diagnostics.Process.GetProcessesByName(SteamTextBox2.Text)
If TargetProcess.Length = 0 Then
...
Else
Call Inject()
I want it to load the actual selected file not just it's name
Nothing is functionally wrong with your code. I guess the problem is with how you load the paths into the listbox in the first place. Here is some code to do that.
Add a new button called LoadButton, then this code for the handler
Private Sub LoadButton_Click(sender As Object, e As EventArgs) Handles LoadButton.Click
Dim filenames As IEnumerable(Of String)
Using dialog As New OpenFileDialog
dialog.Filter = "Application extensions (*.dll)|*.dll|All files (*.*)|*.*"
dialog.Multiselect = True
Select Case dialog.ShowDialog()
Case DialogResult.OK
filenames = dialog.FileNames
Case Else
filenames = Nothing
End Select
End Using
filenames = filenames.Select(Function(fn) System.IO.Path.GetFileName(fn))
If filenames?.Any() Then dll.Items.AddRange(filenames.ToArray())
End Sub
This will put the full path into the listbox. Does this solve the problem?

Can open and read from file but not then resave to same file

I am using vb.NET and windows forms.
I have a simple form with a list box and two buttons.
btnLoadData opens up an OpenFileDialog and allows me to choose the text file to open, this is then read into the list box.
I can then delete items from the list box.
btnSaveList opens up a SaveFileDialog and allows me to choose a file to save to.
The problem occurs when I try to save to the same file that I read in.
It tells me that the file cannot be accessed as it is in use. It works if I choose a new file name.
I have searched and tried a number of different suggestions. I have altered the code a number of times and have finally decided I need to ask for help!
The code for the two buttons is below.
Private Sub btnLoadData_Click(sender As Object, e As EventArgs) Handles btnLoadData.Click
Dim openFD As New OpenFileDialog()
openFD.Filter = "Text [*.txt*]|*.txt|CSV [*.csv]|*.csv|All Files [*.*]|*.*"
openFD.ShowDialog()
openFD.OpenFile()
Dim objReader As New StreamReader(openFD.SafeFileName)
While objReader.Peek <> -1
lstList.Items.Add(objReader.ReadLine)
End While
objReader.Close()
End Sub
Private Sub btnSaveList_Click(sender As Object, e As EventArgs) Handles btnSaveList.Click
Dim saveFD As New SaveFileDialog()
If saveFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Using objWriter As New StreamWriter(saveFD.FileName) 'Throws the exception here
For Each line In lstList.Items
objWriter.WriteLine(line)
Next
End Using
End If
End Sub
Private Sub lstList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstList.SelectedIndexChanged
lstList.Items.Remove(lstList.SelectedItem)
End Sub
Thank you.
You create two streams but only closing one at the end of reading the file. The OpenFile() method of the OpenFileDialog is creating a stream you doesn't close at the end so it stays open and locks the file. In your case you are using your own stream so you don't need the method OpenFile().
code for button #1 (read the file):
openFD.Filter = "Text [*.txt*]|*.txt|CSV [*.csv]|*.csv|All Files [*.*]|*.*"
openFD.ShowDialog()
'openFD.OpenFile()
Using objReader As New StreamReader(openFD.FileName)
While objReader.Peek <> -1
lstList.Items.Add(objReader.ReadLine)
End While
End Using
code for button #2 (write the file):
Dim saveFD As New SaveFileDialog()
If saveFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Using objWriter As New StreamWriter(saveFD.FileName)
For Each line In lstList.Items
objWriter.WriteLine(line)
Next
End Using
End If
Opening a file for read will lock the file against writes and deletes; opening a file for write will lock against reads, writes and deletes.
You can override those locks but trying to both read and write a file at the same time creates its own set of problems.
There are two approaches to avoid this:
Read the whole file in and close before processing and writing. Of course the whole content has to be in memory.
Write to a temporary file, after closing the input and finishing writing delete the original file and rename the temporary file. This will not preserve attributes (eg. ownership, ACL) without extra steps.
However in your case I suspect you need to use a using block to ensure the file is closed after the read rather than depending on the GC to close it at some point in the future.

VB.NET 2010 Passing Picture from OpenFile Dialog Box to another form

I am creating a loan program that opens a Custom Form Dialog Box, you choose the picture, click open and then need to pass it to another form to use after hitting OK from the Dialog Box. This is my code when I click the Logo File button from my custom Dialog Form.
The Form is called Dialog Form and I need to send the picture to the NewLoanCaculatorForm to populate a picture area in the form.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogoFile.Click
Dim mystream As Stream = Nothing
'Open the File to pickup icon for Loan Calculator
Dim OpenFileDialog1 As New OpenFileDialog
'Set up and display the open File Dialog
With OpenFileDialog1
'Begin in the current Directory
.InitialDirectory = "C:\"
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
End With
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
mystream = OpenFileDialog1.OpenFile()
If (mystream IsNot Nothing) Then
' I believe the coded goes here but I'm stuck
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (mystream IsNot Nothing) Then
mystream.Close()
End If
End Try
End If
End Sub
This is how I usually do things like this:
Create global variable in the DialogForm:
Public Property sPath as String
or
Public Property imgLogo as Image
To get the Image do this:
imgLogo = Image.FromFile(OpenFileDialog1.FileName)
Or simply do:
sPath = OpenFileDialog1.FileName
instead of
mystream = OpenFileDialog1.OpenFile()
Then when you have done this you close the form by clicking the OK button or whatever you call it.
Then in your main form NewLoanCaculatorForm where you call DialogForm you simply do:
img = DialogForm.imgLogo
or
path = DialogForm.sPath
img = Image.FromFile(path)
Depending on which way you stored the info in the DialogForm.
Also, if you are looking for images I would recommend you not to have .txt in your filter. That would seriously ruin the execution.