获取文件夹路径的方法(弹窗)
System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
//以下是选择文件夹路径的方法
if (folder.ShowDialog() == DialogResult.OK)
{
textBox2.Text = folder.SelectedPath;
}
获取文件路径的方法(弹窗)
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = dialog.FileName;//获得文件路径
}
C#获取文件路径
string str1 =Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。
string str2=Environment.CurrentDirectory;//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
//备注按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:mySubDirectory”)。
string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
//获取当前进程的完整路径,包含文件名(进程名)。
string str = this.GetType().Assembly.Location;
result: X:xxxxxxxxx.exe (.exe文件所在的目录+.exe文件名)
//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:xxxxxxxxx.exe (.exe文件所在的目录+.exe文件名)
//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str = System.Environment.CurrentDirectory;
result: X:xxxxxx (.exe文件所在的目录)
//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:xxxxxx (.exe文件所在的目录+”")
//获取和设置包含该应用程序的目录的名称。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:xxxxxx (.exe文件所在的目录+”")
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:xxxxxx (.exe文件所在的目录)
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:xxxxxxxxx.exe (.exe文件所在的目录+.exe文件名)
//获取应用程序的当前工作目录(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
result: X:xxxxxx (.exe文件所在的目录)
对已知的文件路径进行处理
String[] files = System.IO.Directory.GetFiles(path) //返回指定目录下的文件名
string str = System.IO.Path.GetFileNameWithoutExtension(path);//返回不具有扩展名的指定路径字符串的文件名
C#-System.IO.Path 用法解析
举例
定义 string filePath =@”E:/Randy0528/中文目录/JustTest.rar”;
方法 说明 效果
System.IO.Path.ChangeExtension(filePath, “txt”) 更改路径字符串的扩展名 E:/Randy0528/中文目录/JustTest.txt
System.IO.Path.GetDirectoryName(filePath) 返回指定路径字符串的目录信息 E:/Randy0528/中文目录
System.IO.Path.GetExtension(filePath) 返回指定的路径字符串的扩展名 .rar
System.IO.Path.GetFileName(filePath) 返回指定路径字符串的文件名和扩展名 JustTest.rar
System.IO.Path.GetFileNameWithoutExtension(filePath) 返回不具有扩展名的指定路径字符串的文件名 JustTest
System.IO.Path.GetPathRoot(filePath) 获取指定路径的根目录信息 E:/
System.IO.Path.GetRandomFileName() 返回随机文件夹名或文件名 ct2h5b2h.sed
System.IO.Path.GetTempFileName() 创建唯一命名的零字节的临时文件并返回其绝对路径 C:/Documents and Settings/Randy/Local Settings/Temp/tmpAD.tmp
System.IO.Path.GetTempPath() 返回当前系统的临时文件夹的路径 C:/Documents and Settings/Randy/Local Settings/Temp/
System.IO.Path.HasExtension(filePath) 返回路径是否包括文件扩展名 Ture
System.IO.Path.IsPathRooted(filePath) 返回路径是绝对路径还是相对路径 Ture
System.IO.Path.Combine(@ “c: “, “b.txt “) 返回合并后的路径 C:/b.txt
System.IO.Path.GetFullPath(“JustTest.rar”) 查找文件并返回其绝对路径 E:/Randy0528/中文目录/JustTest.rar