private static ProjectId DetermineProject(FwAppArgs args)
{
// Get project information from one of four places, in this order of preference:
// 1. Command-line arguments
// 2. Sample DB (if this is the first time this app has been run)
// 3. Registry (if last startup was successful)
// 4. Ask the user
//
// Except that with the new Welcome dialog, 2 through 4 are lumped into the Welcome dialog
// functionality. If the user checks the "...always open the last edited project..." checkbox,
// we will try to do that and only show the dialog if we fail.
// If we try to use command-line arguments and it fails, we will use the Welcome dialog
// to help the user figure out what to do next.
var projId = new ProjectId(args.DatabaseType, args.Database, args.Server);
StartupException projectOpenError;
if (TryCommandLineOption(projId, out projectOpenError))
return projId;
// If this app hasn't been run before, ask user about opening sample DB.
var app = GetOrCreateApplication(args);
if (app.RegistrySettings.FirstTimeAppHasBeenRun)
return ShowWelcomeDialog(args, app, null, projectOpenError);
// Valid project information was not passed on the command-line, so try looking in
// the registry for the last-run project.
var previousStartupStatus = GetPreviousStartupStatus(app);
var latestProject = app.RegistrySettings.LatestProject;
if ((String.IsNullOrEmpty(projId.Name) || projectOpenError != null) &&
previousStartupStatus != StartupStatus.Failed && !String.IsNullOrEmpty(latestProject))
{
// User didn't specify a project or gave bad command-line args,
// so set projId to the last successfully opened project.
projId = GetBestGuessProjectId(latestProject, app.RegistrySettings.LatestServer);
}
else if (previousStartupStatus == StartupStatus.Failed && !string.IsNullOrEmpty(latestProject))
{
// The previous project failed to open, so notify the user.
projectOpenError = new StartupException(String.Format(
Properties.Resources.kstidUnableToOpenLastProject, app.ApplicationName,
latestProject));
}
var fOpenLastEditedProject = GetAutoOpenRegistrySetting(app);
if (fOpenLastEditedProject && projId.IsValid && projectOpenError == null
&& previousStartupStatus == StartupStatus.Successful)
return projId;
// No valid command line args, not the first time we've run the program,
// and we aren't set to auto-open the last project, so give user options to open/create a project.
return ShowWelcomeDialog(args, app, projId, projectOpenError);
}