Hello everybody, i got a little Problem.
i programmed a little C# app to edit some .csv files (filter linebreaks and ending lines) to make an import into another system easier.
The programm works fine on 5 machines all win7 without any problems and completes the task within 30 seconds, but as soon as i try to run it on a win 2008 R2 server it freezes. here is the code:
The filepath are read through a txt file to make it more flexible in case the source or destination folders change. As well as a txt file for the line that need to be removed at the end of the csv.
Main form (using a timer)
private void timer1_Tick(object sender, EventArgs e)
{
try
{
string sourcePath = Application.StartupPath + "\\SourceFolder.txt";
string targetPath = Application.StartupPath + "\\TargetFolder.txt";
srSource = new StreamReader(sourcePath);
srTarget = new StreamReader(targetPath);
string target="";
while (!srTarget.EndOfStream)
{
target = srTarget.ReadLine();
}
writeError(System.DateTime.Now.ToString() + " ->Starting write Target: " + target);
while (!srSource.EndOfStream)
{
string line = srSource.ReadLine();
string[] filePaths = Directory.GetFiles(line, "*.csv");
foreach (string file in filePaths)
{
try
{
FileInfo fi = new FileInfo(file);
ChristmasCards ccrd = new ChristmasCards(Application.StartupPath + "\\ConfigEndings.txt");
if (ccrd.OK)
{
DirectoryInfo dir = new DirectoryInfo(target);
if (!dir.Exists)
{
dir.Create();
}
FileInfo destfi = new FileInfo(target+"\\" + fi.Name);
writeError(System.DateTime.Now.ToString() + " ->Readinfo Target " + target+"\\"+fi.Name);
if (destfi.Exists)
{
destfi.Delete();
}
string result = ccrd.ReadCSV(file, target + "\\" + fi.Name);
if (result == "OK")
writeError(System.DateTime.Now.ToString() + " -> " +"\r\nFile Saved as: " + target + "\\" + fi.Name);
else
writeError(System.DateTime.Now.ToString() + " -> " +"\r\nError File saving: " + result);
}
else
{
writeError(System.DateTime.Now.ToString() + " -> " + "\r\nError Configfile not readable");
srSource.Close();
}
}
catch (Exception ex)
{
writeError(System.DateTime.Now.ToString() + " -> " + "\r\nError:" + ex.Message);
srSource.Close();
}
}
}
srSource.Close();
srTarget.Close();
}
catch (Exception ex)
{
writeError(System.DateTime.Now.ToString() + " -> " + "\r\nError:" + ex.Message);
}
finally
{
this.Close();
Application.Exit();
}
}
private void writeError(string message)
{
StreamWriter errorwriter = new StreamWriter(Application.StartupPath + "\\Errors.txt", true, System.Text.Encoding.Default);
errorwriter.WriteLine(System.DateTime.Now.ToString()+" -> "+ message);
errorwriter.Close();
}
Christmas card class function
StreamReader conditionsreader;
StreamReader csv;
StreamWriter csvwrite;
bool ok = false;
ArrayList cond = new ArrayList();
#region constructor
public ChristmasCards(string configfile)
{
try
{
conditionsreader = new StreamReader(configfile);
while (!conditionsreader.EndOfStream)
{
cond.Add(conditionsreader.ReadLine());
}
conditionsreader.Close();
ok = true;
}
catch (Exception ex)
{
ok = false;
}
}
public string ReadCSV(string filename, string destfilename)
{
try
{
bool nextline = false;
bool write = true;
csv = new StreamReader(filename, System.Text.Encoding.Default);
csvwrite = new StreamWriter(destfilename, true, System.Text.Encoding.Default);
while (!csv.EndOfStream)
{
string line = csv.ReadLine();
if (!line.EndsWith("\""))
{
nextline = true;
while (nextline)
{
line += csv.ReadLine();
if (line.EndsWith("\""))
nextline = false;
}
}
foreach (string s in cond)
{
if (line.Contains(s))
{
line = "";
write = false;
}
}
if (write)
csvwrite.WriteLine(line);
}
csv.Close();
csvwrite.Close();
return "OK";
}
catch (Exception ex)
{
csvwrite.Close();
csv.Close();
return ex.Message;
}
}
The program on the server reaches the point where it creates the first csv with 0 bytes but then it freezes. and its already running as Administrator. any ideas?