Example #1
0
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        #endregion

        #region WriteCSV
        //	+--------------------------------------------------+
        //	|+++	WriteCSV								+++|
        //	+--------------------------------------------------+

        // CSV Datei erzeugen (Array)
        public static bool WriteCSV(string path, string filename, char delimeter, string[][] data, bool encrypt = false, string passphrase = null, bool append = false)
        {
            if (filename == ResourceText.EMPTY)
            {
                throw new Exception(CreateException(ResourceText.Message, ResourceText.ExceptionClass, ResourceText.ExceptionWriteCSV, ResourceText.ExceptionFileNameEmpty));
            }

            if (!CheckDirectory(path))
            {
                throw new Exception(CreateException(ResourceText.Message, ResourceText.ExceptionClass, ResourceText.ExceptionWriteCSV, ResourceText.ExceptionDirectoryNotExist));
            }

            // Weitere Prüfungen nichtnotwendig da später über Filedialog alles bereits abgeprüft
            path = CheckDirectory(path, true);

            string       stream     = null;
            StreamWriter datastream = new StreamWriter(path + filename, append);

            // Zeile Eingabedaten + Seperator (ASCII)

            if (data.Length >= int.MaxValue)
            {
                throw new Exception(CreateException(ResourceText.Message, ResourceText.ExceptionClass, ResourceText.ExceptionWriteCSV, ResourceText.ExceptionFileSize));
            }

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].Length > int.MaxValue)
                {
                    throw new Exception(CreateException(ResourceText.Message, ResourceText.ExceptionClass, ResourceText.ExceptionWriteCSV, ResourceText.ExceptionDataLength));
                }

                for (int j = 0; j < data[i].Length; j++)
                {
                    stream += (data[i][j] + delimeter);
                    // datastream.Write(data[i][j] + delimeter);
                }
                stream += '\n'.ToString();
                // datastream.WriteLine();
            }

            if (encrypt == true)
            {
                if (passphrase == null)
                {
                    return(false);
                }
                else
                {
                    stream = Chiper.Encrypt(stream, passphrase);
                }
            }

            datastream.Write(stream);
            datastream.Close();

            return(true);
        }
Example #2
0
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        #endregion

        #region ReadCSV
        //	+--------------------------------------------------+
        //	|+++	ReadCSV									+++|
        //	+--------------------------------------------------+

        // CSV Datei auslesen (Array)
        public static string[][] ReadCSV(string path, string filename, char delimeter, bool decrypt = false, string passphrase = null)
        {
            List <string[]> data     = new List <string[]>();
            string          linedata = null;

            path = CheckDirectory(path, true);

            if (filename == ResourceText.EMPTY)
            {
                throw new Exception(CreateException(ResourceText.Message, ResourceText.ExceptionClass, ResourceText.ExceptionReadCSV, ResourceText.ExceptionFileNameEmpty));
            }

            if (!CheckDirectory(path) || !CheckFile(path + filename))
            {
                throw new Exception(CreateException(ResourceText.Message, ResourceText.ExceptionClass, ResourceText.ExceptionReadCSV, ResourceText.ExceptionFileNotFound));
            }

            // Weitere Prüfungen nichtnotwendig da später über Filedialog alles bereits abgeprüft

            StreamReader datastream;

            // Wenn Entschlüsseln erforderlich
            if (decrypt == true)
            {
                if (passphrase == null)
                {
                    return(null);
                }

                StreamReader cryptostream = new StreamReader(path + filename);
                byte[]       decryptarray = Encoding.UTF8.GetBytes(Chiper.Decrypt(cryptostream.ReadToEnd(), passphrase));

                MemoryStream stream = new MemoryStream(decryptarray);

                datastream = new StreamReader(stream);
            }
            else
            {
                datastream = new StreamReader(path + filename);
            }

            while ((linedata = datastream.ReadLine()) != null)
            {
                string[] seperateddata = linedata.Split(delimeter);
                data.Add(seperateddata);
            }
            datastream.Close();

            return(data.ToArray());
        }