Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace Default
{

protected void btnCreate_Click(object sender, EventArgs e)
{
string path = "D:\\MyTest.txt";
if (!File.Exists(path))
{
string() createText = { "Hello","Reading and writing text file" };
File.WriteAllLines(path, createText);
}
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
lbltxt.Text = "File Created Successfully";
}
protected void btnRead_Click(object sender, EventArgs e)
{
string path = fileupload1.PostedFile.FileName;
if(!string.IsNullOrEmpty(path))
{
string() readText = File.ReadAllLines(path);
StringBuilder strbuild = new StringBuilder();
foreach (string s in readText)
{
strbuild.Append(s);
strbuild.AppendLine();
}
textBoxContents.Text = strbuild.ToString();
}







the errors are:
Expected enum,class,delegate or struct
Expected enum,class,delegate or struct
Expected enum,class,delegate or struct
Posted
Comments
[no name] 9-Jul-15 7:20am    
string() readText = File.ReadAllLines(path);
is wrong
string[] readText = File.ReadAllLines(path);
Sergey Alexandrovich Kryukov 9-Jul-15 11:53am    
5!—SA

1 solution

Your syntax of creating arrays is wrong. You have to use [], not ():
C#
string[] createText = { "Hello", "Reading and writing text file" };

Also, it appears that you are putting methods directly in a namespace. You cannot do that; methods have to be put in a class. I suppose you meant to put them in the class of your web form (looking at your using namespaces it appears that you use web forms).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Jul-15 11:53am    
5ed.
—SA
TRISA BISWAS 10-Jul-15 1:56am    
I corrected it as u said about that array part. still getting the following errors:
Error 4 A namespace cannot directly contain members such as fields or methods
Error 1 Expected class, delegate, enum, interface, or struct
Error 3 Expected class, delegate, enum, interface, or struct
Error 2 Identifier expected
Error 6 The type or namespace name '?Attribute' could not be found (are you missing a using directive or an assembly reference?)
Error 5 Type or namespace definition, or end-of-file expected
Thomas Daniels 10-Jul-15 16:22pm    
I edited my answer; I saw that you were putting methods directly in a namespace, but that isn't possible; they have to be in a class (that's also what "Error 4" says as provided in your comment).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900