大家好!小弟有个问题,OpenFileDialog这个类我指定了目录但是,程序其他的地方我获取应用程序路径就是这个路径了不知道为什么? 比如我用OpenFileDialog的InitialDirectory指定了路径C:\\ ,但是我其他的窗体使用的是("文件名")获取的是应用程序的路径下,但这个文件就直接找我刚才制定的路径下了,这是为什么啊?我试过把InitialDirectory 指定Application.StartupPath但是还是不行,还是原来指定的路径 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = Application.StartupPath; ofd.RestoreDirectory = true; ofd.ShowDialog(); 真的没有修改 要不我吧代码给你拿来看看?private void BtnFileFind_Click(object sender, EventArgs e) { dt.Rows.Clear(); OpenFileDialog OpenFile = new OpenFileDialog(); OpenFile.InitialDirectory = @"C:\Program Files\锦研XT-JZ型测水系统校准软件\配置文件"; OpenFile.Filter = "ini files (*.ini)|*.ini|All files (*.*)|*.*"; if (OpenFile.ShowDialog() == DialogResult.OK) { this.textBox1.Text = OpenFile.FileName; StreamReader stRead = new StreamReader(this.textBox1.Text, System.Text.Encoding.Default); string str = ""; int count = 0; try { while (str != null) { str = stRead.ReadLine(); if (count > 6 && count < 39) { if (count < 16) { string[] Tablestr = new string[3]; Tablestr[0] = str.Substring(str.IndexOf("=") + 1, 5); Tablestr[1] = str.Substring(str.IndexOf(",") + 1, 5); Tablestr[2] = str.Substring(str.IndexOf(",") + 7, 4); DataRow newrow = dt.NewRow(); dt.Rows.Add(newrow); dt.Rows[count - 7][0] = Tablestr[0]; dt.Rows[count - 7][1] = Tablestr[1]; dt.Rows[count - 7][2] = Tablestr[2]; } else { string[] Tablestr = new string[3]; Tablestr[0] = str.Substring(str.IndexOf("=") + 1, 6); Tablestr[1] = str.Substring(str.IndexOf(",") + 1, 5); Tablestr[2] = str.Substring(str.IndexOf(",") + 7, 4); DataRow newrow = dt.NewRow(); dt.Rows.Add(newrow); dt.Rows[count - 7][0] = Tablestr[0]; dt.Rows[count - 7][1] = Tablestr[1]; dt.Rows[count - 7][2] = Tablestr[2]; } } count = count + 1; } stRead.Close(); this.cbx_OilDischarge.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { this.cbx_OilDischarge.Items.Add(dt.Rows[i][0].ToString()); } dataGridView1.RowHeadersVisible = false; dataGridView1.DataSource = dt.DefaultView; dataGridView1.ShowCellToolTips = true; dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; } catch (Exception) { OpenFile.InitialDirectory = Application.StartupPath; OpenFile.RestoreDirectory = true; stRead.Close(); MessageBox.Show("读取配置文件异常!请确认配置文件是否正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } OpenFile.InitialDirectory = Application.StartupPath; OpenFile.RestoreDirectory = true; } 一楼正解 ofd.RestoreDirectory = true; //此属性为关闭对话框是否恢复当前设置 你的代码这么给你说吧,你每次执行这个事件肯定都会先打开第一次初始化的目录(@"C:\Program Files\锦研XT-JZ型测水系统校准软件\配置文件";)因为每次先执行上面的代码.执行顺序和逻辑判断位置的问题,你好好想想 谢谢你 我找到原因了 OpenFile.RestoreDirectory = true;这个语句应该放在 if (OpenFile.ShowDialog() == DialogResult.OK) 上面 也不知道为什么,至于你说的我是在打开按钮里面做的,我关闭这个画面后,打开别的画面就出现这种情况直接找我指定的路径了,没有在关闭前指定恢复吧 关于outlookbar控件嵌套DataGridView控件的问题 用过vs2010负载测试的请进,帮我看看问题,在线等... 算法问题 有谁用Wise Installation System 9.02做过打包的,求教程或者帮助说明!(有结果另外加分) SBO SDK c#问题请教 求电子书籍 [求助]如何将C#写的客户端程序嵌入到IE浏览器下运行? 如何把richtextbox中的指定元素取出来 一个很怪的问题?! 求助!! C# 怎么来判断2张图片相似度 DLL引用问题
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Application.StartupPath;
ofd.RestoreDirectory = true;
ofd.ShowDialog();
private void BtnFileFind_Click(object sender, EventArgs e)
{
dt.Rows.Clear();
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.InitialDirectory = @"C:\Program Files\锦研XT-JZ型测水系统校准软件\配置文件";
OpenFile.Filter = "ini files (*.ini)|*.ini|All files (*.*)|*.*";
if (OpenFile.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = OpenFile.FileName;
StreamReader stRead = new StreamReader(this.textBox1.Text, System.Text.Encoding.Default);
string str = "";
int count = 0;
try
{
while (str != null)
{
str = stRead.ReadLine();
if (count > 6 && count < 39)
{
if (count < 16)
{
string[] Tablestr = new string[3];
Tablestr[0] = str.Substring(str.IndexOf("=") + 1, 5);
Tablestr[1] = str.Substring(str.IndexOf(",") + 1, 5);
Tablestr[2] = str.Substring(str.IndexOf(",") + 7, 4);
DataRow newrow = dt.NewRow();
dt.Rows.Add(newrow);
dt.Rows[count - 7][0] = Tablestr[0];
dt.Rows[count - 7][1] = Tablestr[1];
dt.Rows[count - 7][2] = Tablestr[2];
}
else
{
string[] Tablestr = new string[3];
Tablestr[0] = str.Substring(str.IndexOf("=") + 1, 6);
Tablestr[1] = str.Substring(str.IndexOf(",") + 1, 5);
Tablestr[2] = str.Substring(str.IndexOf(",") + 7, 4);
DataRow newrow = dt.NewRow();
dt.Rows.Add(newrow);
dt.Rows[count - 7][0] = Tablestr[0];
dt.Rows[count - 7][1] = Tablestr[1];
dt.Rows[count - 7][2] = Tablestr[2];
} }
count = count + 1; }
stRead.Close();
this.cbx_OilDischarge.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
this.cbx_OilDischarge.Items.Add(dt.Rows[i][0].ToString());
}
dataGridView1.RowHeadersVisible = false;
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.ShowCellToolTips = true;
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
}
catch (Exception)
{
OpenFile.InitialDirectory = Application.StartupPath;
OpenFile.RestoreDirectory = true;
stRead.Close();
MessageBox.Show("读取配置文件异常!请确认配置文件是否正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
OpenFile.InitialDirectory = Application.StartupPath;
OpenFile.RestoreDirectory = true;
}
一楼正解 ofd.RestoreDirectory = true; //此属性为关闭对话框是否恢复当前设置
)因为每次先执行上面的代码.执行顺序和逻辑判断位置的问题,你好好想想
OpenFile.RestoreDirectory = true;
这个语句应该放在 if (OpenFile.ShowDialog() == DialogResult.OK) 上面
也不知道为什么,至于你说的我是在打开按钮里面做的,我关闭这个画面后,打开别的画面就出现这种情况直接找我指定的路径了,没有在关闭前指定恢复吧