1

How can i write string in file i tried more methods like :

StreamWriter writer = new StreamWriter(System.IO.Path.GetFullPath("backup.cmd"));
writer.WriteLine("File created using StreamWriter class.");
writer.Close();

And :

System.IO.File.WriteAllText(System.IO.Path.GetFullPath("backup.cmd"), BackupCMD); 

and when i try to execute StreamWriter only without anything the same error

but the the same result :

object reference not set to an instance of an object

I think the error in the StreamWriter initializing.

Farhad Jabiyev
  • 23,650
  • 6
  • 59
  • 96
ideal male
  • 21
  • 1
  • 4

1 Answers1

0

Another option in one code line using static WriteAllText method from File class by giving only the file name and the text you want to write:

File.WriteAllText("backup.cmd", "File created using File.WriteAllText.");
Nikolay Kostov
  • 14,580
  • 20
  • 75
  • 115
  • 1
    It is customary to provide comments when you downvote :) I think my post provides an answer to the question *"How can I write string in file"* – Nikolay Kostov Feb 14 '15 at 10:38
  • The error always is : object reference not set to an instance of an object – ideal male Feb 14 '15 at 10:44
  • Are you sure this `NullReferenceException` is on the line of the code I gave you? Can you provide a screenshot of the exception in Visual Studio? – Nikolay Kostov Feb 14 '15 at 10:46
  • Here you can find the exception error : http://postimg.org/image/8642cdouh/ and i Checking file exists :: The result true – ideal male Feb 14 '15 at 10:51
  • Are you sure the problem is with writing the text in file? – Nikolay Kostov Feb 14 '15 at 11:01
  • 1
    Thanks alot friends i found the error i can't access the file in Program files i am so sorry i am not so good in english to describe my problem well my languages are Arabic & Deutsch Thanks alot – ideal male Feb 14 '15 at 11:06