I would like to know how to check if the file name that is inserted from textbox exist in the same folder as the program itself.
For example, I'm going to enter notepad.exe into textbox then when you click the button it will check if that program exist on the folder where the program is. If I enter notepad.ex into textbox then it didn't find any file with that name, then it will give error.
Simply append a ".\" at the start of the filename, this will check on the current directory path, which always will be the same directory on which your main assembly is:
Dim filename As String = TextBox1.Text
If Not File.Exists(String.Format(".\{0}", filename)) Then
MessageBox.Show("File not found.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' File found.
End If
I suggest to use Directory.GetCurrentDirectory() only when you are not sure what the working directory is, for example when changed the default working directory path with Directory.SetCurrentDirectory() method.
I had to use this code to check if the file exist or not.
Dim currentPath As String = System.IO.Path.Combine(IO.Directory.GetCurrentDirectory(), Textbox1.Text)
If IO.File.Exists(currentPath) Then
..Do something here
Else
MsgBox("Executable doesn't exist!", vbOKOnly, "Error!")
End If
Related
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.
I am trying to write code that opens the saveFileDialog only if the variable 'file' is not empty and generates an error message if 'file' is empty. The following code will pop up a error message box when 'file' is empty but will proceed to open the saveFileDialog anyway.
Public Shared Sub DownloadFile(cloudId As CloudIdentity, directoryPath As String, file As String)
Try
Dim cloudFilesProvider = New CloudFilesProvider(cloudId)
If file = "" Then
cloudFilesProvider.GetObjectSaveToFile("EstherTest", directoryPath, file)
End If
Catch
If file = "" Then
MessageBox.Show("Please select file to view")
End If
End Try
End Sub
Can you please direct me.
I think you have the wrong equality operator in your first if-statement.
In humand words, you are telling the compiler:
If file is equal to "" then do ... (If file = "" Then)
What you are trying to say is: if file is NOT equal to "" (If file <> "" Then).
As a second note: Unless an exception occurs, your second if statement will never be called, because the catch block will never be reached. You should put the second clause in a else or else if clause.
I've removed the try catch block for simplicity (and because I'm not really good at the vb.net syntax).
All in all your code should look like this:
Public Shared Sub DownloadFile(cloudId As CloudIdentity, directoryPath As String, file As String)
Dim cloudFilesProvider = New CloudFilesProvider(cloudId)
' if file is not emtpy, proceed
If file <> "" Then
cloudFilesProvider.GetObjectSaveToFile("EstherTest", directoryPath, file)
' else if file is empty, show message box
else if file = "" Then
MessageBox.Show("Please select file to view")
End If
End Sub
in my VB.NET program i have it create a text file that contains a username and password. the Username being on the first line, the password on the second. this works great.
I do this because i want it to pre-load the account into the text boxes once the user re-opens the program. so at the beginning, i want it to test if the file exist. the file exist in the directory Documents under 'account.txt'. this is the code that generates the text file:
Try
Dim filePath As String
filePath = System.IO.Path.Combine(
My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Account.txt")
My.Computer.FileSystem.WriteAllText(filePath, TextBox1.Text + Environment.NewLine + TextBox2.Text, False)
Catch fileException As Exception
Throw fileException
End Try
MsgBox("Saved account to 'Documents\Account.text' and will auto-fill Username and password box when program is re-opened.")
and this is the code i tried to use to see if it existed or not:
If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.MyDocuments + "Account.txt") Then
MsgBox("File found.")
Else
MsgBox("File not found.")
End If
I also tried this:
If My.Computer.FileSystem.SpecialDirectories.MyDocuments.FileExists("c://Check.txt") Then
MsgBox("File found.")
Else
MsgBox("File not found.")
End If
The first one cant find it because I know in doing it wrong but the second doesn't work because the syntax it wrong as well. Any help is appreciated greatly.
try
Dim filePath As String
filePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Account.txt")
If File.Exists(filePath) Then
MsgBox("File found.")
Else
MsgBox("File not found.")
End If
I'm trying to save data to an external data file on visual basic using visualstudio 2013, so far I've found various methods to do this but they all get stuck on the same problem
"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in >mscorlib.dll
Additional information: Access to the path 'C:\test.txt' is denied."
The current method I'm using is the following code
Dim filename As String
filename = InputBox("Please Enter a Filename", "Enter Filename", "")
If filename = "" Then
MsgBox("Please Enter a value to save", vbOKOnly, "Error")
Else
filename = "C:\" + filename + ".txt"
File.WriteAllText(filename, "Accesories list Items")
End If
The Windows C Drive is inaccessible by any external program (pretty sure MS programs can access it). Try using "C:\temp\test.txt" If that fails then there is a problem. Otherwise it's probably because you're not allowed to access that area of Windows.
We have to learn VB.NET for the semester, my experience lies mainly with C# - not that this should make a difference to this particular problem.
I've used just about the most simple way to save a file using the .NET framework, but Windows 7 won't let me save the file anywhere (or anywhere that I have found yet). Here is the code I am using to save a text file.
Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
... Build up output string ...
Try
' Try to write the file.
My.Computer.FileSystem.WriteAllText(saveLocation, output, False)
Catch PermissionEx As UnauthorizedAccessException
' We do not have permissions to save in this folder.
MessageBox.Show("Do not have permissions to save file to the folder specified. Please try saving somewhere different.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch Ex As Exception
' Catch any exceptions that occured when trying to write the file.
MessageBox.Show("Writing the file was not successful.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
The problem is that this using this code throws an UnauthorizedAccessException no matter where I try to save the file. I've tried running the .exe file as administrator, and the IDE as administrator.
Is this just Windows 7 being overprotective? And if so, what can I do to solve this problem? The requirements state that I be able to save a file!
Thanks.
This code:
Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
Is giving you the location of a folder. Then you're trying to save a file with the same name as the folder. Instead, I assume you want to save a file inside that folder:
Dim saveLocation As String = dialog.SelectedPath
saveLocation = Path.Combine(saveLocation, "SomeFile.txt")
That will create a file called "SomeFile.txt" inside the selected folder.
Alternatively, instead of using FolderBrowserDialog to choose a folder, use SaveFileDialog to select the actual file instead.