C#winform如何根据文件名获得文件路径

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace URLsearching
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

string dir = "C:\\A\\" + textBox1.Text;
if (Directory.Exists(dir))
{
string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
}

}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}
}
}

我就写成这样子,我也不知道现在写的对不对,就算对了,我也有个问题就是想问怎么把路径输出到TextBox2中?
新手程序员,对C#完全不熟悉,但是因为马上要交给客户,求各路大神给完整代码

思路如下:
winform中两个TextBox,一个button,用户对TextBox1输入一段字符,点击button,TextBox2输出绝对路径(已可以确定输入的文件名是精确的,所以精确搜索就可以,不用模糊查找,另外就是要搜索到文件夹下的所有子文件夹)

获取文件名方法:用System.IO.Path.GetFileName和System.IO.Path.GetFileNameWithoutExtension(无扩展名)的方法获取文件路径方法://获取当前进程的完整路径,包含文件名(进程名)。 string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录)//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+”\”)//获取和设置包含该应用程序的目录的名称。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目录+”\”)//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)//获取应用程序的当前工作目录(不可靠)。
result: X:\xxx\xxx (.exe文件所在的目录)追问

您好,我是希望通过输入字符串进行对某一文件的搜索并输出路径到TextBox

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答