Page 1 of 1

C# write a text file

Posted: 07 Oct 2015, 11:26
by VincentPix
Hello,

I'm trying to script a writer node.

a string Input (my text)
a string URL
and a Method write

also add using System.IO

Code: Select all

// This Method is called if the function/method Write is invoked by the user or a bound event.
	// Return true, if this component has to be revalidated!
	public bool OnWrite(int arg)
	{
		StreamWriter writetext = new StreamWriter(URL);
		writetext.WriteLine(Input);
		writetext.Close();
		
		changed = true;
		return false;
	}
I've also tried this method : File.WriteAllText(path, createText);

None gave a result.

Does someone have an idea ?

Thanks

Re: C# write a text file

Posted: 07 Oct 2015, 11:31
by lerou
Don't know what your URL ist. I've used this just recently where the input was a relative path like './log.txt':

Code: Select all

private void Log(string s)
{
	try
	{
		using (StreamWriter w = File.AppendText(inLogFile))
		{
			var now = DateTime.Now;
			w.WriteLine(string.Format("{0},{1}", now, s));
		}
	}
	catch
	{
	}
}

Re: C# write a text file

Posted: 07 Oct 2015, 11:48
by VincentPix
Thanks Lerou,

the URL is the full path as a string "‪D:\VENTUZ\V4tests\Data\MaConfig.txt"

I should have missed something cause I can't have your code working either. (ashamed)

Re: C# write a text file

Posted: 07 Oct 2015, 16:07
by TVM Cracklings
Don't forget to escape the backslash in the path string, or replace it with a normal slash.

Re: C# write a text file

Posted: 07 Oct 2015, 16:28
by lerou
he's putting in the string as a parameter so he doesn't need escaping it. Also in C# you'd use @"..."

Re: C# write a text file

Posted: 07 Oct 2015, 16:46
by VincentPix
It's working perfectly !

Great tool with the json parser.

Thank you