C# write a text file

Q and A about functionality and how to solve a special task for your application.

Moderator: Support

Post Reply
VincentPix
Posts: 44
Joined: 06 Nov 2014, 15:07

C# write a text file

Post by VincentPix » 07 Oct 2015, 11:26

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

User avatar
lerou
Posts: 337
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: C# write a text file

Post by lerou » 07 Oct 2015, 11:31

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
	{
	}
}

VincentPix
Posts: 44
Joined: 06 Nov 2014, 15:07

Re: C# write a text file

Post by VincentPix » 07 Oct 2015, 11:48

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)

TVM Cracklings
Posts: 33
Joined: 30 Sep 2015, 10:49

Re: C# write a text file

Post by TVM Cracklings » 07 Oct 2015, 16:07

Don't forget to escape the backslash in the path string, or replace it with a normal slash.

User avatar
lerou
Posts: 337
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: C# write a text file

Post by lerou » 07 Oct 2015, 16:28

he's putting in the string as a parameter so he doesn't need escaping it. Also in C# you'd use @"..."

VincentPix
Posts: 44
Joined: 06 Nov 2014, 15:07

Re: C# write a text file

Post by VincentPix » 07 Oct 2015, 16:46

It's working perfectly !

Great tool with the json parser.

Thank you

Post Reply