I recently develloped a C# app which goes through directories and subdirectories and apply my if/else instruction on 13 type of .csv files and push informations into my SQL Server Database .
I created a table with Directories and files count , and also the datetime of the treatement , and i want to fill it with these informations , so i could do some comparison and see if there is any missing/error , and have a kind of logs about how much data I entered on my db every day . Is there a way to do it with GetFiles/GetDirectories property ? Or with a SQLBulkCopy ? Or any other kind of C# code ? i searched a lot and could'nt find any proper solution .
Thanks for your listening .
Here is my code :
bool _CompareFileName(string fileName, string id)
{
if (fileName.Substring(fileName.Length - id.Length, id.Length) == id) // On compare les fichiers par rapport à leur " id "
return true;
else
return false;
}
public void LogFile(string sExceptionName, string sEventName, string sControlName, int nErrorLineNo, string sFormName)
{
StreamWriter log;
if (!File.Exists("logfile.txt"))
{
log = new StreamWriter("logfile.txt");
}
else
{
log = File.AppendText("logfile.txt");
}
// Write to the file:
log.WriteLine("Data Time:" + DateTime.Now);
log.WriteLine("Exception Name:" + sExceptionName);
log.WriteLine("Event Name:" + sEventName);
log.WriteLine("Control Name:" + sControlName);
log.WriteLine("Error Line No.:" + nErrorLineNo);
log.WriteLine("Form Name:" + sFormName);
// Close the stream:
log.Close();
}
public void CsvToSql(string strCmdText) // La méthode permettant d'entrer les données du CSV à la table SQL
{
try
{
DirectoryInfo dir = new DirectoryInfo("U:/FichiersINPI/IMR_Donnees_Saisies/tc/flux/2017/06/01/");
Console.WriteLine("Recherche de fichier .csv convertis :"); //Fonction de recherche des csv convertis précédemment
//On utilise un StreamWriter pour écrire les sorties de boucle dans un .bat
using (StreamWriter writer = new StreamWriter("U:/FichiersINPI/IMR_Donnees_Saisies/tc/flux/2017/06/01/CsvSql.bat"))
foreach (var Fi in Directory.GetFiles("U:/FichiersINPI/IMR_Donnees_Saisies/tc/flux/2017/06/01/", "*.csv", SearchOption.AllDirectories)) //On précise qu'on veut tout les répértoires
{
if (_CompareFileName(Fi, "1_PM.csv")) // Boucle If pour les 13 types de fichiers
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..PM /force"); //Personne Morale
}
else if (_CompareFileName(Fi, "2_PM_EVT.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..PM_EVT /force"); //Evenement Personne Morale
}
else if (_CompareFileName(Fi, "3_PP.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..PP /force"); // Personne Physique
}
else if (_CompareFileName(Fi, "4_PP_EVT.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..PP_EVT /force"); //Evenement Personne Physique
}
else if (_CompareFileName(Fi, "5_rep.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Rep /force"); // Représentants
}
else if (_CompareFileName(Fi, "6_rep_nouveau_modifie_EVT.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Rep_new /force"); // Modification relative aux représentants
}
else if (_CompareFileName(Fi, "7_rep_partant_EVT.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Rep_supression /force"); // Supression d'un représentant
}
else if (_CompareFileName(Fi, "8_ets.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Ets /force"); // Etablissements
}
else if (_CompareFileName(Fi, "9_ets_nouveau_modifie_EVT.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Ets_modification /force"); // Modification relative aux établissements
}
else if (_CompareFileName(Fi, "10_ets_supprime_EVT.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Ets_supression /force"); // Supression d'un établissement
}
else if (_CompareFileName(Fi, "11_obs.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Obs /force"); // Observation
}
else if (_CompareFileName(Fi, "12_actes.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Acte /force"); // Actes
}
else if (_CompareFileName(Fi, "13_comptes_annuels.csv"))
{
writer.WriteLine("Csv2Sql_new " + Fi + " IMR_INPI..Compte_annuel /force"); //Comptes annuels
}
else // On conclue la boucle par un catch qui arrête le traitement automatique en cas d'erreur ( Table pas assez large , fichier corrompu .. )
{
}
}
//On execute le .bat
Process.Start("CMD.exe", "/C start U:/FichiersINPI/IMR_Donnees_Saisies/tc/flux/2017/06/01/CsvSql.bat");
Console.WriteLine("Fichier bat executé avec succés , données intégrées");
}
catch (Exception ex)
{
Console.WriteLine("Il y a un eu une erreur : " + ex.ToString());
}
}
Related
I am calling following method inside thread. inside method i have written logs which writes some variable values in notepad file.
Problem : Sometime last log of method do not log anything. ie last line not exucuting sometime. i am not able to understand problem. please guide me if there are issue somewhere.
This is web application hosted in iis server.
Function :
public bool ResetEmployeeAssignedCoursesByRole()
{
bool bReturn = false;
DbTransactionHelper dbTransactionHelper = new DbTransactionHelper();
dbTransactionHelper.BeginTransaction();
try
{
// this line execuete fine
ErrorLog.createRoleLog("Method sp_Reset_EmpAssignedCoursesByRole Started " + this.RoleID + " Course ID " + this.CourseID + " External Message " + sMessage);
StringBuilder sbQueryEmployeeCourse = new StringBuilder();
sbQueryEmployeeCourse.Append(" set #roleID = " + roleID + "; ");
sbQueryEmployeeCourse.Append(" set #courseID = " + courseID + "; ");
sbQueryEmployeeCourse.Append(" set #inActiveReason = " + inActiveReason + "; ");
sbQueryEmployeeCourse.Append(" set #lastRecordUpdateSource = " + lastRecordUpdateSource + "; ");
sbQueryEmployeeCourse.Append(" call sp_Reset_EmpAssignedCoursesByRole (#roleID, #courseID, #inActiveReason, #lastRecordUpdateSource); ");
DataTable dtEmployeeCourse = dbTransactionHelper.GetDataTable(BusinessUtility.GetString(sbQueryEmployeeCourse), CommandType.Text, null
);
dbTransactionHelper.CommitTransaction();
bReturn = true;
// this line not execuete sometime.
ErrorLog.createRoleLog("Method sp_Reset_EmpAssignedCoursesByRole Ended " + this.RoleID + " Course ID " + this.CourseID + " External Message " + sMessage);
}
catch (Exception ex)
{
ErrorLog.createRoleLog("Add Role ID " + BusinessUtility.GetString(roleID));
dbTransactionHelper.RollBackTransaction();
ErrorLog.createRoleLog("Add Course ID " + BusinessUtility.GetString(courseID));
ErrorLog.createRoleLog(ex.ToString());
}
return bReturn;
}
Calls method like below :
Thread t = new Thread(ResetEmployeeAssignedCoursesByRole);
t.Start();
FUNCTION createRoleLog :
public static void createRoleLog(string errorMessage, int empHdrID = 0)
{
try
{
//string path = BusinessUtility.GetString(AppConfig.GetAppConfigValue("LogsDiractory")) + "Log" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
string path = BusinessUtility.GetString(ErrorLog.ErrorLogPath) + "RoleLog" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
if (BusinessUtility.GetString(AppConfig.GetAppConfigValue("LogError")).ToString().ToUpper() == "TRUE")
{
if (!File.Exists(path))
{
StreamWriter sw = File.CreateText(path);
sw.Close();
}
//using (System.IO.StreamWriter sw = System.IO.File.AppendText(path))
//{
// sw.WriteLine("-------- " + DateTime.Now + " --------");
// sw.WriteLine(errorMessage);
// sw.WriteLine("------------------------");
// sw.Close();
//}
if (!IsFileLocked(new FileInfo(path)))
{
using (FileStream stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (System.IO.StreamWriter sw = new StreamWriter(stream))
{
if (empHdrID != 0)
{
sw.WriteLine("-------- [empHdrID: " + empHdrID + "] " + DateTime.Now + " --------");
}
else
{
sw.WriteLine("-------- " + DateTime.Now + " --------");
}
sw.WriteLine(errorMessage);
sw.WriteLine("------------------------");
sw.Close();
}
}
}
else
{
}
}
}
catch (Exception ex)
{
//throw ex;
}
}
Last line of function which not execution sometime is :
** this line not execuete sometime.**
ErrorLog.createRoleLog("Method sp_Reset_EmpAssignedCoursesByRole Ended " + this.RoleID + " Course ID " + this.CourseID + " External Message " + sMessage);
I try to insert a word heading at a specific position but it is always added at the end of the document. Where is my mistake?:
foreach (Publish p in tempstructure)
{
if (p.element_type_id == 3)
{
Word.Paragraph par = selection.Paragraphs.Add();
par.Range.Text = "test";
par.Range.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
par.Range.InsertParagraphAfter();
selection.TypeParagraph();
}
else
{
if (File.Exists(#Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx"))
{
selection.InsertFile(#Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx");
selection = word.Selection;
}
else
{
selection.TypeText("Missing file: " + p.filename + "_" + language + ".docx");
selection.TypeParagraph();
}
}
selection = word.Selection;
}
Best regards ...
The simple solution is:
foreach (Publish p in tempstructure)
{
if (p.element_type_id == 3)
{
selection.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
selection.TypeText(p.name);
selection.TypeParagraph();
}
else
{
if (File.Exists(#Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx"))
{
selection.InsertFile(#Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx");
selection = word.Selection;
}
else
{
selection.TypeText("Missing file: " + p.filename + "_" + language + ".docx");
selection.TypeParagraph();
}
}
selection = word.Selection;
}
I'm getting a OutOfMemory exception when running the following code, it happens on the File.ReadLines line, it processes most files fine until it hits larger files.
It's consistantly using tons of memory and cpu during the whole process though.
The file it crashed on is only 156,000KB, which is 156mb
static void Main(string[] args)
{
Console.CursorVisible = false;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(" [" + DateTime.Now.ToShortTimeString() + "]" + " Connected to the Cassandra Database");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
string filepath = #"C:\Users\admin\Desktop\wecrack lists";
DirectoryInfo directory = new DirectoryInfo(filepath);
int fileCount = 0;
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("cracking");
var collection = database.GetCollection<Password>("passwords");
foreach (var file in directory.GetFiles("*"))
{
fileCount++;
Console.WriteLine(" [" + DateTime.Now.ToShortTimeString() + "]" + " Working through file: {" + file + "} {" + fileCount + "/" + directory.GetFiles("*").Count() + "}");
List<Password> entitys = new List<Password>();
foreach (string line in File.ReadLines(filepath + #"\" + file.ToString()))
{
entitys.Add(new Password { password = line });
}
collection.InsertManyAsync(entitys);
}
Console.WriteLine();
Console.WriteLine(" [" + DateTime.Now.ToShortTimeString() + "]" + " Finished inserting records, press any key to get the count.");
Console.ReadKey(true);
while (true)
{
Console.ReadKey(true);
}
}
Try batching your updates. That way you won't have all that data in memory at the same time. It may also help you not totally lock up your database.
...
foreach (var file in directory.GetFiles("*"))
{
fileCount++;
Console.WriteLine(" [" + DateTime.Now.ToShortTimeString() + "]" + " Working through file: {" + file + "} {" + fileCount + "/" + directory.GetFiles("*").Count() + "}");
System.IO.StreamReader file = new System.IO.StreamReader(filepath + #"\" + file.ToString());
while(!file.EndOfStream)
{
int passwordBatchCount = 0;
List<Password> entitysBatch = new List<Password>();
while ((string line = file.ReadLine()) != null && passwordBatchCount < BATCH_SIZE)
{
entitysBatch.Add(new Password { password = line });
passwordBatchCount++;
}
collection.InsertManyAsync(entitysBatch);
}
file.Close();
}
}
...
So I'm trying to exit a console application IF a parameter check fails, however, I still want it to log to a file. The logging to a file works fine as long as all the parameters are good. However, when a parameter check fails and hits the System.Environment.Exit(0) portion, the log file is still completely empty. Here's the code so far. Please help, I've tried everything I could think of.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace I2C_File_Splitter
{
class Program
{
static void Main(string[] args)
{
//get command line input paramaters
using (StreamWriter log = File.AppendText("Splitter_log.txt"))
{
log.WriteLine(DateTime.Now + " ******************************************** SPLITTER STARTED ****************************************************************");
log.WriteLine(DateTime.Now + " FILE: " + args[0] + " DESTINATION: " + args[1] + " MAX COUNT PER FILE: " + args[2]);
if (args.Length == 0)
System.Environment.Exit(0);
string originalFile = args[0];
string destination = args[1];
int fileLength = Convert.ToInt32(args[2]);
string fileName;
string fileExtension;
string line;
int fileNumber = 1;
if (!File.Exists(originalFile))
{
log.WriteLine(DateTime.Now + " Error: Transfund file not found for: " + args[0]);
log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
System.Environment.Exit(0);
}
if (!Directory.Exists(destination))
{
log.WriteLine(DateTime.Now + " Error: destination directory not found for: " + args[1] );
log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
System.Environment.Exit(0);
}
if (fileLength < 0)
{
log.WriteLine(DateTime.Now + " Error: file length must be greater than 0. Incorrect value " + args[2]);
log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
System.Environment.Exit(0);
}
//get file name and file extension
fileName = Path.GetFileNameWithoutExtension(originalFile);
fileExtension = Path.GetExtension(originalFile);
StreamReader file = new StreamReader(originalFile);
log.WriteLine(DateTime.Now + " processing: " + fileName);
string header = file.ReadLine(); //get first line
while ((line = file.ReadLine()) != null)
{
StreamWriter newFile = new StreamWriter(destination + "\\" + fileName + "_" + fileNumber.ToString() + fileExtension);
newFile.WriteLine(header);
newFile.WriteLine(line);
int counter = 1;
while (counter < fileLength)
{
line = file.ReadLine();
if (line == null)
break;
newFile.WriteLine(line);
counter++;
}
newFile.Close();
log.WriteLine(DateTime.Now + " " + fileName + "_" + fileNumber.ToString() + fileExtension + " created. Card count: " + counter);
fileNumber++;
}
log.WriteLine(DateTime.Now + " Processing completed: " + fileName);
log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
}
}
}
}
When you call Environment.Exit, you're telling it to terminate your program immediately.
Your "log" stream never gets flushed (which would happen when you reach the end of the using block), and so nothing gets a chance to be written to the file.
Try flushing the stream before calling exit.
if (!Directory.Exists(destination))
{
log.WriteLine(DateTime.Now + " Error: destination directory not found for: " + args[1] );
log.WriteLine(DateTime.Now + " ******************************************** SPLITTER ENDED ****************************************************************");
// write all pending log messages to the file
log.Flush();
System.Environment.Exit(0);
}
I've the below c# code and i'm getting the current index is out of range error
public partial class FrmTreeViewContinents : Form
{
// Objets communs
XmlDocument monXml = new XmlDocument();
XmlNodeList listeNode = null;
XmlNode root = null;
public FrmTreeViewContinents()
{
InitializeComponent();
}
private void btnTransferToXml_Click(object sender, EventArgs e)
{
ManipXml obj = new ManipXml();
// Sérialise une collection d'objets 'Pays' en fichier XML
obj.SerialiseObjets("Pays.xml");
}
private void btnAfficheEntity_Click(object sender, EventArgs e)
{
// Chargement du fichier XML - Abandon si erreur
if (!ChargeFichierXml()) return;
// Sélecton des données dans le noeud XML 'Pays'
listeNode = root.SelectNodes("//Pays");
// Affichage des données lues
AffichageDonnees();
}
// Méthode d'affichage des données lues dans le fichier XML
private void AffichageDonnees()
{
txtBoxAffiche.Clear();
foreach (XmlNode noeud in listeNode)
{
txtBoxAffiche.Text += noeud.Attributes[2].InnerText.ToString() +
" - " +
noeud.Attributes[3].InnerText.ToString() +
" - " +
noeud.Attributes[4].InnerText.ToString() +
Environment.NewLine;
}
}
// Méthode de chargement du fichier XML
private bool ChargeFichierXml()
{
try
{
monXml.Load("Pays.xml");
}
catch (Exception ex)
{
MessageBox.Show("Erreur sur chargement du fichier XML"
+ Environment.NewLine + ex.Message,
"Erreur",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
//XmlNodeList listeNode;
root = monXml.DocumentElement;
return true;
}
}
class ManipXml
{
// Cette méthode permet de générer un fichier XML avec une collection de 'Pays'
public void SerialiseObjets(string sNomFichierXml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Collection<Pays>));
Collection<Pays> colPays = new Collection<Pays>();
Pays cnt1 = new Pays();
cnt1.NomPays = "Australie";
cnt1.NomCapitalePays = "Canberra";
cnt1.NomContinent = "Australie";
Pays cnt2 = new Pays();
cnt2.NomPays = "France";
cnt2.NomCapitalePays = "Paris";
cnt2.NomContinent = "Europe";
Pays cnt3 = new Pays();
cnt3.NomPays = "Espagne";
cnt3.NomCapitalePays = "Madrid";
cnt3.NomContinent = "Europe";
Pays cnt4 = new Pays();
cnt4.NomPays = "Chine";
cnt4.NomCapitalePays = "Beijing";
cnt4.NomContinent = "Asie";
Pays cnt5 = new Pays();
cnt5.NomPays = "Malaysia";
cnt5.NomCapitalePays = "Kuala-Lumpur";
cnt5.NomContinent = "Asie";
// Ajout des 'Continent' dans la collection
colPays.Add(cnt1);
colPays.Add(cnt2);
colPays.Add(cnt3);
colPays.Add(cnt4);
colPays.Add(cnt5);
// Instanciation d'un Stream
Stream st = new FileStream(sNomFichierXml, FileMode.Create);
// Génération du fichier XML
xmlSerializer.Serialize(st, colPays);
st.Close();
}
}
public class Pays // Définition de la classe Continent
{
public string NomPays { get; set; }
public string NomCapitalePays { get; set; }
public string NomContinent { get; set; }
}
the error i got is in the section below , which is "the current index is out of range"
foreach (XmlNode noeud in listeNode){
txtBoxAffiche.Text += noeud.Attributes[2].InnerText.ToString() +" - "
+ noeud.Attributes[3].InnerText.ToString() + " - "
+ noeud.Attributes[4].InnerText.ToString() +
Environment.NewLine;
}
can you please help me
thank you
Problem : You might be accessing the invalid Attributes of the Pays.xml file.
Solution: You need to check the Attributes.Count property before accessing the Attributes of the Pays.xml file.
Try This:
foreach (XmlNode noeud in listeNode)
{
for(int i=2;i<noeud.Attributes.Count;i++)
{
if(i!=noeud.Attributes.Count-1)
txtBoxAffiche.Text += noeud.Attributes[i].InnerText.ToString() +" - ";
else
txtBoxAffiche.Text += noeud.Attributes[i].InnerText.ToString() + Environment.NewLine;
}
}
The error is due to you are using the Index out of range.
for example if Array size 3 but if you try to access 4th index ,it will give error.
so please check this part for index value
foreach (XmlNode noeud in listeNode){
txtBoxAffiche.Text += noeud.Attributes[2].InnerText.ToString() +" - "
+ noeud.Attributes[3].InnerText.ToString() + " - "
+ noeud.Attributes[4].InnerText.ToString() +
Environment.NewLine;
}
The problem is in one of the Attributes[] index. You get the position 2, 3 and 4, and I bet that you don't have that many Attributes (4, 3 or 2).
You can set that values first to some variables before using them.
string attribute2 = noeud.Attributes[2].InnerText.ToString();
string attribute3 = noeud.Attributes[3].InnerText.ToString();
string attribute4 = noeud.Attributes[4].InnerText.ToString();
But this isn't a very good practice.... You would check first if the attribute exists.
string attribute2;
if (noeud.Attributes[2] != null) {
attribute2 = noeud.Attributes[2].InnerText.ToString();
}