![](/img/trans.png)
[英]C# How do I use Directory.GetFiles() to get files that have the same order as in Windows explorer?
[英]How do I use RichTextBox to open Window Explorer with get directory path, db?
Access 2003和VS 2010 C#
更新此软件是为Windows XP构建的
我担心的是更新7-谢谢
我创建了一个方法,用户可以在其中将路径目录的命令参数插入Richtextbox中并将其保存在数据库中。 我想创建一个超链接,用户可以在其中单击richTextBox中的链接,从外部打开Windows资源管理器以找到文件所在的位置。 我不知道什么是所谓的描述我已经给了你,但最近的什么我期待的是在这里 ,并在这里 ,但我不太知道这是什么,我期待? 如果有人能帮助我,我将不胜感激,在此先感谢。
这是我的btnOpen_Click方法...
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);
}
这是我的插入方法...
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();
}
文件是Access 2003中保存目录的数据字段。 btw文件数据字段的数据类型为超链接。
此网站的Update 2第2次尝试, 此处符合但未打开Windows资源管理器
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
string FilePath = @"C:\myFolder\myFolder";
System.Diagnostics.Process.Start("Explorer.exe", @"/select,""" + FilePath + "\"");
}
更新3第3次尝试这是我的尝试:目的是当我单击一个路径时,在Richtextbox中,Windows资源管理器应打开它没有打开的窗口。 实际上什么也没有发生,也没有错误
我确实要指出,可以使用insert命令参数将文件保存在各个文件夹中,但驱动器相同。 例如
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 *-如果我将richTextBox1_LinkClicked代码放在richTextBox1_TextChanged中,则当我使用导航按钮时,它将自动打开Windows资源管理器。
所以我的问题是,当路径目录保存在Access 2003数据库中时,当我在richTextBox中选择路径目录时,如何使用richtextbox打开Windows资源管理器?
当我使用我不想这样做的导航按钮时,带有此“我的文档”的更新4将打开。
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);
}
更新5
这样就可以了,但是和上面一样,它将使用导航按钮自动打开路径目录。 我不希望那样发生。 我想单击richtextbox中的链接以打开Windows资源管理器。 我正在家里使用代码,并在此处学习导航。 richTextBox1_LinkClicked事件不起作用。
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);
}
更新6使用导航按钮时打开Windows资源管理器的原因是因为richTextBox1_TextChanged中的这段代码。 如果我在richTextBox1_MouseClick方法中使用该段代码,则资源管理器将打开。
更新7我在家里和学习网站上使用导航按钮。 我创建了一个Richtextbox,用于打开默认的窗口资源管理器路径目录。 我试图通过使用id为etc的选择查询来进一步扩展,因此可以在外部打开Windows资源管理器时根据表ID打开不同的路径目录吗?
例如,有两个doc文件,即Test.doc和Test.doc ...
ID = 1的路径目录为= myFolder \\ myFolder \\ Test.doc
ID = 2的路径目录为= myFolder \\ myFolder2 \\ Test2.doc
是否可以做这样的事情。
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();
}
提前致谢
您已接近要在Update 7上获得的功能 。
这是更正后的功能:
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...
}
}
}
重要的位
cmd.CommandText
是SQL查询命令。 cmd.ExecuteNonQuery()
将仅执行SQL查询,但完全放弃所有结果。
cmd.ExecuteScalar()
返回第一行的第一列只 (在你的情况,你只关心的第一行和第一列)。 更新
在我的示例中,使用using
语句; 它将负责清理OleDbCommand
对象(尤其是在发生异常的情况下)。
使用健全性检查(如果语句,空检查等):我已经更新了上面的答案以反映这一点。
您希望仅通过检索第一条记录(使用TOP 1
)来提高性能(尽管过早)。
我想我明白了您的要求,这是如何使用选定的特定文件启动Explorer的方法。 我认为您不能使用URI来完成此操作,因此您必须启动一个Explorer进程。 还有其他几个问题,这些问题会为您提供帮助。
但是,摘要是您需要启动“ explorer.exe / select,完整路径到文件”
为了使用LinkClicked()
处理程序,富文本框中的链接必须是格式正确的超链接,例如
file://C:/Windows
并且富文本框控件必须将其.DetectUrls
属性设置为True
。 如果是这种情况,则富文本格式控件框中的链接应看起来像一个Web链接(通常为蓝色并带有下划线),并且您可以通过单击以下代码来打开“浏览器”窗口。
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.