reference to external file in streamwriter

G

gordon

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
 
B

bobbychopra

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
 
G

gordon

thanks

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

and this works perfectly

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top