Posted By : Murugan Andezuthu Dharmaratnam
Posted On : 26 November 2014
Keywords : Write to File, Stream Writer Write To File, C# .Net Write To File, Asp .Net Write To File, Write To File

C# .Net Streamwriter Write To File

Write To File


File.Open Method Opens a FileStream on the specified path.

Sample Code


   String strWrite = "\r\n Hello World";
   Write(Server.MapPath("todo.txt"), strWrite);


    public int Write(String FileName, String Data)
    {
        try
        {
            using (System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter(FileName, true))
            {
                oStreamWriter.Write(Data);
                return 1;
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }





You can also open file using File.Open
FileMode Enumeration Specifies how the operating system should open a file.

ENUM Values

FileMode.Append
FileMode.Create
FileMode.CreateNew
FileMode.Open
FileMode.OpenOrCreate
FileMode.Truncate



FileStream oFileStream = File.Open("C:\\fileName.txt", FileMode.Create);






Murugan Andezuthu Dharmaratnam

Murugan is a technology leader with 14 years of hands-on experience in full life cycle systems design, development, implementation, and management. In addition to systems design and development, his core competencies include cloud design and architecture, interactive web applications and websites development, mobile computing, and Healthcare Information Technology. His experience is very diverse with a broad range of technologies within multiple industry settings including private, public, and non-profit. Coupled with his technology expertise, Murugan is a well-organized, result-oriented, and a highly analytical leader with proven track record in developing and implementing very complex systems.