Saves the text to a file. : Text File Read Write « File Stream « C# / C Sharp






Saves the text to a file.

     
/*
 * Filename:    FileUtil.cs
 * Product:     Versioning Controlled Build
 * Solution:    BuildAutoIncrement
 * Project:     Shared
 * Description: File utility methods.
 * Copyright:   Julijan ?ribar, 2004-2007
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author(s) be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace BuildAutoIncrement {

    /// <summary>
  /// A set of file utilities.
  /// </summary>
  public struct FileUtil {
        /// <summary>
        ///   Saves the text to a file.
        /// </summary>
        /// <param name="fileName">
        ///   Name of the file to save text to.
        /// </param>
        /// <param name="content">
        ///   Text to save.
        /// </param>
        /// <param name="encoding">
        ///   Encoding used to save the file.
        /// </param>
        public static void SaveTextFile(string fileName, string content, Encoding encoding) {
            Debug.Assert(fileName != null);
            FileAttributes fileAttribs = File.GetAttributes(fileName);
            if ((fileAttribs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
                fileAttribs ^= FileAttributes.ReadOnly;
                File.SetAttributes(fileName, fileAttribs);
            }
            using (StreamWriter sw = new StreamWriter(fileName, false, encoding)) {
                sw.Write(content);
            }
        }
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Read and Write a Text File
2.Read text file line by line
3.Read ASCII string from byte bufferRead ASCII string from byte buffer
4.Reads and displays bytes until end-of-fileReads and displays bytes until end-of-file
5.Write string to a text file
6.Read whole text file to the end
7.Read text file line by line with exception catch
8.Text file Write with format and write boolean value to a text file
9.Action Text Reader Line
10.Open and Append to a Log File
11.Read and Write to a Newly Created Data File
12.Read Text from a File
13.Read text file with File.OpenText
14.Write Text to a File
15.Read a text file and obtain it's contents.
16.Reads text from a file.
17.Read Text File From Ressource
18.Create a large file of 100 lines to upload
19.Creates or opens a file for writing and writes text to it.
20.Returns the raw number of the current line count.
21.Returns the zero-based line number where source appears in target.
22.Returns the number of lines appearing in target where a line is counted as a '\n'