//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
Application.StartupPath
返回上一级路径 ..\..\ 是你返回的文件夹级数
DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", Application.StartupPath))
string ParentPath = di.FullName;
//判断文件是否存在
if (System.IO.File.Exists(ParentPath )
{
//文件路径不存在!
Directory.CreateDirectory(PathTXTString); //创建目录
}
Directory.Exists(path); // path表示路径参数;可判断文件路径是否存在
Directory.CreateDirectory(path);// path表示路径参数;可创建目录
File.Exists(path); // path表示路径参数;可判断文件是否存在
string path = @"C:\Program Files\Microsoft.NET";
if (!Directory.Exists(path))
{
//Console.WriteLine("文件路径不存在!");
Directory.CreateDirectory(path); //创建目录
}
文件选择,文件夹选择
// 选择文件:
private string SelectFile()
{
string path = string.Empty;
var openFileDialog = new OpenFileDialog()
{
Filter = "Files (*.*)|*.*"//如果需要筛选txt文件("Files (*.txt)|*.txt")
};
var result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
path = openFileDialog.FileName;
}
return path;
}
// 选择路径
private string SelectPath()
{
string path = string.Empty;
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
fbd.SelectedPath = @"D:\"; //设置提示框起始路径,当路径不存在时,报错
fbd.Description = "选择TXT保存路径";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = fbd.SelectedPath;
}
return path;
}