1

I want history links for Google Chrome and when i search, i found 'History' file of Google Chrome. But i can't read 'History' file because Google Chrome use this 'History' file. Therefore firstly i copy 'History' file and i read 'History' file that i copied.It worked but only once. Because when the second time i want history links, I get "File used by another Process" error(For it can't delete).

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear(); 

    string userName = Environment.UserName;

    string confilename = string.Format(@"Data Source=C:\Users\{0}\Desktop\History1827", userName);

    string oldfilename = string.Format(@"C:\Users\{0}\AppData\Local\Google\Chrome\User Data\Default\History", userName);

    string newfilename = string.Format(@"C:\Users\{0}\Desktop\History1827", userName);

    File.Copy(oldfilename, newfilename); // I copy oldfile to newfile destination

    using (SQLiteConnection connection = new SQLiteConnection(confilename))
    {
        connection.Open();
        SQLiteCommand command1 = new SQLiteCommand();
        command1.Connection = connection;
        command1.CommandText = "Select * From urls";
        SQLiteDataReader dr = command1.ExecuteReader();
        string historylinks = "";
        while (dr.Read())
        {
            // dr[0] = ID of Link , dr[1] = Link , dr[2] = Title of Link
            historylinks = dr[0].ToString() + "\t--->" + dr[1].ToString(); // I get ID and Link from History file that i copied

            listBox1.Items.Add(historylinks);
        }

        dr.Close();
        connection.Close();


    }
    if (File.Exists(newfilename))
    {
        File.Delete(newfilename);
        MessageBox.Show("Deleted");
    }

}

Error : File.Delete(newfilename);

What did i do after that when i see this error?

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear(); 

    string userName = Environment.UserName;

    string confilename = string.Format(@"Data Source=C:\Users\{0}\Desktop\History1827", userName);

    string oldfilename = string.Format(@"C:\Users\{0}\AppData\Local\Google\Chrome\User Data\Default\History", userName);

    string newfilename = string.Format(@"C:\Users\{0}\Desktop\History1827", userName);

    File.Copy(oldfilename, newfilename); // I copy oldfile to newfile destination
 //------ I DID NOT USE SQLİTECONNECTİON -----
    //using (SQLiteConnection connection = new SQLiteConnection(confilename))
    //{
    //    connection.Open();
    //    SQLiteCommand command1 = new SQLiteCommand();
    //    command1.Connection = connection;
    //    command1.CommandText = "Select * From urls";
    //    SQLiteDataReader dr = command1.ExecuteReader();
    //    string historylinks = "";
    //    while (dr.Read())
    //    {
    //        // dr[0] = ID of Link , dr[1] = Link , dr[2] = Title of Link
    //        historylinks = dr[0].ToString() + "\t--->" + dr[1].ToString(); // I get ID and Link from History file that i copied

    //        listBox1.Items.Add(historylinks);
    //    }

    //    dr.Close();
    //    connection.Close();


    //}
    if (File.Exists(newfilename))
    {
        File.Delete(newfilename);
        MessageBox.Show("Deleted");
    }

}

It worked. So, if i don't use SQLiteConnection, Program can delete 'History' file that it copied .

Who use this 'History' file that was copied? How can i fix ?

Thank you.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • Did your program hit an error while you were reading from the copy the first time? – Jaskier Dec 13 '18 at 16:15
  • @Symon No, it was worked – Yusufhaystra Dec 13 '18 at 16:16
  • Why do you assume the user's profile is stored in *C:*? Never use such code. If you want to get access to special folders use [Environment.GetFolderPath](https://learn.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=netframework-4.7.2) – Panagiotis Kanavos Dec 13 '18 at 16:32
  • you can try [`process explorer`](https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer) or [`Handle`](https://learn.microsoft.com/en-us/sysinternals/downloads/handle) tool to find which application is using the file – Abdo Dec 13 '18 at 16:33
  • How do you read that file? Did you close the stream or streamreader you used to read it? If you didn't, the process that locks the file is your own – Panagiotis Kanavos Dec 13 '18 at 16:35
  • Wait, are you treating the history file as a SQLite database? SQLite doesn't shut down just because you closed the connection. – Panagiotis Kanavos Dec 13 '18 at 16:38
  • @PanagiotisKanavos Format of this 'History' file can be read. When just one time i tried , it was worked. Only, I use File.Copy from FileStream and this is void. I can't open or close 'File.Copy'. Thank you for answer – Yusufhaystra Dec 13 '18 at 16:40
  • @PanagiotisKanavos How can i shut down SQLite ? – Yusufhaystra Dec 13 '18 at 16:43
  • First of all, which provider are you using? There are many and all use `SQLite` as a prefix. Second, you are using the path itself as a connection string. A proper connection string can specify features like connection pooling, read-only operation, etc. You could try opening the database as read-only. [This page](https://www.connectionstrings.com/sqlite/) shows common SQLite connection string options – Panagiotis Kanavos Dec 13 '18 at 16:46

1 Answers1

-1

You can try Process Explorer to find which application opened the file handle. If Process Explorer cannot find that, use Process Monitor to trace which process is trying to access the file.

Hej.Ag
  • 39
  • 5