Problem With Zipping An AppData Folder In VB.Net - vb.net

Code : ZipFile.CreateFromDirectory((Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Folder"))
"Overload resolution failed because no accessible 'CreateFromDirectory' accepts this number of arguments."
Any Idea Why?

Here is a complete solution, which creates an archive called "tmp.zip" in the current directory, if the source folder exists.
Your example isn't compiling because you have to supply the path of the destination archive as well as the source folder to archive.
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim destArchive = "tmp.zip"
Dim sourceFolder As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "folder")
If Directory.Exists(sourceFolder) Then
If File.Exists(destArchive) Then
File.Delete(destArchive)
End If
ZipFile.CreateFromDirectory(sourceFolder, destArchive)
End If
Console.ReadLine()
End Sub
End Module

Related

Rename files sequentially

with reference to Renaming all files in a folder
on running the below code, get type def errors:
Type 'DirectoryInfo' is not defined
Type 'FileInfo' is not defined
how to resolve these errors. Please suggest.
Dim sourcePath As String = "E:\testrenamerbackup\vbdotnet"
Dim searchPattern As String = "*.doc"
Dim curDir As New DirectoryInfo(sourcePath)
Dim i As Integer = 0
For Each fi As FileInfo In curDir.GetFiles(searchPattern).OrderBy(Function(num) num.CreationTime)
File.Move(fi.FullName, Path.Combine(fi.Directory.FullName, "docFile_" & i & ".doc"))
i += 1
Next
Add Import System.IO on top of your vb.net class file, for example
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' your code...
End Sub
End Class
System.IO Namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support including DirectoryInfo, FileInfo. by adding Import System.IO you can use those types and methods in the namespace.

VB.NET creating a directory then downloading and running a file in Appdata

For my application I am trying to create a directory for my program, download an executable into the folder and then run it but I am getting two errors,
Error 1 Too many arguments to 'Public Shared Function Exists(path As String) As Boolean'.
Error 2 Value of type 'String' cannot be converted to 'System.Security.AccessControl.DirectorySecurity'.
any Idea what I am doing wrong?
Imports System.DirectoryServices
Imports System.IO
Module Download
Public Sub DownloadExecute(ByVal url As String)
Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
If (Not System.IO.Directory.Exists(folder, "TankGame\Installer")) Then
System.IO.Directory.CreateDirectory(folder, "TankGame\Installer")
End If
Dim Downloadedfile = Path.Combine(folder, "TankGame\Installer\installer.exe")
My.Computer.Network.DownloadFile("url", Downloadedfile)
Process.Start(Downloadedfile)
End Sub
End Module

How to get the full path of a file through the file's name

I am trying to get the path of a file from the name of it. I made an executable file on my desktop, Commands.exe, I am trying to get the full path of it through a console application. I want the program to search the whole of my computer for the file, (it is on my desktop for simplicity) and return the path, bearing in mind the file could be anywhere. The path I want the program to return is:
"C:\Users\Jimbob\Desktop\Commands.exe"
My Code:
Imports System.IO
Module Module1
Dim fileLocation As String
Dim filename As String = "Commands.exe"
Sub Main()
fileLocation = Path.GetFullPath(filename)
Console.WriteLine(fileLocation)
Console.ReadLine()
End Sub
End Module
but instead it prints out
"C:\Users\Jimbob\Documents\Visual Studio 2012\Projects\get path
test\get path test\bin\Debug\Commands.exe"
It prints out the path of the project all of my code is in. In this path, there isn't even a Commands.exe in the Debug folder.
Help?
or couldn't you just
dim fileName as string = My.Application.Info.DirectoryPath() & "\Commands.exe"
You can use the Directory.GetFiles or the FileSystem.GetFiles methods to do this.
Something along the lines of:
fileLocation = My.Computer.FileSystem.GetFiles("C:\",
FileIO.SearchOption.SearchAllSubDirectories,
filename)(0)

Check for newest folder in a directory with VB, then go to that directory and repeat the process

I am very new to VB
i am trying to write a script in VB that checks a particular directory for the newest folder (or the last one to be modified), and then go into that folder and do the same thing again. So i was wondering how i should go about doing this? i already have a script that goes to the last folder alphabetically but i need to know how to actually make it choose the latest folder?
script:
Imports System
Imports System.IO
Module Module1
Sub Main()
Dim dir As String
Dim dir2 As String
Dim fil As String
For Each dir In Directory.GetDirectories("\\alinta.net.int\alintadata\SOP\mdl database\Weekly Readings")
Console.WriteLine(dir)
Dim lat As New System.IO.DirectoryInfo(dir)
Console.WriteLine(lat.LastAccessTime)
Next
For Each dir2 In Directory.GetDirectories(dir)
Console.WriteLine(dir2)
Dim lat2 As New System.IO.DirectoryInfo(dir2)
Console.WriteLine(lat2.LastAccessTime)
Next
For Each fil In Directory.GetFiles(dir2, "*.txt")
Console.WriteLine(fil)
Next
Console.WriteLine("...")
Console.ReadLine()
End Sub
End Module

Trying to "iterate" a folder

I'm not sure if I'm using the right word: I'm trying to "iterate" trought a folder of files using Do While
My failed attempt:
Sub Main()
Dim myurl As String
Dim m As Object
myurl = "\\myroute\myroute"
ChDir(myurl)
m = Dir(myurl)
Do While m <> "" '<--m = nothing?
...'lines not important here
Loop
End Sub
But for some reason, when I start debugging, m has no value(m=nothing) so "do while" bucle is ignored.
What am I doing wrong? Any idea how to do it?
"\myroute\myroute" does exists
There is a very helpful method in .NET to do this Directory.EnumerateFiles:
For Each fileName As String In Directory.EnumerateFiles(myDirectory)
' Add your code
Next
You have to import the System.IO namespace to use the Directory class:
Imports System.IO