1

I am new to C# and ASP.Net, and I am running into this error

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

Here is my code Currently:

<% @Page Language="C#" %>

<!-- code section -->
<script runat="server">


private void WriteUpper(object sender, EventArgs e)
{
  string str = mytext.Value;

  using (StreamWriter outputFile = new StreamWriter(@"~\AppData\data.txt", true)) {
outputFile.WriteLine(str);
   }

}
</script>

<!-- Layout -->
<html>
 <head> 
 <title> Untitled </title> 
</head>

<body>


  <form id="Form1" runat="server">
     <input runat="server" id="mytext" type="text" />
     <input runat="server" id="button1" type="submit" value="Enter..." OnServerClick="WriteUpper"/>

     <hr />

  </form>

</body>

</html>
Anonymous Dodo
  • 241
  • 3
  • 14
  • Possible duplicate of [Getting "type or namespace name could not be found" but everything seems ok?](http://stackoverflow.com/questions/3304741/getting-type-or-namespace-name-could-not-be-found-but-everything-seems-ok) – Heretic Monkey Jul 19 '16 at 19:54

2 Answers2

1

StreamWriter is in the System.IO namespace. Try adding a

#using System.IO;

to your page.

u8it
  • 3,084
  • 1
  • 15
  • 28
Jason James
  • 1,062
  • 1
  • 14
  • 27
1

I have never added namespaces in cshtml(razor) code but you could just use the entire namespace when initializing like so:

using (System.IO.StreamWriter outputFile = new System.IO.StreamWriter(@"~\AppData\data.txt", true))

let me know if that helps.

Grizzly
  • 5,411
  • 5
  • 39
  • 94
  • I mean I can try. But that is a completely different issue from this question. I would honestly just ask another question – Grizzly Jul 19 '16 at 19:56