The lazy way to check if the file is present.
-
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.
-
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.
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.
-
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.
-
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.
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.
-
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.
Perhaps I was not clear. You summarized my exact thoughts