/// <summary> /// Checks if a given <paramref name="fileUri" /> can be interpreted as CSV without any errors. /// </summary> /// <param name="fileUri">The location of the CSV file.</param> /// <param name="encoding">The encoding to use.</param> /// <param name="containsHeaders"><c>true</c> if headers are present in the topmose line.</param> /// <param name="separator">The char which seperates columns.</param> /// <returns><c>true</c> if the file is valid otherwise <c>false</c>.</returns> public static bool IsValidCsvFile(string fileUri, Encoding encoding, bool containsHeaders = false, char separator = ',') { if (!File.Exists(fileUri)) { throw new FileNotFoundException("Provided file not found.", fileUri); } string[] lines = null; try { lines = File.ReadAllLines(fileUri, encoding); } catch { } if (lines == null || !lines.Any()) { return(false); } var firstLine = lines[0].Split(separator); if (!firstLine.Any()) { return(false); } var result = true; var lineNo = 1; foreach (var line in lines) { result &= line.Split(separator).Count() == firstLine.Count(); lineNo++; if (!result) { TraceUtil.WriteTraceError("Line #{0}", lineNo); break; } } return(result); }
/// <summary> /// Has to be called before any other operation in this type. /// </summary> /// <param name="server">The server dns name without any prefix and port.</param> /// <param name="user">The user name.</param> /// <param name="pass">The password of the <paramref name="user"/>.</param> /// <param name="port">The port number (defaults to 21).</param> /// <returns></returns> public Task <bool> InitializeAsync(string server, string user, string pass, int port = 21) { return(Task.Run( () => { _initialized = false; _server = server; _user = user.ToSecureString(); _pass = pass.ToSecureString(); _port = port; try { var request = GetRequest(); _initialized = request != null; } catch (Exception ex) { TraceUtil.WriteTraceError(ex.Message); } return _initialized; })); }