Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. search hard drive for files

search hard drive for files

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jds1207
    wrote on last edited by
    #1

    I am trying to write a program that will search the hard drive for files using windows service in vb.net. I already created the service, but I am not sure if my code to search for files is correct. Does anyone have any suggestions or samples on how to search for files using vb.net. Here is the code that I have below: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. Timer1.Enabled = True SearchFiles(".txt") End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Check if the event log exists If Not System.Diagnostics.EventLog.SourceExists("MyService") Then 'Create Log System.Diagnostics.EventLog.CreateEventSource("MyService", "Myservice Log") End If EventLog.Source = "MyService" 'Write to the log System.Diagnostics.EventLog.WriteEntry("MyService Log", "This is log on " & _ CStr(TimeOfDay), EventLogEntryType.Information) End Sub Private Sub SearchFiles(ByVal FileType As String) Dim FilePath As String = "c:\" Dim rootDi As New DirectoryInfo(FilePath) Dim Di As DirectoryInfo For Each Di In rootDi.GetDirectories Directory.GetFiles(FilePath, FileType) Next End Sub Please help! jds1207

    M D P 3 Replies Last reply
    0
    • J jds1207

      I am trying to write a program that will search the hard drive for files using windows service in vb.net. I already created the service, but I am not sure if my code to search for files is correct. Does anyone have any suggestions or samples on how to search for files using vb.net. Here is the code that I have below: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. Timer1.Enabled = True SearchFiles(".txt") End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Check if the event log exists If Not System.Diagnostics.EventLog.SourceExists("MyService") Then 'Create Log System.Diagnostics.EventLog.CreateEventSource("MyService", "Myservice Log") End If EventLog.Source = "MyService" 'Write to the log System.Diagnostics.EventLog.WriteEntry("MyService Log", "This is log on " & _ CStr(TimeOfDay), EventLogEntryType.Information) End Sub Private Sub SearchFiles(ByVal FileType As String) Dim FilePath As String = "c:\" Dim rootDi As New DirectoryInfo(FilePath) Dim Di As DirectoryInfo For Each Di In rootDi.GetDirectories Directory.GetFiles(FilePath, FileType) Next End Sub Please help! jds1207

      M Offline
      M Offline
      MatrixCoder
      wrote on last edited by
      #2

      I would just use File.Exists method. Or were you trying to list each file that you searched?


      Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

      1 Reply Last reply
      0
      • J jds1207

        I am trying to write a program that will search the hard drive for files using windows service in vb.net. I already created the service, but I am not sure if my code to search for files is correct. Does anyone have any suggestions or samples on how to search for files using vb.net. Here is the code that I have below: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. Timer1.Enabled = True SearchFiles(".txt") End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Check if the event log exists If Not System.Diagnostics.EventLog.SourceExists("MyService") Then 'Create Log System.Diagnostics.EventLog.CreateEventSource("MyService", "Myservice Log") End If EventLog.Source = "MyService" 'Write to the log System.Diagnostics.EventLog.WriteEntry("MyService Log", "This is log on " & _ CStr(TimeOfDay), EventLogEntryType.Information) End Sub Private Sub SearchFiles(ByVal FileType As String) Dim FilePath As String = "c:\" Dim rootDi As New DirectoryInfo(FilePath) Dim Di As DirectoryInfo For Each Di In rootDi.GetDirectories Directory.GetFiles(FilePath, FileType) Next End Sub Please help! jds1207

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        First, scrap this service. If you can't write the code to search for files in a normal app, writing it as serice will only serve to frustrate you to death. Besides, the way you've written this code demonstrates a lack of understanding how Services work, how to write them, and most importantly, how to communicate with them to get them to do something useful. The Timer isn't doing anything other than creating an event log on every tick of the timer. You have no method of calling your function. Sure, it CAN work as a search, but you have no methods in here that allow for communication with the service in any way, so you have no way to call the search code. Services require you to do a lot more planning up front. There's no such thing a "whipping one togther" like you can with a Console or Windows Forms app. There's nothing in this code to salvage to get a working service. This thing really does need to be scrapped. Simplify your code and get a working search in a Console or Windows Forms app first. The experience you get with that will show you what a search needs for input and what it can return as output and how it needs to be returned. After that, you can add threading support, which will give you the basis for writing a service that's going to work. You can't do this with a Timer...

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        J 1 Reply Last reply
        0
        • D Dave Kreskowiak

          First, scrap this service. If you can't write the code to search for files in a normal app, writing it as serice will only serve to frustrate you to death. Besides, the way you've written this code demonstrates a lack of understanding how Services work, how to write them, and most importantly, how to communicate with them to get them to do something useful. The Timer isn't doing anything other than creating an event log on every tick of the timer. You have no method of calling your function. Sure, it CAN work as a search, but you have no methods in here that allow for communication with the service in any way, so you have no way to call the search code. Services require you to do a lot more planning up front. There's no such thing a "whipping one togther" like you can with a Console or Windows Forms app. There's nothing in this code to salvage to get a working service. This thing really does need to be scrapped. Simplify your code and get a working search in a Console or Windows Forms app first. The experience you get with that will show you what a search needs for input and what it can return as output and how it needs to be returned. After that, you can add threading support, which will give you the basis for writing a service that's going to work. You can't do this with a Timer...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          J Offline
          J Offline
          jds1207
          wrote on last edited by
          #4

          I have started working on a search in a windows forms app but when I try to run the program I get the following security Exception "Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed." Here is my code: Dim FilePath As String = TextBox1.Text Dim FileType As String = TextBox2.Text Dim diContents As New DirectoryInfo(FilePath) Dim di As FileInfo Dim filename As String = ListBox1.SelectedIndex Dim testname As String For Each di In diContents.GetFiles Dim dirs() As String = Directory.GetFiles(FilePath & di.Name) For Each filename In dirs testname = System.IO.Path.GetFileName(filename) Dim fileTest As String = testname.Remove(0, (Len(testname) - 3)) If UCase(fileTest) = FileType Then ListBox1.Items.Add(testname) End If Next Next End Sub Just to explain what I am trying to do, I want to enter the file path into the textbox where I want to search for the files. I want to enter the file extension(.txt, .mp3) into a textbox to specific what type of file I am looking for. Once the files are found, add them to the list box on the form. Is this code correct? This line of code(Dim diContents As New DirectoryInfo(FilePath)) is highlighted when error occurs. Any ideas? jds1207

          D 1 Reply Last reply
          0
          • J jds1207

            I have started working on a search in a windows forms app but when I try to run the program I get the following security Exception "Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed." Here is my code: Dim FilePath As String = TextBox1.Text Dim FileType As String = TextBox2.Text Dim diContents As New DirectoryInfo(FilePath) Dim di As FileInfo Dim filename As String = ListBox1.SelectedIndex Dim testname As String For Each di In diContents.GetFiles Dim dirs() As String = Directory.GetFiles(FilePath & di.Name) For Each filename In dirs testname = System.IO.Path.GetFileName(filename) Dim fileTest As String = testname.Remove(0, (Len(testname) - 3)) If UCase(fileTest) = FileType Then ListBox1.Items.Add(testname) End If Next Next End Sub Just to explain what I am trying to do, I want to enter the file path into the textbox where I want to search for the files. I want to enter the file extension(.txt, .mp3) into a textbox to specific what type of file I am looking for. Once the files are found, add them to the list box on the form. Is this code correct? This line of code(Dim diContents As New DirectoryInfo(FilePath)) is highlighted when error occurs. Any ideas? jds1207

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Nowhere in your code is there any provisions for handling the inevitable "access denied" error. Your code probably bombed in the root of your C: drive on a folder called "System Volume Information". You have to put some of this code in Try/Catch blocks to handle this eventuallity and deal with it as you must.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            1 Reply Last reply
            0
            • J jds1207

              I am trying to write a program that will search the hard drive for files using windows service in vb.net. I already created the service, but I am not sure if my code to search for files is correct. Does anyone have any suggestions or samples on how to search for files using vb.net. Here is the code that I have below: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. Timer1.Enabled = True SearchFiles(".txt") End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Check if the event log exists If Not System.Diagnostics.EventLog.SourceExists("MyService") Then 'Create Log System.Diagnostics.EventLog.CreateEventSource("MyService", "Myservice Log") End If EventLog.Source = "MyService" 'Write to the log System.Diagnostics.EventLog.WriteEntry("MyService Log", "This is log on " & _ CStr(TimeOfDay), EventLogEntryType.Information) End Sub Private Sub SearchFiles(ByVal FileType As String) Dim FilePath As String = "c:\" Dim rootDi As New DirectoryInfo(FilePath) Dim Di As DirectoryInfo For Each Di In rootDi.GetDirectories Directory.GetFiles(FilePath, FileType) Next End Sub Please help! jds1207

              P Offline
              P Offline
              P P Vilsad
              wrote on last edited by
              #6

              i didn't see any output from your search, just calling di.listfiles(type) but not adding it to anywhere! and you are searching only C drive, what if your computer have morethan one drive. try this initialise your search like this dim drv as driveinfo for each drv in my.computer.filesystem.drives() if drv.isready then searchfiles(drv.rootdirectory,type) end if next and create a searchfiles method with path and filetype as parameters private sub searchfiles(byval path as directoryinfo,byval filetype as string) try 'this will handle the error access denied 'list files dim fi as fileinfo for each fi in di.getfiles(filetype) Application.DoEvents() 'do something with the file here next 'now go through the directories and get files from subdirectories dim sdi as directoryinfo for each sdi in di.getDirectories() Application.DoEvents() searchfiles(sdi,filetype) next catch exception as ex end catch end sub this will get you through all the directories in a computer. hope this works for you

              Vilsad P P MCTS (Windows Applications) .Net 2.0

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups