How do I use RichTextBox to open Window Explorer with get directory path, db?

advertisements

Access 2003 & VS 2010 C#

Update this piece of software is built for Windows XP

My concern is in update 7 - thanks

I have created a method where user can insert command parameter for a path directory in a richtextbox and save it in the database. I would like to create a hyperlink where a user can click on the link, in the richTextBox, opening Windows Explorer externally locating where the file is. I don't know what it is called on the description I have given you but the nearest to what I am looking for is here, and herebut I am not quite sure if that is what I am looking? If anyone could help me I would be most grateful, thanks in advance.

This is my btnOpen_Click method...

OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.InitialDirectory = "C:\\";
        openFileDialog1.Filter = "Word 97-2003 Document(*.doc)|*.doc|All files(*.*)|*.*";

        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.RestoreDirectory = true;
            richTextBox1.Text = Path.GetDirectoryName(openFileDialog1.FileName);
        }

        try
        {
            string filePath;
            filePath = Path.GetDirectoryName(openFileDialog1.FileName); //Path.GetDirectoryName(openFileDialog1.FileName) openFileDialog1.FileName
            richTextBox1.Text = filePath;
        }

        catch (Exception ex)
        {
            MessageBox.Show("Error: : " + ex.Message);
        }

this is my insert method...

    private void btnInsert_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO Table1 File) Values(@File)
        cmd.Parameters.AddWithValue("@File", richTextBox1.Text);
        cmd.Connection = myCon;
        myCon.Open();
        cmd.ExecuteNonQuery();
        myCon.Close();
   }

File is the data field in Access 2003 where the directory is saved. btw File data field has data type of Hyperlink.

Update 2 2nd attempt from this website, here complies but does not open Windows Explorer

 private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
    {
        string FilePath = @"C:\myFolder\myFolder";
        System.Diagnostics.Process.Start("Explorer.exe", @"/select,""" + FilePath + "\"");
     }

Update 3 3rd attempt Here my attempt: The purpose is when I click on a path, in the richtextbox, windows explorer should open which it doesn't. Actually nothing is happening and there is no error

I do want to point out files could be saved in various folders using the insert command parameter but same drive. So for example..

C:\Myfolder\Myfolder1

C:\Myfolder\Myfolder2

    private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
    {
       string FilePath = @"C:\\myFolder\\myFolder";
       System.Diagnostics.Process.Start("Explorer.exe", @"/select," + FilePath + e.LinkText);
       // 1*
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler
        (richTextBox1_LinkClicked);

    }

// 1* - If I place the richTextBox1_LinkClicked code in the richTextBox1_TextChanged It will open Windows explorer automatically when I use the navigation button.

So my question how do I use the richtextbox to open Windows Explorer when I select a path directory in richTextBox when path directory is saved in Access 2003 database?

Update 4 I with this My Document opens when I use the navigation button which I don't want to do that..

private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        string FilePath = @"C\\Windows";
        System.Diagnostics.ProcessStartInfo exploreTest = new System.Diagnostics.ProcessStartInfo();
        exploreTest.FileName = "Explorer.exe";
        exploreTest.Arguments = FilePath;
        System.Diagnostics.Process.Start(exploreTest);
    }

    private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e, string path)
    {
       System.Diagnostics.Process.Start(e.LinkText);
    }


Update 5

With this it will work but as above it will automatically open the path directory with navigation button. I don't want that to happen. I want click on the link in the richtextbox to open the Windows Explorer. I am using code from home and learn for navigation, here. The richTextBox1_LinkClicked event does not work.

 private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        string FilePath = @"C:\MyFolder\myFolder";
        System.Diagnostics.Process.Start("Explorer.exe", @"/select,""" + FilePath + "\"");
    }

   private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.LinkText);

    }

Update 6 The reason Windows Explorer was opening when using navigation button is because the piece of code in the richTextBox1_TextChanged. If I use that piece of code in richTextBox1_MouseClick method then the Explorer will open.


Update 7 I am using navigation buttons, from home and learn website. I have created a richtextbox to open default window explorer path directory. I trying to further extend by using select query where id is etc, so is possible to open different path directory based on table ID when Windows Explorer is opened externally?

For example there two doc files, Test.doc and Test.doc...

ID = 1 has pathdirectory like = myFolder\myFolder\Test.doc

ID = 2 has pathdirectory like = myFolder\myFolder2\Test2.doc

Is it possible to do something like this..

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
  OleDbCommand cmd = new OleDbCommand();
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = @"SELECT File FROM Table1 WHERE ID = @ID";
  cmd.Parameters.AddWithValue("@ID", txtID.Text);
  string FilePath = cmd.CommandText;
  // FilePath = @"C:\\myFolder\myFolder";
  System.Diagnostics.Process.Start("Explorer.exe", @"/select," + FilePath);
  cmd.Connection = myCon;
  myCon.Open();
  cmd.ExecuteNonQuery();
  myCon.Close();

}

Thanks in advance


You are close to what you want on Update 7.

Here's the corrected function:

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
    Object returnValue;

    using (OleDbCommand cmd = new OleDbCommand())
    {
        cmd.Connection = myCon;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = @"SELECT TOP 1 File FROM Table1 WHERE ID = @ID";
        cmd.Parameters.AddWithValue("@ID", txtID.Text);

        myCon.Open();
        returnValue = cmd.ExecuteScalar();
        myCon.Close();
    }

    if ((returnValue == null) || (returnValue == DBNull.Value))
    {
        // null was returned, meaning the ID wasn't found, or the "File" field has no value
        // handle the error appropriately...
    }
    else
    {
        // FilePath = @"C:\\myFolder\myFolder";
        String FilePath = returnValue.ToString();

        if (Directory.Exists(FilePath))
        {
            System.Diagnostics.Process.Start("Explorer.exe", @"/select," + FilePath);
        }
        else
        {
            // FilePath doesn't exist!
            // handle the error appropriately...
        }
    }
}


The important bits

  • cmd.CommandText is the SQL query command.
  • cmd.ExecuteNonQuery() will simply execute the SQL query, but completely discard any results.
    • What you actually want to use is cmd.ExecuteScalar() which returns the first column of the first row only (in your case you only care about the first row and the first column).

Update

  1. As used in my example, use the using statement; it'll take care of cleaning up the OleDbCommand object (especially in case of an exception).

  2. Use sanity checks (if statements, null checks, etc.): I've updated the answer above to reflect this.

  3. You want to improve performance (albeit, prematurely) by only retrieving the first record (by using TOP 1).