Search files based on the date modified entered by user in console application VB.NET

我只是一个虾纸丫 提交于 2019-12-25 13:52:35

问题


i been given a assignment to create one console application using VB.NET. The assignment will able to open the file that based on the date modified entered by user. can anyone help me to solve my problem. I'm new in vb.net and most of the tutorial are using C# . together i put the latest code i has already done but still if i put the date modified the error file will display .

Thank you in advance

Imports System.IO

Module Module3
    Sub Main()
        While True

            ' Read value.

            Dim s As DateTime = Console.ReadLine()

            ' Test the value.
            If s = File.GetLastWriteTime("path") Then

                Dim p As New Process()
                p.StartInfo = New ProcessStartInfo("notepad.exe", "path")
                p.Start()

            Else

                Dim p As New Process()
                p.StartInfo = New ProcessStartInfo("notepad.exe", "path2")
                p.Start()

            End If

            ' Write the value.
            Console.WriteLine("You typed " + s)

        End While
    End Sub
End Module

回答1:


in the code snippet you are not searching for a file you are setting the time last modified which isn't what you want to do.

you will need to search through each file until you find the date modified information which is inputted by the user:-

dim s as datetime = console.readline()

'Target Directory
Dim directory = "C:\Users\Peter\Desktop\TestFolder\"


For Each filename As String In IO.Directory.GetFiles(directory, "*", IO.SearchOption.AllDirectories)
    Dim fName As String = IO.Path.GetExtension(filename)
    dim datemod as string = File.GetLastWriteTime(directory & fname)

        If s = datemod Then
            Dim p As New Process()
            p.StartInfo = New ProcessStartInfo("notepad.exe", directory & fname)
            p.Start()
        Else
            'do something else
        endif
next

things that you will need to add are, what to do when it doesn't find a file with that variable.

hope this gets you a little further.



来源:https://stackoverflow.com/questions/42826832/search-files-based-on-the-date-modified-entered-by-user-in-console-application-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!