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. Other Discussions
  3. The Weird and The Wonderful
  4. The lazy way to check if the file is present.

The lazy way to check if the file is present.

Scheduled Pinned Locked Moved The Weird and The Wonderful
5 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.
  • A Offline
    A Offline
    Ammar_Ahmad
    wrote on last edited by
    #1
        Try
            Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\\" & TextBox3.Text & "\_OAuthtk.dat")
            My.Settings.m\_strToken = objReader.ReadLine()
            My.Settings.Save()
    
            objReader.Close()
    
            Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\\" & TextBox3.Text & "\_OAuthtkST.dat")
            My.Settings.m\_strTokenSecret = objReader1.ReadLine()
            My.Settings.Save()
    
            objReader1.Close()
    
    
            Me.Hide()
            Form1.Show()
    
    
    
        Catch ex As Exception
            Me.Hide()
            Form3.Show()
        End Try
    

    I used that code in my application called TRocket to check if the files are present or not :P *The lazy way to check/read.

    P 1 Reply Last reply
    0
    • A Ammar_Ahmad
          Try
              Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\\" & TextBox3.Text & "\_OAuthtk.dat")
              My.Settings.m\_strToken = objReader.ReadLine()
              My.Settings.Save()
      
              objReader.Close()
      
              Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\\" & TextBox3.Text & "\_OAuthtkST.dat")
              My.Settings.m\_strTokenSecret = objReader1.ReadLine()
              My.Settings.Save()
      
              objReader1.Close()
      
      
              Me.Hide()
              Form1.Show()
      
      
      
          Catch ex As Exception
              Me.Hide()
              Form3.Show()
          End Try
      

      I used that code in my application called TRocket to check if the files are present or not :P *The lazy way to check/read.

      P Offline
      P Offline
      Pascal Ganaye
      wrote on last edited by
      #2

      You call it lazy I call it simple. You could check for the file existence

      if (File.Exist(...)) {...}

      But what if the file exist but is not readable? I think your code is fine. I also often hear you should not catch all. In some other languages, the compiler know what exception is raised by a block of code. Catching the required exception in that scenario is easy. In C#, it is harder. If you catch the exceptions you can guess, you might miss the rare exceptions like disk disconnected, out of memory or god knows. I am not a programmer that put program elegance or programmer state of mind before the end-user satisfaction. I think catching all is safer. In one word, I think your code if FINE.

      L P 2 Replies Last reply
      0
      • P Pascal Ganaye

        You call it lazy I call it simple. You could check for the file existence

        if (File.Exist(...)) {...}

        But what if the file exist but is not readable? I think your code is fine. I also often hear you should not catch all. In some other languages, the compiler know what exception is raised by a block of code. Catching the required exception in that scenario is easy. In C#, it is harder. If you catch the exceptions you can guess, you might miss the rare exceptions like disk disconnected, out of memory or god knows. I am not a programmer that put program elegance or programmer state of mind before the end-user satisfaction. I think catching all is safer. In one word, I think your code if FINE.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        +5

        Bastard Programmer from Hell :suss:

        1 Reply Last reply
        0
        • P Pascal Ganaye

          You call it lazy I call it simple. You could check for the file existence

          if (File.Exist(...)) {...}

          But what if the file exist but is not readable? I think your code is fine. I also often hear you should not catch all. In some other languages, the compiler know what exception is raised by a block of code. Catching the required exception in that scenario is easy. In C#, it is harder. If you catch the exceptions you can guess, you might miss the rare exceptions like disk disconnected, out of memory or god knows. I am not a programmer that put program elegance or programmer state of mind before the end-user satisfaction. I think catching all is safer. In one word, I think your code if FINE.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          On the other hand, checking whether or not a file exists and is readable before you need to access it may not be worth the trouble because it may not exist or be readable later when you do need to access it. Generally, I prefer to just access it when I need to and deal problems then.

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            On the other hand, checking whether or not a file exists and is readable before you need to access it may not be worth the trouble because it may not exist or be readable later when you do need to access it. Generally, I prefer to just access it when I need to and deal problems then.

            P Offline
            P Offline
            Pascal Ganaye
            wrote on last edited by
            #5

            Perhaps I was not clear. You summarized my exact thoughts

            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