locked
Compare FileInfo array with DataTable RRS feed

  • Question

  • User-289010069 posted

    I have FileInfo array that I bind to a gridview control to show users a list of files in a directory.  I have another list of files that I retrieve from a web service and store in a dataTable.  I'm trying to figure out how to compare the file name value in the FileInfo array with the file name value in the dataTable.  If the file names match, I would like to notify the user with a "Yes" value in a new column in my existing FileInfo array gridview control.  Can anyone assist with comparing the values and updating a new column in the existing gridview control?

    
    Code
      If Directory.Exists(FilePath) Then
                Dim dirInfo As New DirectoryInfo(FilePath)
                Dim fileInfoArray() As FileInfo = dirInfo.GetFiles("*.*")
    
    
               'Populate dataTable with external file names
    	    Dim dt As New DataTable = GetExternalFiles()
    
                For Each f As FileInfo In fileInfoArray
                   'Add file name compare
                Next
               
                FilesGrid.DataSource = fileInfoArray
                FilesGrid.DataBind()
    
            End If
    
    
     
    html
      <asp:GridView ID="FilesGrid"   runat="server" 
                                    AutoGenerateColumns="False"  >
                                    <Columns>
                                
                                        <asp:TemplateField HeaderText="File Name">
                                            <ItemTemplate>
                                                <asp:HyperLink ID="hlFile" runat="server" Target="_blank"></asp:HyperLink>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="LastWriteTime" HeaderText="File Date" HtmlEncode="False"
                                            DataFormatString="{0:d}" />
                                    </Columns>
                                </asp:GridView>

    Monday, November 25, 2019 4:23 AM

Answers

  • User1535942433 posted

    Hi HellowWorld18,

    Accroding to your description,I suggest you could creat a new class to store fileInfo and  adn the State  of result. And you could compare file names with datatble values to get the the State  of result.

    More details ,you could refer to below code:

    ASPX:

    <div>
                  <asp:GridView ID="FilesGrid"   runat="server">
                 </asp:GridView>
            </div>

    Code-Behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim pathToDirectory As String = "C:\Users\yijings\Desktop\image"
                Dim diDir As DirectoryInfo = New DirectoryInfo(pathToDirectory)
    
    
                Dim fileNames = Directory.GetFiles(pathToDirectory)
                Dim fileInfos As List(Of FileInfo) = New List(Of FileInfo)()
    
                For Each fi In diDir.GetFiles()
                    Dim fileInfo As FileInfo = New FileInfo()
                    fileInfo.FileName = fi.Name
                    Dim dt As DataTable = New DataTable()
                    dt.Columns.Add("Name", GetType(String))
                    dt.Rows.Add("apple.jpg")
                    dt.Rows.Add("BB")
                    dt.Rows.Add("CC")
                    Dim isEuqal As Boolean = False
    
                    For i As Integer = 0 To dt.Rows.Count - 1
    
                        If Convert.ToString(fi.Name) = Convert.ToString(dt.Rows(i)("Name")) Then
                            isEuqal = True
                            Exit For
                        End If
                    Next
    
                    If isEuqal Then
                        fileInfo.State = "Yes"
                    Else
                        fileInfo.State = "No"
                    End If
    
                    fileInfos.Add(fileInfo)
                Next
    
                FilesGrid.DataSource = fileInfos
                FilesGrid.DataBind()
            End If
        End Sub
        Public Class FileInfo
            Public Property FileName As String
            Public Property State As String
        End Class

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 26, 2019 10:27 AM

All replies

  • User1535942433 posted

    Hi HellowWorld18,

    Accroding to your description,I suggest you could creat a new class to store fileInfo and  adn the State  of result. And you could compare file names with datatble values to get the the State  of result.

    More details ,you could refer to below code:

    ASPX:

    <div>
                  <asp:GridView ID="FilesGrid"   runat="server">
                 </asp:GridView>
            </div>

    Code-Behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim pathToDirectory As String = "C:\Users\yijings\Desktop\image"
                Dim diDir As DirectoryInfo = New DirectoryInfo(pathToDirectory)
    
    
                Dim fileNames = Directory.GetFiles(pathToDirectory)
                Dim fileInfos As List(Of FileInfo) = New List(Of FileInfo)()
    
                For Each fi In diDir.GetFiles()
                    Dim fileInfo As FileInfo = New FileInfo()
                    fileInfo.FileName = fi.Name
                    Dim dt As DataTable = New DataTable()
                    dt.Columns.Add("Name", GetType(String))
                    dt.Rows.Add("apple.jpg")
                    dt.Rows.Add("BB")
                    dt.Rows.Add("CC")
                    Dim isEuqal As Boolean = False
    
                    For i As Integer = 0 To dt.Rows.Count - 1
    
                        If Convert.ToString(fi.Name) = Convert.ToString(dt.Rows(i)("Name")) Then
                            isEuqal = True
                            Exit For
                        End If
                    Next
    
                    If isEuqal Then
                        fileInfo.State = "Yes"
                    Else
                        fileInfo.State = "No"
                    End If
    
                    fileInfos.Add(fileInfo)
                Next
    
                FilesGrid.DataSource = fileInfos
                FilesGrid.DataBind()
            End If
        End Sub
        Public Class FileInfo
            Public Property FileName As String
            Public Property State As String
        End Class

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 26, 2019 10:27 AM
  • User-289010069 posted

    I was able to resolve the issue using your recommendation and example.  Thanks!

    Wednesday, November 27, 2019 11:57 PM