3

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

Community
  • 1
  • 1
bucketblast
  • 437
  • 4
  • 14
  • 37
  • can you be more specific as to which line is throwing the error..? have you stepped through the code..? should this line also be referencing `runatscript` `var link = e.LinkText.Replace("%20", " ");`? – MethodMan Apr 01 '13 at 16:31
  • actually DatosDeEjecucion is not being recognised? to be honest I don't what DatosDeEjecucion is? I am using the code from the link I have given at the top. Kiquenet has used variables but I don't where he/she put it. I will update at the top. – bucketblast Apr 01 '13 at 16:38
  • if you don't know what it is then I would suggest you doing a google search or a MSDN search on `DatosDeEjecucion.PathAbsScript;` sounds like you need to add a `reference` – MethodMan Apr 01 '13 at 16:41
  • OK Thanks DJ Kraze I will try again – bucketblast Apr 01 '13 at 16:42
  • Please can someone update with their comment as to way they have downgrade? – bucketblast Apr 01 '13 at 18:00
  • FWIW, Google Translate says that "Datos De Ejecucion" in Spanish translates to "Performance Data" in English, so maybe it has something to do with the `PerformanceData` class described [here](http://msdn.microsoft.com/en-us/library/ms817972.aspx). – Gord Thompson Apr 01 '13 at 18:24
  • @GordThompson - I don't think I need to use PerformanceData class but perhaps shell command or do something else in richTextBox1_LinkClicked – bucketblast Apr 02 '13 at 15:55

3 Answers3

2

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).

Jesse
  • 8,605
  • 7
  • 47
  • 57
  • I knew I had to do something on cmd to convert it into string but I wasn't sure how to. More importantly could you tell me more about the errors and exceptions? Can it handle about 100 records of doc files? – bucketblast Apr 04 '13 at 20:20
  • Ok, before I update my answer - is this intended to open a single file at a time, or as many files as are stored under the same `ID`? – Jesse Apr 04 '13 at 20:21
  • If existing Windows Exlorer is opened and I moved to another ID will there be any problems if I open that path directoy? – bucketblast Apr 04 '13 at 20:26
  • Is it possible if you can check why hyperlink is not show? I have.. System.Diagnostics.Process.Start(e.LinkText); – bucketblast Apr 04 '13 at 20:31
  • Is it possible if you can check out why hyperlink is not showing? I have.. System.Diagnostics.Process.Start in the richTextBox1_LinkClicked. Windows Exlorer opens more than once so that is great. – bucketblast Apr 04 '13 at 20:37
  • @bucketblast, join [this Stack Overflow chat](http://chat.stackoverflow.com/rooms/27588/how-do-i-use-richtextbox-to-open-window-explorer-with-get-directory-path-db) and we can discuss this there. – Jesse Apr 04 '13 at 20:41
0

I think I get what you're asking, and it's how to start Explorer with a particular file selected. I don't think you can do it with a URI, so you'll have to start an Explorer process instead. There are a couple other questions that received answers which should help you.

Opening a folder in explorer and selecting a file

Opening an explorer window with designated file selected

Implement "Open Containing Folder" and highlight file

The summary, though, is that you need to start "explorer.exe /select,full-path-to-file"

Community
  • 1
  • 1
djdanlib
  • 21,449
  • 1
  • 20
  • 29
  • From the links you have given Windows Explorer does open only with navigation buttons. So thanks for answering this part of the question, most appericated – bucketblast Apr 03 '13 at 13:17
  • Hope it helps. At the time of my answer though, you had not included the idea of hiding the navigation buttons. I assume you mean the toolbar. AFAIK, you can't do that and might need to use a custom control to get the effect you're trying to achieve. Go check out the Explorer command-line switches KB page. http://support.microsoft.com/kb/130510 – djdanlib Apr 03 '13 at 17:24
  • I think I might need to code select command parameter in the richtextbox MouseClick event hander with the System.Diagnostics.Process.Start etc. Will update once I found the answer. – bucketblast Apr 03 '13 at 18:55
0

In order to use the LinkClicked() handler the link in your rich text box must be a properly-formatted hyperlink, like

file://C:/Windows

and the rich text box control must have its .DetectUrls property set to True. If that's the case, then the link in the rich text control box should appear like a web link (normally blue and underlined), and you can open an Explorer window when you click on it just by using the following code

    private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.LinkText);
    }
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • from the advise you have given, Windows Explorer does open but with navigation buttons. But thanks for your advise and time. I am looking the new problem but I think I may need to open a new thread if I can't solve the problem. – bucketblast Apr 03 '13 at 13:20