ASP.NET C# WriteToFile StreamWriter error: 'StreamWriter' could not be found


R

rex64

I am getting an error message and I have not been able to figure hot
how to fix it. I have done some research with no answers yet.

I found this code that may help? Not sure what to do with it.
using (StreamWriter sw = new StreamWriter (fullAddress , false,
Encoding.UTF7))


Error message::::::::::::::::::::::::::::::::::::::::::::::
Code:
Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name
'StreamWriter' could not be found (are you missing a using directive or
an assembly reference?)

Source Error:



Line 40:
Line 41:
Line 42: 	StreamWriter sw = new StreamWriter(arg1, arg2);  //This was
commented out????
Line 43: 	//the line above is where I get the errors either way.
Line 44:


Source File: E:\webapps\finance\gfst\asp\jeffUploadNsave.aspx    Line:
42



Code::::::::::::::::::::::::::::::
void WriteToFile(string szData, string szFilePath)
{
//Write to file
StreamWriter sw = new StreamWriter(arg1, arg2);  //This was commented
out????  StreamWriter(arg1, arg2);
//the line above is where I get the errors either way.

//             Constructor with 2 arguments:
//             1) Path location of file
//             2) Append or Overwrite Data
StreamWriter(@"G:\Inetpub\wwwroot\csf\Tutorials\Exec\fileIO.txt",true);

//This will append the given input to the file since arg2 is true
sw.Write("I love C# and I don't care who knows it!~~" + "<br>");

//Cleanup
sw.Close();
}
 
Ad

Advertisements

G

Guest

StreamWriter is part of System.IO, so you need a reference to it.

Try putting

using System.IO;

at the top of your code.

HTH,
pagates
 
N

Nicholas Paldino [.NET/C# MVP]

rex64,

The reason for this is that you are using the shorthand for the
StreamWriter class, and you don't have the appropriate imports statement at
the top. What you want to do is place this at the top of your page:

<%@ Import namespace="System.IO" %>

And it should work.

Hope this helps.
 
R

rex64

That did not work:
Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS1519: Invalid token 'using' in class, struct,
or interface member declaration

Source Error:



Line 6: <body bgcolor="#ffffff" style="font:8pt verdana;">
Line 7: <script language="C#" runat="server">
Line 8: using System.IO;
Line 9: //Strings:
http://www.peachpit.com/articles/article.asp?p=31938&seqNum=12
Line 10: //http://www.developerfusion.com/show/4398/
 
Ad

Advertisements


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