/// <summary> /// Check whether the provided <paramref name="filePath"/> points to a valid file. If the specified file exists, /// while OverwriteExisting and AppendExisting are both false, an exception will be thrown. /// </summary> /// <param name="filePath">The path of the specified file for writing</param> /// <param name="settings">The OverwriteExisting and AppendExisting properties will be used for checking the file specified</param> protected static void CheckFilePath(String filePath, CsvWriterSettings settings) { settings = settings ?? new CsvWriterSettings(); if (String.IsNullOrEmpty(filePath)) { throw new ArgumentException("Parameter is not valid", "filePath"); } if (File.Exists(filePath) && !settings.OverwriteExisting && !settings.AppendExisting) { throw new Exception(String.Format("The specified file {0} already exists", filePath)); } if (settings.OverwriteExisting && settings.AppendExisting) { throw new ArgumentException("Overwrite and Append cannot both be true."); } var parentFolder = Path.GetDirectoryName(filePath); if (!Directory.Exists(parentFolder)) { throw new FileNotFoundException("Cannot find specified folder", filePath); } }
/// <summary> /// Initialize the current CsvWriter instance with provided information /// </summary> /// <param name="stream">A writable stream to which current writer will write data</param> /// <param name="settings">Configurable options customizing current CsvWriter instance</param> /// <param name="dataResolver">A customer data resolver converting objects to raw CSV values</param> protected CsvWriter(Stream stream, CsvWriterSettings settings, IDataResolver <T> dataResolver) { this.mDataResolver = dataResolver; this.mCsvSettings = settings = settings ?? new CsvWriterSettings(); EnsureParameters(stream, settings, dataResolver); settings.BufferSize = Math.Min(BUFFER_SZMAX, Math.Max(settings.BufferSize, BUFFER_SZMIN)); mNeedQuoteChars = new Char[] { '\r', '\n', '\"', settings.Seperator }; this.mWriter = new StreamWriter(stream, settings.Encoding, settings.BufferSize); if (stream.CanSeek) { if (settings.AppendExisting) { stream.Seek(0, SeekOrigin.End); } if (settings.OverwriteExisting) { stream.SetLength(0); } } }
private static void EnsureParameters(Stream stream, CsvWriterSettings settings, IDataResolver <T> dataResolver) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanWrite) { throw new ArgumentException("stream is not writable", "stream"); } if (settings.Encoding == null) { throw new ArgumentNullException("settings.Encoding"); } if (dataResolver == null) { throw new ArgumentNullException("dataResolver"); } }
/// <summary> /// Create an instance of CsvWriter with a specified writable stream, a CsvWriterSettings object and a custom data resolver. /// </summary> /// <param name="stream">A writable strem to be written into.</param> /// <param name="settings">Specify the options to control behavior of CsvWriter.</param> /// <param name="dataResolver">A custom data resolver used to serialize and deserialize data.</param> /// <returns>A CsvWriter instance.</returns> public static CsvWriter <T> Create(Stream stream, CsvWriterSettings settings, IDataResolver <T> dataResolver) { return(new CsvWriter <T>(stream, settings, dataResolver)); }
/// <summary> /// Create an instance of CsvWriter with a specified file path, a CsvWriterSettings object and a custom data resolver. /// If the path already exists and AppendExisting and OverwriteExisting of settings are both false, an exception will be thrown. /// </summary> /// <param name="filePath">The path of the CSV file to be written.</param> /// <param name="settings">Specify the options to control behavior of CsvWriter.</param> /// <param name="dataResolver">A custom data resolver used to serialize and deserialize data.</param> /// <returns>A CsvWriter instance.</returns> public static CsvWriter <T> Create(String filePath, CsvWriterSettings settings, IDataResolver <T> dataResolver) { CheckFilePath(filePath, settings); return(Create(File.OpenWrite(filePath), settings, dataResolver)); }