Function getItems()
''# make a reference to a directory
Dim di As New IO.DirectoryInfo("https://ipossum.svn.sourceforge.net/svnroot/ipossum/")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
''#list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Function
That is my code and it did not work. The error was "Warning 1 Function 'getItems' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Users\******\AppData\Local\Temporary Projects\iPossum\Form1.vb 13 5 iPossum".
How do I do this? Thanks!
To fix the error you're asking about, just change the word Function to Sub.
However, after you do this you're code still won't work. You'll have a new error, because the System.IO directory and file classes only work on the local file system. You can't reference a remote https location that way. You'll need to use System.Net.HttpWebRequest/System.Net.HttpWebResponse or System.Net.WebClient instead, and that means essentially starting from scratch with this code.
A very simple example that may or may not work, depending on the https requirement:
Dim fileList As String
Using wc As New WebClient
fileList = wc.DownloadString("https://ipossum.svn.sourceforge.net/svnroot/ipossum/")
End Using
''# Here you'll have to parse the file names out of this response on your own
Related
I am trying to move only one file that is the newest created or edited from a directory to another folder on two different servers. How would I only move the newest file from one directory to the next instead of all the files in the folder?
Here is the code I am using to move the file over.
My.Computer.FileSystem.CopyDirectory("\\172.16.1.42\s$\SQLBackup\FWP", "\\172.16.1.22\F$\BackupRestore", True)
It shouldn't be too hard to determine which file is the newest one. A simple way to do this would be to retrieve information about all the files in a directory and then cycle through them to find the most recent.
You could do something like this:
Imports System.IO
Dim di As New IO.DirectoryInfo("c:\") ' Change this to match your directory
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim mostRecentFile As IO.FileInfo = Nothing
Dim mostRecentTimeStamp As DateTime = Nothing
DateTime.TryParse("01/01/1900 0:00:00", mostRecentTimeStamp) ' Set to early date
For Each dra In diar1 ' Cycle through each file in directory
If File.GetLastAccessTime(dra.FullName) > mostRecentTimeStamp Then
mostRecentTimeStamp = File.GetLastAccessTime(dra.FullName)
mostRecentFile = dra
End If
Next
Debug.Print(mostRecentFile.FullName) ' Will show you the result
' Use mostRecentFile.Copy to copy to new directory
Hopefully that solves your problem. If not, let me know. There may be an issue about this routine detecting hidden files, so if you see something like that, post back here. You will also want to add code to detect if no new file is found, for example.
I'm doing some experiments lately but I can't make this one work. Here's my code:
If cbHistory.Checked = True Then
Dim di As New DirectoryInfo("C:\Users\" & userName & "\AppData\Local\Google\Chrome\UserData")
Dim diar1 As FileInfo() = di.GetFiles("*.*", SearchOption.AllDirectories)
Dim dra As FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
listToDelete.Items.Add(dra)
Next
End If
My code takes a the list of files located in the specified location and put it in a list box. I have 2 more block of code like that (cbCache and cbCookies).
Now my problem is, I want that if I unchecked one of them, the cbHistory checkbox for example, it will remove the items it took from the directory. Now, I can't use listbox.items.clear() because it will remove everything in the list box. So what can I do here?
Any help would be appreciated. Thank you! :)
Hi I'm doing some testing on unzipping files using Shell32. In this code there's a several files in "C:\Temp" and I unzip those files by creating a new folder name Unzip which will store the unzipped files.
So the question is how to check if the file already exists, the program will hold or continue to the next function. Here's the code.
Dim di As New IO.DirectoryInfo("C:\Temp")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim sc As New Shell32.Shell()
For Each dra In diar1
IO.Directory.CreateDirectory("C:\Temp\Unzip\" & dra.Name & "")
Dim output As Shell32.Folder = sc.NameSpace("C:\Temp\Unzip\" & dra.Name & "")
Dim input As Shell32.Folder = sc.NameSpace("C:\Temp\" & dra.Name & "")
output.CopyHere(input.Items, 4)
Next
Thanks In advance :)
File.Exists() is bad for this kind of thing.
What you want to do instead is attempt to open your output stream in a way that will fail (throw an exception) if the file is already there, and handle the exception. Unfortunately, the Shell32 process you're using right now doesn't allow for that. However, it's not that hard to unzip files using .Net directly. Especially if you can use .Net 4.5, this is pretty easy: there is new ZipArchive support built-in. In prior versions of .Net, you may want to look at SharpZipLib. It's in C#, but you will be able use the library from VB.Net.
After some great arguments made by other users in this question: How to write to a text file inside of the application, I decided to not use a resource file, instead create a file in a folder & then read/write from there.
Although, for some reason, I can't seem to write to the file in question, it keeps throwing an exception telling me the file is already in use by another process.
Here's the code which I use for writing to this file.
If System.IO.File.Exists(credentials) Then
Dim objWriter As New System.IO.StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
Else
System.IO.Directory.CreateDirectory(Mid(My.Application.Info.DirectoryPath, 1, 1) & ":\ProgramData\DayZAdminPanel")
System.IO.File.Create(credentials)
Dim objWriter As New System.IO.StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
End If
Any ideas on how I can write to the text file in question?
There's a good chance that a previous iteration of your application failed to properly close access to the file in the StreamWriter. Since your constructor is set to overwrite (and not append) to the file, this could be the source.
Try setting up your application with a "Using" statement to properly open/close the file:
If System.IO.File.Exists(credentials) Then
Using objWriter As StreamWriter = New StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
End Using
Else
System.IO.Directory.CreateDirectory(Mid(My.Application.Info.DirectoryPath, 1, 1) & ":\ProgramData\DayZAdminPanel")
System.IO.File.Create(credentials)
Using objWriter As StreamWriter = New StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
End Using
End If
It does look rather redundant to have a Using block and a close statement, but this ensures access to your file even if an exception occurs.
You are trying to create a directory in the common application data directory. This directory should be found using the Environment class methods and enums because is different between operating systems. However you use the value credentials for the filename. I suppose that you want to store your datafile in the common application directory and not in a place where there is no permission to write data files (Like C:\program files (x86)).
Then, to avoid problems with file stream not correctly closed try to use the Using statement that assures a correct closure and disposal of your file resource (No need to call close inside a Using).
Also, notice that StreamWriter is perfectly capable to create the file if it doesn't exists or if you wish to overwrite the previous contents (passing false for the Append flag).
So your code could be reduced to these lines.
' Get the common application data directory (could be different from Win7 and XP)
Dim workDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
' Combine with your own working data directory
workDir = Path.Combine(workdir, "DayZAdminPanel")
' Create if not exists
If Not Directory.Exists(workDir) Then
Directory.CreateDirectory(workDir)
End If
' Create/Overwrite your data file in a subfolder of the common application data folder
Dim saveFile = Path.Combine(workDir, Path.GetFileName(credentials))
Using objWriter = New System.IO.StreamWriter(saveFile, False)
objWriter.WriteLine(remember)
End Using
File.Create returns an opened FileStream that you should be passing into the StreamWriter constructor on the subsequent, rather than passing the filename again, or you can just omit the File.Create call altogether.
You might want to look into a Using block for the StreamWriter so it gets predictably closed.
I’m trying to programmatically clear out my temps folder however it can't delete files that are in use, which would be okay as long as it would then delete all the files not in use. However my code basically demands either we delete all or none, not just those not in use.
Below is the code could anybody please tell me how I can work around this?
'Deletes files in temporary directory...
Dim MYDAYS_2 As String
Dim MYTEMPFOLDER As String = System.IO.Path.GetTempPath
'this reads the regestry key otherwise gives a default value in its place (365)...
MYDAYS_2 = Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\AA", "HELLO", "365")
'Deletes all files older then the day specifyed in the variable MDAYS...
For Each file As IO.FileInfo In New IO.DirectoryInfo(MYTEMPFOLDER).GetFiles("*.*")
If (Now - file.CreationTime).Days > MYDAYS_2 Then file.Delete()
Next
You could use a simple try/catch:
For Each file As IO.FileInfo In New IO.DirectoryInfo(MYTEMPFOLDER).GetFiles("*.*")
If (Now - file.CreationTime).Days > MYDAYS_2 Then
Try
file.Delete()
Catch
' log exception or ignore '
End Try
End If
Next