Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
C#
private void timer1_Tick(object sender, EventArgs e)
       {
           try
           {
               //todo  Read Configfile
               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();
               }
               //testing
               writeError(System.DateTime.Now.ToString() + " ->Starting write Target: " + target);
               // TEST IS OK. File is written
               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"); // Conditions for ending lines (each line is a line that needs to be removed)
                           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); // write ok!

                               if (destfi.Exists)
                               {
                                   destfi.Delete();
                               }
                               //do process
                               string result = ccrd.ReadCSV(file, target + "\\" + fi.Name);

                               //Check if everything saved ok
                               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

C#
        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;
                // open the file "data.csv" which is a CSV file with headers
                csv = new StreamReader(filename, System.Text.Encoding.Default);                
                csvwrite = new StreamWriter(destfilename, true, System.Text.Encoding.Default);
                while (!csv.EndOfStream)
                {
                    // remove line breaks
                    string line = csv.ReadLine();
                    if (!line.EndsWith("\""))
                    {
                        nextline = true;
                        while (nextline)
                        {
                            line += csv.ReadLine();
                            if (line.EndsWith("\""))
                                nextline = false;
                        }
                    }
                    //filter ending lines

                    foreach (string s in cond)
                    {
                        if (line.Contains(s))
                        {
                            line = "";
                            write = false;
                        }
                    }
                    //TODO Write the line
                    if (write)
                        csvwrite.WriteLine(line);
                }
                //close streams
                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?
Posted

1 solution

It may be that you are trying to write your error log to the application program folder, which under Win7 is not a good idea - it is quite possible that you do not have write permission to do so, despite running as admin.

Try changing the code to write it in a more "friendly" location and see if that gives you any actual information as to the nature of the problem.

(There are several "safe" places to do that that do not require any special permissions - see here for a few ideas: Where should I store my data?[^])
 
Share this answer
 
Comments
Mendor81 2-Jul-13 2:23am    
Hello Griff,

its not win7 that causes the problem, its works perfectly in 4 machines with win7 but not on the win 2008 server. it must be something within the code that i´m missing, because the first error.txt (the one in the commented line) is written perfectly it just freezes after that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900