VB Console application to rename a file - vb.net

I am trying to write a Console Application in VB that will allow me to change the name of a file.
The code I have so far is:
Public Class Form1
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
If txtpath.Text.Length <> 0 And txtName.Text.Length <> 0 Then
' Change "c:\test.txt" to the path and filename for the file that
' you want to rename.
' txtpath contains the full path for the file
' txtName contains the new name
My.Computer.FileSystem.RenameFile(txtpath.ToString, txtName.ToString)
Else
MessageBox.Show("Please Fill all Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtpath.Clear()
txtName.Clear()
End Sub
End Class
But when I try to run it i get an error in this line:
My.Computer.FileSystem.RenameFile(txtpath.ToString, txtName.ToString)
Any suggestions?

Changing :
My.Computer.FileSystem.RenameFile(txtpath.ToString, txtName.ToString)
To:
My.Computer.FileSystem.RenameFile(txtpath.Text.ToString, txtName.Text.ToString)
Solves the Problem.

The problem is that you are performing .ToString on the textbox object, and not the value of the textbox. I always check to make sure that the source and destination files exist or not. Also, make sure that you are passing the full path to the files to that function to ensure it performs properly.
Try something like this:
If Not System.IO.File.Exists(txtpath.Text) Then
MsgBox("File not found!")
ElseIf System.IO.File.Exists(txtName.Text) Then
MsgBox("Target path already exists!")
Else
My.Computer.FileSystem.RenameFile(txtpath.Text, txtName.Text)
End If

Related

Expression is not a method

Hello Coders Im trying to code a Anti virus and the following problem was here!
Im trying to add MsgBoxStyle.YesNo and will the problem in this...
So im trying to add a Question for delete file or no with MsgBox.
My code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If My.Settings.ifTmpR = True Then
For Each aF In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp)
Try
MsgBoxStyle.YesNo()
MsgBox("Biztosan törli: " + aF)
IO.File.Delete(aF)
MsgBox("Temp kiürítése: " + aF)
MsgBox(aF + "kitötrölve")
Catch ex As Exception
End Try
Next
End If
If My.Settings.ifTmpR = False Then
Try
MsgBox("Kérlek kapcsold be a TEMP Queryt!")
Catch ex As Exception
End Try
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
My.Settings.ifTmpR = True
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = False Then
My.Settings.ifTmpR = False
End If
End Sub
End Class
And what the problem?
Help?
So you're coding an Anti-Virus application but you seem to have no basic knowledge about how to use methods and parameters.
You are supposed to include MsgBoxStyle.YesNo as one of the parameters to the MsgBox() method. You cannot call it like you do since it's simply an Integer value.
As Steve suggested you should use MessageBox.Show() instead, due to that MsgBox only exists for backwards compability.
The first parameter is the message you want it to show, and the second parameter is what title the window should have. They're just plain strings.
The third parameter however is the one you pass to tell the method what buttons to include in the MessageBox, and it should be passed like this:
MessageBox.Show("I am a message", "I am a title", MessageBoxButtons.YesNo)
This will show a message box with the buttons "Yes" and "No" inside it.
Now to get use out of this you should put your MessageBox in an If-statement, otherwise the application won't care if you press either Yes or No.
If MessageBox.Show("Are you sure you want to delete this file?", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Delete code here.
End If
There. Now I suggest that you would have started with something like a "Hello World"-application instead of an Anti-Virus application, as Anti-Virus is pretty much harder than just showing a simple MessageBox.

creating then writing to a text file

I have button to create a text file and a textbox to write stuff to the text file when I press enter.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
path = "C:\Testing.txt"
File.Create(path)
End Sub
If e.KeyCode = Keys.Enter Then
System.IO.File.AppendAllText(path, TextBox1.Text & vbCrLf)
End If
The file is created properly but when I want to write to it using the code above, I get an error.
The process cannot access the file 'C:\Testing.txt' because it is being used by another process.
Change this line:
File.Create(path)
To this:
File.Create(path).Dispose()
However as stated before, you can cut all this out and simply use:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
path = "C:\Testing.txt"
If e.KeyCode = Keys.Enter Then
System.IO.File.AppendAllText(path, TextBox1.Text & vbCrLf)
End If
End Sub
As per MSDN: AppendAllText will "Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file."

Read/Compile vb.net code dynamically from text file

I am trying to read and compile code from a text file. I have done some of its portion but feeling difficulties when trying to add controls to the Form. Kindly guide me. I am attaching code.
Public Class Form1
Sub Execute()
' Creates object of the compiler
Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler
'References/Parameters.
Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
objCompilerParameters.ReferencedAssemblies.Add("System.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
'Compiles in memory.
objCompilerParameters.GenerateInMemory = True
'Runs the source code.
'You can use resources, textbox's or even the settings, up to you! :D
'Dim strCode As String = TextBox1.Text
'Compiler Results
'Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)
Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromFile(objCompilerParameters, "H:\VB project\LE21 - CodeDom - Run code from Textbox\LE21 - CodeDom - Run code from Textbox\LE21 - CodeDom - Run code from Textbox\file.txt")
'If an Error occurs
If objCompileResults.Errors.HasErrors Then
MsgBox("Error: Line>" & objCompileResults.Errors(0).Line.ToString & ", " & objCompileResults.Errors(0).ErrorText)
Exit Sub
End If
'Creates assembly
Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly
Dim objTheClass As Object = objAssembly.CreateInstance("MainClass")
If objTheClass Is Nothing Then
MsgBox("Can't load class...")
Exit Sub
End If
'Trys to excute
Try
objTheClass.GetType.InvokeMember("ExecuteCode",
System.Reflection.BindingFlags.InvokeMethod, Nothing, objTheClass, Nothing)
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Runs the source code from textbox1.
Execute()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim p As New Form2
p.Text = "hahahahah"
p.Show()
End Sub
End Class
"Type Form2 is not defined"
That's because you haven't defined it. You may be dynamically loading and compiling some file which contains code which does define it. But the compiler has no way of knowing that at compile time. Being a statically typed system, the compiler needs to know about all of the types you're using when it compiles the code.
If you're dynamically loading class definitions from a file, then you also need to dynamically invoke those class definitions. That's going to involve a lot of reflection and CodeDOM and whatnot. It's not going to be pretty, and it's definitely not going to have compile-time type checking. (And it's going to be very "stringly typed" in the sense that instead of creating an instance of Form2 you're going to be requesting from reflection to create an instance of "Form2" and hoping for the best.) So you're going to want to put in a lot of error handling for things like, for example, classes which don't exist.

how to drag a file and get path of it

How to implement a Panel in Winform , when a user drags a file on it (a simple .txt file) , it should accept it and stores its path into some variable called filepathname etc. which can be used earlier. I could find examples on how to implement drag and drop but not on how to get the path and store it for use later in the program.
Using : Visual Studio 2008 - Vb.net
Thanks!
I found this in this MSDN page
The code below is the modificated ones
Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim MyFiles() As String
Dim i As Integer
' Assign the files to an array.
MyFiles = e.Data.GetData(DataFormats.FileDrop)
'If there are more than one file, set first only
'If you want another restrictment, please edit this.
filepathname = MyFiles(0)
End If
End Sub

Click Button Event in VB that displays a message about user input

I have a really quick and simple question, I’m learning programming in C# and VB and I need to create a GUI application in Windows Form with Visual Studio. This GUI will prompt the user to enter in an integer. I believe I have this part alright but then I need to have the user click a button that will convert the user's entry to an integer and display a message indicating whether the user was successful. I think I even have the conversion done correctly but I am having a problem displaying that message if the user was successful. Basically I need to know how to function the click method in VB that will allow this message to appear. Any help with this would be greatly appreciated. The following code is what I have already written for this project:
Public Class Form1
Private Sub EvaluateInput()
Dim InputValue As String
InputValue = ValueTextBox.Text
If IsNumeric(InputValue) Then
MessageBox.Show(InputValue & " is a number.")
Else
MessageBox.Show(InputValue & " is not a number.")
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 'Continue Button
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Exit Button
Dim button As DialogResult
button = MessageBox.Show _
("Are you sure you want to exit this application?", _
"Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
If button = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
'Do Nothing
End If
End Sub
End Class
If I understand your question correctly, then you would need a simply change:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) _
Handles Button2.Click 'Continue Button
EvaluateInput()
End Sub
When you press Button2, it will call the EvaluateInput sub and display a message accordingly.