views:

265

answers:

5

I'm making a Library type app which needs to scan the whole computer when it is run for the first time. Not again ever. How can I accomplish it?

I'll be using SQL database to store data. So, I can easily make a table there and store a flag and check it on first run, but is there any other way? Any native support for this in VB.NET?

+1  A: 

EDIT: If you're looking for a way to determine, using some native feature of VB.NET, whether a method has ever been run before (as in, last year), I think you're out of luck. That said, a quick-and-dirty approach might be to define a method to query the database for the flag you've referred to and store the result of that method in a static flag.

Public Sub MethodToRunOnlyOnce()
    ' this flag will maintain its value between method calls '
    ' in the same session '
    Static methodAlreadyRun As Boolean = MethodHasBeenRun()

    If methodAlreadyRun Then
        Exit Sub
    End If

    Try
        ' ... code ... '
    Finally
        MethodToSetDatabaseFlag()
        methodAlreadyRun = True
    End Try
End Sub

Private Sub MethodToSetDatabaseFlag()
    ' code here to set Db flag '
End Sub

Private Function MethodHasBeenRun() As Boolean
    ' code here to check Db flag '
End Function
Dan Tao
In the application lifetime, not in the session lifetime
Eduardo Molteni
@Eduardo: Noted.
Dan Tao
You ever tried the answer by `Anax`? Am gonna try it, but have queries regarding it.
Bibhas
+1  A: 

Using your SQL database seems like a viable solution. You can also write a flag to a settings file on the file system if you don't want to use a table to hold it. This is a pretty good site with some examples on how to write to a file.

http://www.freevbcode.com/ShowCode.asp?ID=4492

Aaron
Okay, Can you tell me, whats the difference between saving in a normal `*.txt` file and a `*.ini` file?
Bibhas
@Bibhas - There should be no difference. `.ini` files were used for application initialization back in the hey-days but not anymore.
Sathya
@Sathya - Okay. Got it. will try it. BTW, what about those `*.cfg` files? they seem more structured than others.
Bibhas
@Bibhas - No different from text or ini files. It depends on the developer. VB.net ( and C#net ) can make use of Application Settings using XML files - and they provide builtins to read from the settings file - http://msdn.microsoft.com/en-us/library/a65txexh%28VS.80%29.aspx
Sathya
A: 

You could always store this flag in an xml file or in the registry so it's on the PC. If you store it in a database and there are multiple copies of this program running on different PCs, you would have to identify them in the DB somehow, whereas if you keep track of it locally you don't need to worry about it.

smoore
Yeah, initially, I thought of this too.
Bibhas
+1  A: 

Use either

Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun

or

My.Application.Deployment.IsFirstRun

Edit:

Check this article for additional info.

Anax
Nice. But how would it determine if its the first run? I mean, I need to test the app, right? so, I'll get only one shot to test it? Will it reset if I re-publish?
Bibhas
If My.Application.Deployment.IsFirstRun then DoSomeMagic(). It's as simple as that. But as far as I know, yes, if you republish it will reset. Other than that, you can use the registry (mind user priviledges) or just write an empty file in the application directory. But I would consider using a Database for this an exagerated solution.
Anax
Hmm. Thanx for the helps. :)
Bibhas
+1  A: 

I found the best way to be to use appsettings..

Bibhas