Save File Dialog says file is being used by another process Visual Basic - vb.net

I'm making a program that opens, edits, and saves text files. So far, the opening and editing parts are working (but there's a bug with the editing part, I'll get to that later, worst case is I'll have to change the form some but I'm sure I can fix it). My main problem is the save part. Honestly, I don't know what I'm doing when it comes to Open/Save File Dialog, so I really have no idea where to begin fixing it.
I go to save a file, and I save it as a .txt file with any name (new file or overwrite it doesn't matter) and then it gives me an exception that says "The file is being used by another process" even if it's a brand new file. Here's the source:
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.DefaultExt = ".txt"
saveFileDialog1.FileName = "New File.txt"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
My.Computer.FileSystem.WriteAllText(saveFileDialog1.FileName.ToString, txtOutput.Text, True)
End If
End If

Related

How do I get a user to input a document and then save that document?

So I'm a bit stuck with my project, I am using Visual Studio and coding in Visual Basic, I am also using Microsoft Access with SQL if that helps at all.
What I need is to allow the user to select a document from an OpenFileDialog and to then save that document to the actual program so it is there when the program is next ran.
The following code is triggered on a button press, what I have so far is...
saveDocumentDialog.Filter = "Document Files|*.docx;*.doc;*.dot;*.txt;*.rtf;*.pdf;*.ppt;*.pptx;*.xls;*.xlsx"
saveDocumentDialog.FileName = "Untitled"
saveDocumentDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
saveDocumentDialog.ShowDialog()
If saveDocumentDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
fullFilename = saveDocumentDialog.FileName
End If
Using openDocumentDialog As New SaveFileDialog
Dim filename As String = IO.Path.GetFileName(fullFilename)
openDocumentDialog.FileName = "Untitled"
openDocumentDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
openDocumentDialog.Title = "Select Save Location"
openDocumentDialog.Filter = "All Files (*.*)|*.*"
If openDocumentDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
My.Computer.FileSystem.CopyFile(fullFilename, openDocumentDialog.FileName)
Catch ex As Exception
MessageBox.Show("Could not copy the file." & Environment.NewLine & ex.Message, "Error copying file.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
One way to do this is to designate a specific folder to the program(e.g. Public Documents), and save the files there and reload them from the folder when the program restarts. You could either hard code the path, or make a setting in the program for the user to specify a folder they would prefer.

Unhandled exception has occured in the application vb.net

Ok, so my code look like the following.
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("E:/Med/Dra.txt", False)
file.WriteLine(NameBasic)
file.WriteLine(LastBasic)
file.WriteLine(PhoneBasic)
file.Close();
All those are variables that I have set for text boxes. This is OnbuttonClick(...
Now for my onload I take the info out of the notepad, Here is the code,
Dim read As System.IO.StreamReader
read = My.Computer.FileSystem.OpenTextFileReader("E:/Med/Dra.txt")
lblNameBasic.Text = read.ReadLine
lblLastBasic.Text = read.ReadLine
lblPhoneBasic.Text = read.ReadLine
read.Close();
I have placed the notepad(txt file) inside a flashdrive folder named med
I got the saving info to work and load, so I took the flashdrive to another computer, I got this nasty error, talking about the System.IO and all this other stuff.
It then prompts me, would you like to continue with errors, or quit.
I click continue than all the saved data does not load. Am I doing something wrong here??
Also sorry for alot of questions today. (The .exe is in the flashdrive, med folder aswell).
First of all, your path is incorrect - E:/Med/Dra.txt should be E:\Med\Dra.txt. And here how you use open file dialog - this is just basis, you need to take care of error handling, etc.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim read As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(openFileDialog1.FileName)
End If
I think, main reason why you had errors is because incorrect path. You can also check if path exists
If Not File.Exists("E:\Med\Dra.txt") Then
MessageBox.Show("There is no such file")
Exit Sub
End If
' Code to open non existing file will be skipped

Exporting pdf file using Crystal report (Allow user to choose file path)?

I know how to export to pdf using Crystal Report, but I don't want to preset file path, I want users can choose file path (and file name) when clicking Export button.
Do you know how to do that ?
Thank you very much,
Tai
Prompting the user for a save location would be something you'd implement as part of your app - this related question provides some answers on how to do that. Then, you'd just supply the path from the dialog to the Crystal Reports export API so it saves to that location.
Here is a partial code in my project. I hope it helps.
Dim saveFileDialog1 As New SaveFileDialog()
'saveFileDialog1.InitialDirectory = My.Computer.FileSystem.CurrentDirectory
saveFileDialog1.Filter = "PDF files (*.PDF)|*.PDF"
saveFileDialog1.FilterIndex = 1
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
'Here is where you write the file
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.
End Try
End If

Editing Batch through VB.net

I am currently building a transcompiler for batch and I want an option where you can edit existing batch files through a Rich Textbox in VB.Net How does one do this? It is known knowledge that you can edit batch files through notepad without even touching the file type.
Use a StreamReader to load the text into the RichTextBox. For example:
Dim StreamReader1 As New IO.StreamReader
RichTextBox1.Text = StreamReader1.ReadToEnd()
You may also want to use an OpenFileDialog to load the file. Add a OpenFileDialog to your Form first.
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Open a Batch File"
OpenFileDialog1.Filter = "Batch files (*.bat) | *.bat"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Dim StreamReader1 As New IO.StreamReader(OpenFileDialog1.FileName)
RichTextBox1.Text = StreamReader1.ReadToEnd
StreamReader1.Close()
Else
'What to do if OpenFileDialog is cancelled
End If

Opening a notepad from a button in VB.net

I want to create a button in VB.net that lets me browse my hard drive for the specified notepad file i want to open and retrieve the contents from it, i only have tried using FileStream and StreamReader but this wont let me manually select the notepad file instead i have to declare a default filename. Any sample codes would be appreciated thanks in advance, i just need a starting point. I am really stuck to this.
This the code i am using right now, but i have to specify the correct file name on it:
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
ReDim Preserve sArray(Index)
sArray(Index) = sReader.ReadLine
Index += 1
Loop
If I understand your question correctly, you want to have an option to choose which textfile to open, if so you can try this:
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
stream = openFileDialog1.OpenFile()
If (stream IsNot Nothing) Then
//do your loop here
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Finally
If (stream IsNot Nothing) Then
stream.Close()
End If
End Try
End If
I think you may be using the wrong approach with a FileStream. Instead look to allow a user to select a file, then use Process.Start to open Notepad.
Take a look here for examples on selecting a file. The page here then details Process.Start.
I'm happy to provide more code samples directly here, but those two pages should be sufficient.