I am making a Program where you add files to an ListBox, and when you click the Install-Button it should move the files that got added to the ListBox, to an other folder. But this doesn't work, and i don't know how to fix. Here is my current Code:
Private Sub MoveFileBackgroundWorker(sender As Object, e As DoWorkEventArgs) Handles mfb.DoWork
For Each i As String In ListBox1.Items 'For each Items in ListBox1.Items, move.
IO.File.Move(i, mcpath_ & "\mods\")
Next
End Sub
Any help is really appreciated! PS: Sorry if my English isn't good, i am German :)
Move works in the following way:
IO.File.Move("C:\\myfile1.txt", "D:\\myfile2.txt")
It takes the source file as the first argument, and the target file as the second argument. In other words, the line above will move the file C:\myfile1.txt to a file named D:\myfile2.txt
So your line should be
IO.File.Move(i, IO.Path.Combine(mcpath_, "mods", IO.Path.GetFileName(i))
Which means: move file stored in i to the folder mcpath & "\mods\" with the same file name and extension
That's how i fixed it:
IO.File.Move(i, IO.Path.Combine(mcpath_.Trim, "mods", IO.Path.GetFileName(i)))
You need to add at mcpath_ .Trim!
Related
I have the following code to try and get all file names in my parent directory and all its sub directories.
The code works, but not how I would like. Namely it will process all files in the parent directory and all in the "first- level" of sub directories but I want to be able to go into all levels of sub directories.
How do I do that?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'ListBox1.Items.AddRange(IO.Directory.GetFiles("C:\"))
For Each Dir As String In IO.Directory.GetDirectories("C:\Program Files")
' ListBox1.Items.Add(Dir)
ListBox1.Items.AddRange(IO.Directory.GetFiles(Dir))
Next
End Sub
Here is the code that does what you want in just two lines:
Dim result As List(Of String) = System.IO.Directory.GetFiles("C:\Program Files", "*", System.IO.SearchOption.AllDirectories)
listBox1.DataSource = result
[ Credit do #Carsten in this post, which listed subdirectories and I changed to listing files and binded it to the ListBox element. I didn't know that the recursive solution was already implemented in System.IO ]
Edit1: taking comment suggestion.
Edit2: GetFiles does not allow a workaround for this problem: when you attempt to read could be configured so that the current user may not access them. More details (and solution with a recursive function) here.
Okay, so I'm making a program where you have two radio boxes to choose from, for the time being, lets call them rb1 and rb2. And so, Which ever one you choose, you press the launch button (BtnLaunch) and the program will launch depending on which radiobox you chose.
The problem I am having is that it only works on my computer as my file path for these programs are in my Z:\ Hard Drive where as most people have C:\ or E:. So I'd like it if the user could choose the file path for rb1 and rb2 and so the next time they open the program, it saves so that they don't need to write the path for the file location again.
So if you dont understand here is my code for radiobuttons:
Private Sub BtnLaunch_Click(sender As Object, e As EventArgs) Handles BtnLaunch.Click
If BtnServer1.Checked = True Then Process.Start("Z:\Path\Program1")
If BtnServer2.Checked = True Then Process.Start("Z:\Path\Program2")
End Sub
So I would like to replace the path with the users choice of path as some people will have ("C:\OtherFolderName\Program") and other will have different. I really hope you understand. Please be broad in answers as I'm new to VB.
Thank you.
You can either save an ini file that your program could write to when the user changes the selection, the reads when ever it's launched. Or you could write the path to the registry and read it back on launch.
Same thing, two different ways.
Good evening to all of you!
I have a problem that I can't solve with the method GetFullPath in vb.net.
What I want to do :
I would like to get the full path of a text file (test.txt) which is located in the same folder as the app.exe, the one I am working on. I need the full path to use an other method. To know more about the reason it doesn't work, I put the GetFullPath output in a MsgBox.
What is happening :
The MsgBox just shows a blank. This is really strange because, even if the test.txt doesn't exist, the output should exist (as if the file exists).
WARNING : IN MY CASE THE FILE EXISTS
Documentation : https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.100).aspx Cf. "Remarks"
What I think about that :
Is it possible that the app.exe doesn't see test.txt which is in the same folder ? I don't think so, it would give an outpout.
Maybe it is a problem of permission ? I really don't know why the MsgBox is empty.
My peace of code :
Dim file1 As String = "test.txt"
MsgBox(GetFullPath(file1))
Thankyou to everyone who will try to help me.
Have a nice day ! :)
Please excuse my poor english.
Microsoft Visual Studio 2010.
It appears something in your code is changing the working directory (or in the case of Windows XP, it is not being set properly). The GetFullPath function I have provided below will return the path I believe you are expecting regardless of the working directory.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(GetFullPath("file.txt"))
End Sub
Private Function GetFullPath(fileName As String) As String
Return IO.Path.Combine(Application.StartupPath, fileName)
End Function
Or you can do this ,
Label1.Text = Application.StartupPath
You do all with this code ^^
I am trying to use it like this:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Computer.Network.DownloadFile(
"http://magicalexample.com/multi.zip", "My.Computer.FileSystem.CurrentDirectory\multi.zip")
Process.Start("cmd", "/k jar xf multi.zip")
MsgBox("Done.")
End Sub
End Class
(note: I screwed up the formatting, it's right in VB, don't worry lol)
When I try to use the update button, it tells me that destinationFileName needs to include a file name. Do they not play well together or what?
My.Computer.FileSystem.CourrentDirectory is a variable and isn't going to be magically parsed into your string. You need to concatenate the variable with the file location:
My.Computer.Network.DownloadFile("http://magicalexample.com/multi.zip", My.Computer.FileSystem.CurrentDirectory & "\multi.zip")
Or use the Path.Combine() method to merge the directory plus filename:
My.Computer.Network.DownloadFile("http://magicalexample.com/multi.zip", Path.Combine(My.Computer.FileSystem.CurrentDirectory, "multi.zip"))
Note that your examples aren't working for various reasons. They need the http:// in front of them AND the file must exist at the source (cannot be a 404 Not Found Error). Also, the extra semi colon in the comments you posted seems to indicate that it wouldn't even be compiling so you might be running a prior version. I created a new project and as an example ran this code which worked to download the google logo from the main page.
My.Computer.Network.DownloadFile("https://www.google.com/images/srpr/logo11w.png", Path.Combine(My.Computer.FileSystem.CurrentDirectory, "logo.png"))
After looking through many sites and some tutorial videos, I'm still stumped on this one. I'm finishing up a program and I need to add one last bit of functionality.
The program works this way. The user specifies a file in textbox1 and then specifies a directory in textbox2. The user sets how often they want the file to by copied in textbox3. The user hits run and the program copies the file to the new location, adding a number to the file name each time it is copied (to avoid overwrites). That all works fine, but I want the user to have the choice to either copy the file by time or when the file is modified.
How can I use the FileSystemWatcher to look for modification in the directory (given in textbox1) and then call the statement that copies the specified directory to the target destination (specified in textbox 2)?
Additional Note:
In one tutorial the FileSystemWatcher path was set up by doing this
Dim watched As String = System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures")
Dim fsw As New FileSystemWatcher(watched)
The path that the code directs to is "C:\Users[User Name]\Pictures" .
I can't find a resource online that shows what variables ".GetEnvironmentVariable" accepts or even what the variables mean. This is one of many reasons why I am having trouble with this last bit code.
GetEnvironmentVariable returns the value for the specified environment for the current process.
In the case of your example, USERPROFILE is the path to the folder for the current user. For example, on my laptop USERPROFILE is C:\Users\Tim.
The output of System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures") would be the USERPROFILE path plus "Pictures" - to continue with my example, it would be C:\Users\Tim\Pictures - which is the physical path to the My Pictures folder for my user account.
To get a list of all your environment variables, if you're curious, go to the DOS prompt and type in SET and hit return.
To answer your original question, you need to handle the FileSystemWatcher.Changed Event.
For example:
Private Shared Sub OnChanged(source As Object, e As RenamedEventArgs)
' Do your copy here
End Sub
You can hook the event handler up to your FileWatcher like this:
AddHandler fsw.Changed, AddressOf OnChanged
However, pay attention to this warning from the MSDN docs.
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.
This is the code I used it does what I want it to.
Option Explicit On
Option Strict On
Imports System.IO
Imports Microsoft.VisualBasic ' I don't know this one, but the code worked without this.
Imports System.Security.Permissions ' I don't know the exactly what this does but the
' http://msdn.microsoft.com/ site did this, I assume it'f to allow for permission to
' watch files? The code still worked for me without this
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
Dim directoryPath As String = Path.GetDirectoryName(TextBox1.Text)
Dim varFileSystemWatcher As New FileSystemWatcher()
varFileSystemWatcher.Path = directoryPath
varFileSystemWatcher.NotifyFilter = (NotifyFilters.LastWrite)
varFileSystemWatcher.Filter = Path.GetFileName(TextBox1.Text) ' I know this
' limits what's being watched to one file, but right now this is
' what I want the program to do.
AddHandler varFileSystemWatcher.Changed, AddressOf OnChanged
varFileSystemWatcher.EnableRaisingEvents = True
End Sub
Private Sub OnChanged(source As Object, ByVal e As FileSystemEventArgs)
My.Computer.FileSystem.CopyFile(e.FullPath, TextBox2.Text & "\" & e.Name, True)
End Sub