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()); } } } }
源码下载
相关文章推荐
- 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
- Silverlight 5 RC新特性探索系列:11.SaveFileDialog保存文件指定默认名称---OpenFileDialog打开文件指定默认路径
- c# 疑难(一)之打开“OpenFileDialog”文本对话框后默认路径改变问题和解决方法
- C#中打开OpenFileDialog会改变默认路径的问题
- C#中OpenFileDialog获取文件名和文件路径的常用方法
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- C# 关于使用OpenFileDialog 默认路径被重置的问题。
- C# OpenFileDialog打开文件对话框(详解)
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- Silverlight 5 RC新特性:11.SaveFileDialog保存指定默认名称-OpenFileDialog打开指定默认路径
- C#用openfiledialog文件和savefileDialog打开和保存文件
- C#用openfiledialog文件和savefileDialog打开和保存文件
- C#中OpenFileDialog获取文件名和文件路径的常用方法.
- C# OpenFileDialog 打开/选择文件对话框
- C# 打开文件对话框 OpenFileDialog
- C# FolderBrowserDialog和OpenFileDialog 使用 指定目录和指定文件
- 如何用c#打开文件夹并选择文件夹内的一个文件 open folder and select file
- c#设置OpenFileDialog的Filter属性
- 打开ASP.NET Web项目时,此项目的默认Web访问模式设置为文件共享, 但是无法从路径“...”打开“...”处的项目文件夹。返回的错误是: 无法打开Web项目“”。返回的错误是: 无法打开Web项目“...”。文件路径“...”怀URL“...”不
- C# openfiledialog设置filter属性后达不到过滤效果的原因之一