I have managed to get my program running as I would like it to. The issue I am running into is when I write to an XML file. I feel like I have everything set up correctly to write the contents of the datatable to an XML file. While the XML file saves in the specified location, it is lacking any of the database columns or rows that I specified beforehand.
Listed below is the code:
Public Class Test
Public Shared Sub Main()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
Dim DR1 As String = ""
Dim DR2 As String = ""
Dim DR3 As String = ""
Dim DR4 As String = ""
Dim DR5 As String = ""
Dim DR1TotalSize, DR2TotalSize, DR3TotalSize, DR4TotalSize, DR5TotalSize As Long
Dim DR1Used, DR2Used, DR3Used, DR4Used, DR5Used As Long
Dim DR1FreeSpace, DR2FreeSpace, DR3FreeSpace, DR4FreeSpace, DR5FreeSpace As Long
Dim dt As New DataTable("DriveInformation")
Dim Column_1st As New DataColumn("Date/Time")
Dim Column_2nd As New DataColumn("Server")
Dim Column_3rd As New DataColumn("Drive")
Dim Column_4th As New DataColumn("TotalSpace")
Dim Column_5th As New DataColumn("UsedSpace")
Dim Column_6th As New DataColumn("RemainingSpace")
Dim Column_7th As New DataColumn("Status")
' Get information and Write to Console.
Console.WriteLine("Server Name: {0}", System.Net.Dns.GetHostName)
Console.WriteLine("Date: {0}", DateTime.Now.ToString("yyyy/MM/dd"))
Console.WriteLine("")
Dim DriveNumber As Int16 = 0
For Each d In allDrives
DriveNumber = DriveNumber + 1
Console.WriteLine("Drive {0}", d.Name)
Console.WriteLine(" Drive type: {0}", d.DriveType)
If d.IsReady = True And d.TotalSize < 1099511627776 Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total size of drive: {0, 15} GB ",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024))
ElseIf d.IsReady = True And d.TotalSize >= 1099511627776 Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total size of drive: {0, 15} TB ",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024 / 1024))
End If
If d.IsReady = True And d.TotalSize - d.TotalFreeSpace < 1099511627776 Then
Console.WriteLine(
" Total used space: {0, 15} GB",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024 - d.TotalFreeSpace / 1024 / 1024 / 1024))
ElseIf d.IsReady = True And d.TotalSize - d.TotalFreeSpace >= 1099511627776 Then
Console.WriteLine(
" Total used space: {0, 15} TB",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024 / 1024 - d.TotalFreeSpace / 1024 / 1024 / 1024 / 1024))
End If
If d.IsReady = True And d.TotalFreeSpace < 1099511627776 Then
Console.WriteLine(
" Total available space: {0, 15} GB",
FormatNumber(d.TotalFreeSpace / 1024 / 1024 / 1024))
Console.WriteLine("")
ElseIf d.IsReady = True And d.TotalFreeSpace >= 1099511627776 Then
Console.WriteLine(
" Total available space: {0, 15} TB",
FormatNumber(d.TotalFreeSpace / 1024 / 1024 / 1024 / 1024))
Console.WriteLine("")
End If
If DriveNumber = 1 Then
DR1 = d.Name
DR1TotalSize = d.TotalSize
DR1Used = d.TotalSize - d.TotalFreeSpace
DR1FreeSpace = d.TotalFreeSpace
End If
If DriveNumber = 2 Then
DR2 = d.Name
DR2TotalSize = d.TotalSize
DR2Used = d.TotalSize - d.TotalFreeSpace
DR2FreeSpace = d.TotalFreeSpace
End If
If DriveNumber = 3 Then
DR3 = d.Name
DR3TotalSize = d.TotalSize
DR3Used = d.TotalSize - d.TotalFreeSpace
DR3FreeSpace = d.TotalFreeSpace
End If
If DriveNumber = 4 Then
DR4 = d.Name
DR4TotalSize = d.TotalSize
DR4Used = d.TotalSize - d.TotalFreeSpace
DR4FreeSpace = d.TotalFreeSpace
End If
If DriveNumber = 5 Then
DR5 = d.Name
DR5TotalSize = d.TotalSize
DR5Used = d.TotalSize - d.TotalFreeSpace
DR5FreeSpace = d.TotalFreeSpace
End If
Next
' Put Information into DataTable
For Each d In allDrives
DriveNumber = DriveNumber + 1
If DriveNumber = 1 Then
Dim drow1 As DataRow = dt.NewRow
dt.Columns.Add(Column_1st)
dt.Columns.Add(Column_2nd)
dt.Columns.Add(Column_3rd)
dt.Columns.Add(Column_4th)
dt.Columns.Add(Column_5th)
dt.Columns.Add(Column_6th)
dt.Columns.Add(Column_7th)
drow1("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
drow1("Server") = System.Net.Dns.GetHostName
drow1("Drive") = d.Name
drow1("TotalSpace") = d.TotalSize
drow1("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow1("RemainingSpace") = d.TotalFreeSpace
drow1("Status") = d.IsReady
dt.Rows.Add(drow1)
End If
If DriveNumber = 2 Then
Dim drow2 As DataRow = dt.NewRow
dt.Columns.Add(Column_1st)
dt.Columns.Add(Column_2nd)
dt.Columns.Add(Column_3rd)
dt.Columns.Add(Column_4th)
dt.Columns.Add(Column_5th)
dt.Columns.Add(Column_6th)
dt.Columns.Add(Column_7th)
drow2("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
drow2("Server") = System.Net.Dns.GetHostName
drow2("Drive") = d.Name
drow2("TotalSpace") = d.TotalSize
drow2("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow2("RemainingSpace") = d.TotalFreeSpace
drow2("Status") = d.IsReady
dt.Rows.Add(drow2)
End If
If DriveNumber = 3 Then
Dim drow3 As DataRow = dt.NewRow
dt.Columns.Add(Column_1st)
dt.Columns.Add(Column_2nd)
dt.Columns.Add(Column_3rd)
dt.Columns.Add(Column_4th)
dt.Columns.Add(Column_5th)
dt.Columns.Add(Column_6th)
dt.Columns.Add(Column_7th)
drow3("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
drow3("Server") = System.Net.Dns.GetHostName
drow3("Drive") = d.Name
drow3("TotalSpace") = d.TotalSize
drow3("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow3("RemainingSpace") = d.TotalFreeSpace
drow3("Status") = d.IsReady
dt.Rows.Add(drow3)
End If
If DriveNumber = 4 Then
Dim drow4 As DataRow = dt.NewRow
dt.Columns.Add(Column_1st)
dt.Columns.Add(Column_2nd)
dt.Columns.Add(Column_3rd)
dt.Columns.Add(Column_4th)
dt.Columns.Add(Column_5th)
dt.Columns.Add(Column_6th)
dt.Columns.Add(Column_7th)
drow4("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
drow4("Server") = System.Net.Dns.GetHostName
drow4("Drive") = d.Name
drow4("TotalSpace") = d.TotalSize
drow4("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow4("RemainingSpace") = d.TotalFreeSpace
drow4("Status") = d.IsReady
dt.Rows.Add(drow4)
End If
If DriveNumber = 5 Then
Dim drow5 As DataRow = dt.NewRow
dt.Columns.Add(Column_1st)
dt.Columns.Add(Column_2nd)
dt.Columns.Add(Column_3rd)
dt.Columns.Add(Column_4th)
dt.Columns.Add(Column_5th)
dt.Columns.Add(Column_6th)
dt.Columns.Add(Column_7th)
drow5("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
drow5("Server") = System.Net.Dns.GetHostName
drow5("Drive") = d.Name
drow5("TotalSpace") = d.TotalSize
drow5("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow5("RemainingSpace") = d.TotalFreeSpace
drow5("Status") = d.IsReady
dt.Rows.Add(drow5)
End If
Next
' Write Information to DataTable
dt.WriteXml("c:/temp/ServerDriveInformation_" + DateTime.Now.ToString("yyyy MM dd h mm ss tt ") + ".xml")
' Write Information to Database
End Sub
End Class
However when I run this program, this is the results of the published XML file specified within the code:
Maybe I am missing something here, or incorreclty listed variables. I just cannot pin point why it is not writing the data table to the XML file. Any help or advice would be greatly appreciated!
The big problem was the adding of columns, only need it once. Made a few changes to get the code to run and it did produce XML.
Dim dt As New DataTable("DriveInformation")
Dim Column_1st As New DataColumn("Date/Time")
Dim Column_2nd As New DataColumn("Server")
Dim Column_3rd As New DataColumn("Drive")
Dim Column_4th As New DataColumn("TotalSpace")
Dim Column_5th As New DataColumn("UsedSpace")
Dim Column_6th As New DataColumn("RemainingSpace")
Dim Column_7th As New DataColumn("Status")
dt.Columns.Add(Column_1st)
dt.Columns.Add(Column_2nd)
dt.Columns.Add(Column_3rd)
dt.Columns.Add(Column_4th)
dt.Columns.Add(Column_5th)
dt.Columns.Add(Column_6th)
dt.Columns.Add(Column_7th)
' Get information and Write to Console.
Console.WriteLine("Server Name: {0}", System.Net.Dns.GetHostName)
Console.WriteLine("Date: {0}", DateTime.Now.ToString("yyyy/MM/dd"))
Console.WriteLine("")
Dim DriveNumber As Integer = 0
Dim allDrives() As IO.DriveInfo = IO.DriveInfo.GetDrives()
Dim d As IO.DriveInfo
For Each d In allDrives
DriveNumber = DriveNumber + 1
Console.WriteLine("Drive {0}", d.Name)
Console.WriteLine(" Drive type: {0}", d.DriveType)
If d.IsReady = True And d.TotalSize < 1099511627776 Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total size of drive: {0, 15} GB ",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024))
ElseIf d.IsReady = True And d.TotalSize >= 1099511627776 Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total size of drive: {0, 15} TB ",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024 / 1024))
End If
If d.IsReady = True And d.TotalSize - d.TotalFreeSpace < 1099511627776 Then
Console.WriteLine(
" Total used space: {0, 15} GB",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024 - d.TotalFreeSpace / 1024 / 1024 / 1024))
ElseIf d.IsReady = True And d.TotalSize - d.TotalFreeSpace >= 1099511627776 Then
Console.WriteLine(
" Total used space: {0, 15} TB",
FormatNumber(d.TotalSize / 1024 / 1024 / 1024 / 1024 - d.TotalFreeSpace / 1024 / 1024 / 1024 / 1024))
End If
If d.IsReady = True And d.TotalFreeSpace < 1099511627776 Then
Console.WriteLine(
" Total available space: {0, 15} GB",
FormatNumber(d.TotalFreeSpace / 1024 / 1024 / 1024))
Console.WriteLine("")
ElseIf d.IsReady = True And d.TotalFreeSpace >= 1099511627776 Then
Console.WriteLine(
" Total available space: {0, 15} TB",
FormatNumber(d.TotalFreeSpace / 1024 / 1024 / 1024 / 1024))
Console.WriteLine("")
End If
' If DriveNumber = 1 Then
' DR1 = d.Name
' DR1TotalSize = d.TotalSize
' DR1Used = d.TotalSize - d.TotalFreeSpace
' DR1FreeSpace = d.TotalFreeSpace
' End If
' If DriveNumber = 2 Then
' DR2 = d.Name
' DR2TotalSize = d.TotalSize
' DR2Used = d.TotalSize - d.TotalFreeSpace
' DR2FreeSpace = d.TotalFreeSpace
' End If
' If DriveNumber = 3 Then
' DR3 = d.Name
' DR3TotalSize = d.TotalSize
' DR3Used = d.TotalSize - d.TotalFreeSpace
' DR3FreeSpace = d.TotalFreeSpace
' End If
' If DriveNumber = 4 Then
' DR4 = d.Name
' DR4TotalSize = d.TotalSize
' DR4Used = d.TotalSize - d.TotalFreeSpace
' DR4FreeSpace = d.TotalFreeSpace
' End If
' If DriveNumber = 5 Then
' DR5 = d.Name
' DR5TotalSize = d.TotalSize
' DR5Used = d.TotalSize - d.TotalFreeSpace
' DR5FreeSpace = d.TotalFreeSpace
' End If
Next
' Put Information into DataTable
For Each d In allDrives
DriveNumber = DriveNumber + 1
If DriveNumber = 1 Then
Dim drow1 As DataRow = dt.NewRow
drow1("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
'drow1("Server") = System.Net.Dns.GetHostName
drow1("Drive") = d.Name
drow1("TotalSpace") = d.TotalSize
drow1("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow1("RemainingSpace") = d.TotalFreeSpace
drow1("Status") = d.IsReady
dt.Rows.Add(drow1)
End If
If DriveNumber = 2 Then
Dim drow2 As DataRow = dt.NewRow
drow2("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
'drow2("Server") = System.Net.Dns.GetHostName
drow2("Drive") = d.Name
drow2("TotalSpace") = d.TotalSize
drow2("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow2("RemainingSpace") = d.TotalFreeSpace
drow2("Status") = d.IsReady
dt.Rows.Add(drow2)
End If
If DriveNumber = 3 Then
Dim drow3 As DataRow = dt.NewRow
drow3("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
'drow3("Server") = System.Net.Dns.GetHostName
drow3("Drive") = d.Name
drow3("TotalSpace") = d.TotalSize
drow3("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow3("RemainingSpace") = d.TotalFreeSpace
drow3("Status") = d.IsReady
dt.Rows.Add(drow3)
End If
If DriveNumber = 4 Then
Dim drow4 As DataRow = dt.NewRow
drow4("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
'drow4("Server") = System.Net.Dns.GetHostName
drow4("Drive") = d.Name
drow4("TotalSpace") = d.TotalSize
drow4("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow4("RemainingSpace") = d.TotalFreeSpace
drow4("Status") = d.IsReady
dt.Rows.Add(drow4)
End If
If DriveNumber = 5 Then
Dim drow5 As DataRow = dt.NewRow
drow5("Date/Time") = DateTime.Now.ToString("yyyy/MM/dd")
'drow5("Server") = System.Net.Dns.GetHostName
drow5("Drive") = d.Name
drow5("TotalSpace") = d.TotalSize
drow5("UsedSpace") = d.TotalSize - d.TotalFreeSpace
drow5("RemainingSpace") = d.TotalFreeSpace
drow5("Status") = d.IsReady
dt.Rows.Add(drow5)
End If
Next
' Write Information to DataTable
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
path = IO.Path.Combine(path, "ServerDriveInformation_.xml")
dt.WriteXml(path)