Hello I need some help, I'm trying to output something to a text file (.htm) which will then display the data that is being generated in the C# program..

Now the problem is, whenever I try to replace something, it doesn't work. Just replaces it with "System.Char[]"

Here is the code:

public static void WebPage(char[] text)
{
       var theText = text;
	
       string dir = Path.Combine(Directory.GetCurrentDirectory(), "index.htm");
	if(!File.Exists(dir))
	{
	     Console.WriteLine ("Cannot find the file");
	     return;
	}
			
	string webpage = getData (dir);
	FileStream fstream = new FileStream(dir, System.IO.FileMode.Create);
	StreamWriter sWriter = new StreamWriter(fstream);
			
	string variable = "var text="; // This is the variable in "index.htm"
	string replacement = variable + theText; // This is what I'm trying to replace it with 
	sWriter.Write(webpage.Replace(variable, replacement));
	sWriter.Flush();
}

So basically, let's say that the program generated the line "Hello world, this is C#", it would then search through the index.htm file and find "var text=" and then replace it with "var text=Hello world, this is C#"

Any ideas please? :)

Remember: text and theText are char arrays and not a string.

Ahh thanks for the reply =)

I've tried to convert it to a string, still outputs the same.. It's really weird:

var theText = text.ToString();

etc..

Does it really need to be a char array?
Can't the parameter be a string?

If not, look at something like this:

char[] arr = { 'a', 'b', 'c' };
string strArr = new string(arr);
if (File.Exists("index.htm"))
            {
                string data = File.ReadAllText("index.htm");

                string newdata = data.Replace("var text=", "var text=Hello world, this is C#");

                File.WriteAllText("index.htm", newdata);
            }

Did you fix it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.