Click here to Skip to main content
15,890,579 members
Home / Discussions / C#
   

C#

 
AnswerRe: nullrefererence exception Pin
Luc Pattyn9-Nov-09 14:33
sitebuilderLuc Pattyn9-Nov-09 14:33 
GeneralRe: nullrefererence exception Pin
hobbsjas9-Nov-09 15:26
hobbsjas9-Nov-09 15:26 
GeneralRe: nullrefererence exception Pin
Luc Pattyn9-Nov-09 15:31
sitebuilderLuc Pattyn9-Nov-09 15:31 
GeneralRe: nullrefererence exception Pin
hobbsjas9-Nov-09 15:53
hobbsjas9-Nov-09 15:53 
GeneralRe: nullrefererence exception Pin
N a v a n e e t h9-Nov-09 16:02
N a v a n e e t h9-Nov-09 16:02 
GeneralRe: nullrefererence exception Pin
Luc Pattyn9-Nov-09 16:05
sitebuilderLuc Pattyn9-Nov-09 16:05 
GeneralRe: nullrefererence exception Pin
N a v a n e e t h9-Nov-09 15:59
N a v a n e e t h9-Nov-09 15:59 
GeneralRe: nullrefererence exception Pin
hobbsjas10-Nov-09 9:25
hobbsjas10-Nov-09 9:25 
Ok here is my dowork:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] src = e.Argument as string[];
            cnt = 0;
            while (cnt < src.Length && src[cnt] != null)
            {
                toolStripStatusLabel1.Text = "Copying " + src[cnt] + "...";
                string[] words = src[cnt].Split('\\');
                string dest = keeppath2 + "\\" + words[words.Length - 1];
                if (dest.IndexOf(".") > -1)
                {
                    FileStream inFile = new FileStream(src[cnt].ToString(), System.IO.FileMode.Open);
                    if (!System.IO.File.Exists(dest))
                    {
                        using (System.IO.FileStream fs = System.IO.File.Create(dest))
                        {
                            for (byte i = 0; i < 100; i++)
                            {
                                fs.WriteByte(i);
                            }
                        }
                    }
                    FileStream outFile = new FileStream(dest, System.IO.FileMode.Open);
                    long size = inFile.Length;
                    int lineSize = 1024 * 64;
                    long filesize1 = inFile.Position / 1024;
                    long filesize2 = inFile.Length / 1024;
                    byte[] buffer = new byte[lineSize];
                    while ((inFile.Read(buffer, 0, lineSize)) > 0)
                    {
                        outFile.Write(buffer, 0, lineSize);
                        filesize1 = inFile.Position / 1024;
                        labeltext = "Copying " + src[cnt] + "... " + filesize1 + "KB of " + filesize2 + "KB";
                        backgroundWorker1.ReportProgress(0);
                    }
                    outFile.Close();
                    inFile.Close();
                }
                else
                {
                    copyDirectory(src[cnt].ToString(), dest, toolStripStatusLabel1);
                }
                cnt++;
            }
        }


I handed off the toolstripstatuslabel1.text to backgroundWorker1.ReportProgress
here is the code:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            toolStripStatusLabel1.Text = labeltext;
        }


This works like a dream for files. The problem is when I call copyDirectory when I am copying a directory.
I can't call reportprogress from that and need to update the statuslabel which bombs. Here is the code for copyDirectory:
public static void copyDirectory(string Src, string Dst, ToolStripStatusLabel tool)
        {
            String[] Files;
            long filesize1 = 0;            
            long filesize2 = 0;                      
            if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
            {
                Dst += Path.DirectorySeparatorChar;
            }
            if (!Directory.Exists(Dst))
            {
                Directory.CreateDirectory(Dst);
            }
            Files = Directory.GetFileSystemEntries(Src);
            foreach (string Element in Files)
            {
                if (Directory.Exists(Element))
                {                    
                        copyDirectory(Element, Dst + Path.GetFileName(Element), tool);
                }
                else
                {
                    FileStream inFile = new FileStream(Element, System.IO.FileMode.Open);
                    if (inFile != null)
                    {
                        if (!System.IO.File.Exists(Dst + Path.GetFileName(Element)))
                        {
                            using (System.IO.FileStream fs = System.IO.File.Create(Dst + Path.GetFileName(Element)))
                            {
                                for (byte i = 0; i < 100; i++)
                                {
                                    fs.WriteByte(i);
                                }
                            }
                        }
                        FileStream outFile = new FileStream(Dst + Path.GetFileName(Element), System.IO.FileMode.Open);
                        long size = inFile.Length;
                        int lineSize = 1024 * 64;
                        filesize2 = inFile.Length / 1024;
                        byte[] buffer = new byte[lineSize];
                        while ((inFile.Read(buffer, 0, lineSize)) > 0)
                        {
                            outFile.Write(buffer, 0, lineSize);
                            filesize1 = inFile.Position / 1024;
                            tool.Text = "Copying " + Element + "... " + filesize1 + "KB of " + filesize2 + "KB";
                        }
                        outFile.Close();
                        inFile.Close();
                    }
                }
            }
        }


What is the best way to update the label from copyDirectory?
Thanks for all the help!
QuestionInconsistent accessibility: property type is less accessible than property Pin
Kevin Marois9-Nov-09 13:28
professionalKevin Marois9-Nov-09 13:28 
AnswerRe: Inconsistent accessibility: property type is less accessible than property Pin
J$9-Nov-09 13:36
J$9-Nov-09 13:36 
GeneralRe: Inconsistent accessibility: property type is less accessible than property Pin
Kevin Marois9-Nov-09 13:49
professionalKevin Marois9-Nov-09 13:49 
GeneralRe: Inconsistent accessibility: property type is less accessible than property Pin
Luc Pattyn9-Nov-09 13:56
sitebuilderLuc Pattyn9-Nov-09 13:56 
QuestionWin32 Message Hooks and/or Legitimate Code Injection Pin
cymblicity9-Nov-09 12:54
cymblicity9-Nov-09 12:54 
AnswerRe: Win32 Message Hooks and/or Legitimate Code Injection Pin
Luc Pattyn9-Nov-09 15:28
sitebuilderLuc Pattyn9-Nov-09 15:28 
Questionhow can i calculate shortest path between neurons in artifical neural network graph? Pin
karayel_kara9-Nov-09 12:32
karayel_kara9-Nov-09 12:32 
QuestionC# subject not correct form because of shift+enter Pin
Jacob Dixon9-Nov-09 10:45
Jacob Dixon9-Nov-09 10:45 
AnswerRe: C# subject not correct form because of shift+enter Pin
Abhishek Sur9-Nov-09 10:48
professionalAbhishek Sur9-Nov-09 10:48 
GeneralRe: C# subject not correct form because of shift+enter Pin
Jacob Dixon9-Nov-09 10:55
Jacob Dixon9-Nov-09 10:55 
GeneralRe: C# subject not correct form because of shift+enter Pin
Abhishek Sur9-Nov-09 21:37
professionalAbhishek Sur9-Nov-09 21:37 
Questionthreading access to toolstripmenuitem Pin
ToeKing9-Nov-09 10:41
ToeKing9-Nov-09 10:41 
AnswerRe: threading access to toolstripmenuitem Pin
Luc Pattyn9-Nov-09 11:37
sitebuilderLuc Pattyn9-Nov-09 11:37 
QuestionMp3 or Wav Stream and Parse Pin
Greg Mort9-Nov-09 9:56
Greg Mort9-Nov-09 9:56 
Questionhow to start a form in a specific location Pin
BDJones9-Nov-09 8:51
BDJones9-Nov-09 8:51 
AnswerRe: how to start a form in a specific location Pin
Christian Graus9-Nov-09 9:15
protectorChristian Graus9-Nov-09 9:15 
GeneralRe: how to start a form in a specific location Pin
BDJones9-Nov-09 9:20
BDJones9-Nov-09 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.