471,337 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

reference to external file in streamwriter

Hi,

I am trying to create a reference to an external file to write to using
stream writer and a value that is created in a for. Basically the value
that a user will create will be the name of the form.

The code below show my dilemma. The line "using (StreamWriter sw = new
StreamWriter(@"c:\{0}.txt",fmtname)) " does not work as the arguement
"fmtname" is not valid in that context. Do I need to use some string
builder routine prior to this to make the sw accept the input value as its
arguement??

private void btnOK_Click(object sender, System.EventArgs e)

{

if (int.Parse(tbStart.Text)>int.Parse(tbEnd.Text))

MessageBox.Show("The value in the End box is less than the Start");
double Vend =System.Convert.ToDouble(tbEnd.Text);

double Interval=System.Convert.ToDouble(tbInterval.Text);

double Start=System.Convert.ToDouble(tbStart.Text);

if ((Vend-Start) %Interval>0 )

MessageBox.Show("The interval is not a readily divisible into the End
value");

string fmtname=tbName.Text.ToString();

string fmtstart=tbStart.Text.ToString();

string fmtend=tbEnd.Text.ToString();

using (StreamWriter sw = new StreamWriter(@"c:\{0}.txt",fmtname))

{

sw.WriteLine("Proc format; ");

sw.WriteLine("value {0}",fmtname) ;

sw.WriteLine("{0} - {1} = '{0} to {1}'",fmtstart, fmtend);

sw.WriteLine("; ");

sw.WriteLine("Run;");

MessageBox.Show(@"File written to c:\{0}.txt",fmtname);

}

}

thanks

Doug
Feb 23 '06 #1
2 3454
I think the problem is
using (StreamWriter sw = new StreamWriter(@"c:\{0}.txt",fmtname))
try using
using (StreamWriter sw = new StreamWriter("c:\{0}.txt",fmtname))
OR
string filename = String.Format("c:\{0}.txt",fmtname);
using (StreamWriter sw = new StreamWriter(filename));
Sincerely,
Bobby

Feb 23 '06 #2
thanks

I tried
using (StreamWriter sw = new StreamWriter(string.Format(@"c:\{0}.txt",
fmtname)))

and this works perfectly

Doug

<bo*********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
I think the problem is
using (StreamWriter sw = new StreamWriter(@"c:\{0}.txt",fmtname))
try using
using (StreamWriter sw = new StreamWriter("c:\{0}.txt",fmtname))
OR
string filename = String.Format("c:\{0}.txt",fmtname);
using (StreamWriter sw = new StreamWriter(filename));
Sincerely,
Bobby

Feb 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Matt | last post: by
3 posts views Thread by Darrin | last post: by
3 posts views Thread by Ganesh Palaniappan | last post: by
reply views Thread by punjabinezzie | last post: by
8 posts views Thread by wdh3rd | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.