vendredi 3 juillet 2015

do stuff after file(db) has been created

I want my app to do stuff only after an sqlite database has been created. Placing and eventlistener on sqliteCon.Close.

I'm using Filesystemwatcher to listen if the file is created, but Filesystemwatcher gives already the command when the filename is created in the folder and not when the db is filled with tables as well. How can make that FileSystemWatcher gives a message after the db closes or is there another way to make this work?

 private void CreateNewFile()
    {
        FileCreated();

        var dir = @"C:\Users\Projects\";
        SQLiteConnection.CreateFile(Path.Combine(dir, ""+ this.ProjectName +""+".sqlite"));

        String dbConnectionString = @"Data Source =C:\Users\Filip Vandewal\Documents\MoviePrepper1.0\MoviePrepper\Projects\" + this.ProjectName + "" + ".sqlite";
        SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
        sqliteCon.Open();

        SQLiteCommand Command = new SQLiteCommand(sqliteCon);

        Command.CommandText = "CREATE TABLE projects (ProjectID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , projectname VARCHAR)";
        Command.ExecuteNonQuery();

        Command.CommandText = "INSERT INTO projects (ProjectID, projectname) VALUES (NULL,'" + this.ProjectName + "')";
        Command.ExecuteNonQuery();

        sqliteCon.Close();
 }

 public void FileCreated()
    {
        string filePath = @"C:\Users\Projects\" + this.ProjectName + "" + ".sqlite";
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = Path.GetDirectoryName(filePath);
        watcher.Filter = Path.GetFileName(filePath);
        watcher.Created += new FileSystemEventHandler(OpenProjectsView);
        watcher.EnableRaisingEvents = true;
    }

    private void OpenProjectsView(object sender, FileSystemEventArgs e)
    {
        Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "CloseProjectsView"));
        Messenger.Default.Send<NotificationMessage>( new NotificationMessage(this, "OpenWorkSpace"));
    }

Aucun commentaire:

Enregistrer un commentaire