您的位置:首页 > 编程语言 > C#

C# OpenFileDialog设置默认打开的文件路径

2017-05-21 11:51 1526 查看
这个功能我的使用场景是这个样子的:tool运行的时候保存了几种log,查看不同的问题需要打开不同的log,为不同log放在了不同的文件夹下面。

好了,直接上代码了:

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

namespace openlog
{
public partial class Form1 : Form
{
private string logPath1, logPath2;
public Form1()
{
InitializeComponent();
FileStream fs;
logPath1 = System.Environment.CurrentDirectory.ToString()/*获取软件运行时的路径*/ + @"\log\log1\";//如果需要设置特定的路径logPath1=@"C:\Program Files\";
logPath2 = System.Environment.CurrentDirectory.ToString() + @"\log\log2\";
if (!Directory.Exists(logPath1))
{
Directory.CreateDirectory(logPath1);
using (fs = new FileStream(logPath1 + "log1.txt", FileMode.CreateNew)) { }//创建相对于软件运行目录的路径以及文件名
}
if (!Directory.Exists(logPath2))
{
Directory.CreateDirectory(logPath2);
using (fs = new FileStream(logPath2 + "log2.txt", FileMode.CreateNew)) { }
}
}

private void button1_Click(object sender, EventArgs e)

4000
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = logPath1;//设置打开路径的目录
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("你打开的文件路径为" + openFileDialog1.FileName.ToString());
}
}

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.InitialDirectory = logPath2;
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("你打开的文件路径为" + openFileDialog2.FileName.ToString());
}
}
}
}


源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐