急急急!!!C#高手请帮忙!指点新手!在线等

mayuliuze 2010-12-23 10:21:28
这是在一个form里有9个textbox,和一个button,目的是触发button按钮,将文本框的Text转换为double型,并存储到新建的文本文件中 文本文件的命名是根据相关文本框的Text内容而定的
private void button1_Click(object sender, EventArgs e)
{
double a = double.Parse(txt_浇注温度.Text);
double b = double.Parse(txt_拉速.Text);
double c = double.Parse(txt_时间步长.Text);
double m = double.Parse(txt_宽度.Text);
double k = double.Parse(txt_厚度.Text);
double n = double.Parse(txt_网格dx.Text);
double l = double.Parse(txt_网格dy.Text);
double x = 0.5 * m / n;
double y = k / l;
double[] dValue = { a, b, c, n, l, x, y };
//设置文件的存储路径
string str1 = Application.StartupPath;
string str2 = "BeginValue";
string beginValueStr = Path.Combine(str1, str2);
//自动生成文件夹下面的文件
string fileName = txt_厚度.Text + "_" + txt_宽度.Text;
string valuePath = Path.Combine(beginValueStr, fileName);
using (StreamWriter sw = new StreamWriter(valuePath))
{
for (int i = 0; i < dValue.Length; i++)
{
sw.Write(dValue[i]);
sw.WriteLine(" ");
}
sw.Close();
}
}

调试时有如下错误
未能找到路径“E:\C#\窗体工作\窗体程序初结合\my_first_windows\bin\Debug\BeginValue\230_1270”的一部分。(说明230,1270是文本框的Text值)
请大家帮我看一下,哪里有问题,请帮我在原有代码上修改!谢谢大家啦!是在原有代码的基础上修改哦
...全文
143 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
X_MSDN 2010-12-23
  • 打赏
  • 举报
回复
重新生成函数 执行
TimZhuFaith 2010-12-23
  • 打赏
  • 举报
回复
string valuePath = Path.Combine(beginValueStr, fileName+".txt");
using (StreamWriter sw = new StreamWriter(valuePath,true))
{
for (int i = 0; i < dValue.Length; i++)
{
sw.Write(dValue[i]);
sw.WriteLine(" ");
sw.Flush();
}
sw.Close();
}
}

kewell119 2010-12-23
  • 打赏
  • 举报
回复
你beginValueStr这个目录还没有创建啊
if (Directory.Exists())
Directory.CreateDirectory(beginValueStr);
StreamWriter sw = File.CreateText(valuePath);
这样就可以了
Kevin0105 2010-12-23
  • 打赏
  • 举报
回复
//自动生成文件夹下面的文件
string fileName = txt_厚度.Text + "_" + txt_宽度.Text;
string valuePath = Path.Combine(beginValueStr, fileName);
FileStream fs = new FileStream(valuePath,FileMode.OpenOrCreate);
using (StreamWriter sw = new StreamWriter(fs))
{
for (int i = 0; i < dValue.Length; i++)
{

试一下改红色的地方。

mayuliuze 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kevin0105 的回复:]
问题可能是出在你只有写入流。没有文件流。
例如下面的代码可以正常工作:
Using System.IO;
FileStream fs = new FileStream("c:\\test.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);

sw.Write("Hello world!");
sw……
[/Quote]
那请问您,我这个该怎么改呢,在原代码上,谢谢您啦
Kevin0105 2010-12-23
  • 打赏
  • 举报
回复
问题可能是出在你只有写入流。没有文件流。
例如下面的代码可以正常工作:
Using System.IO;
FileStream fs = new FileStream("c:\\test.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);

sw.Write("Hello world!");
sw.Close();
Console.Read();
mayuliuze 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]
要写的内容拼接好,一次性执行File.WriteAllLines写文件。
[/Quote]
请说的具体点,因为文本框的内容是用户输入的,不是固定值,而这些值我后面的窗体工作是要引用的,所以先把他们存到文本文档,等用时再读取。麻烦你啦
mayuliuze 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 timzhufaith 的回复:]
Rebuild
[/Quote]
麻烦能不能说的清楚点
mayuliuze 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]
清理解决方案 重新生成
[/Quote]
重新生成没错误,调试时出错的
TimZhuFaith 2010-12-23
  • 打赏
  • 举报
回复
Rebuild
q107770540 2010-12-23
  • 打赏
  • 举报
回复
清理解决方案 重新生成
兔子-顾问 2010-12-23
  • 打赏
  • 举报
回复
要写的内容拼接好,一次性执行File.WriteAllLines写文件。
xixihaha_2011_098 2010-12-23
  • 打赏
  • 举报
回复
应该是路径错了,

string fileName = txt_厚度.Text + "_" + txt_宽度.Text+".txt";
string valuePath = Path.Combine(beginValueStr, fileName);

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧