Advertisement

01.21.2008 at 05:36AM PST, ID: 23098081
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.6

Error: There has been a sharing violation, the source or destination file maybe in use.

Asked by gbzhhu in Handheld and PDA Programming, .NET, C# Programming Language

Tags: , , , ,

Hi,

I am developing amobile device app. with VS 2005 and c#.  At start up the app checks if there is an update for any of the files (via web service), if there is any they get downloaded and the actual application exe is started.

Now, whenever I do an update, everything seems ok.  Next time I try to do the same thing, I get an error.  It seems the files are locked.  I cannot delete/copy them manually or by code.  I believe it is my code locking it but I need more eyes to find which bit of code is doing it.  Below I post most of my code dealing with file download/update.

It starts with the code below.  The code uses the backgroundworker component from D Moth.

                    foreach (XmlNode node in files)
                    {
                        if (node != null)
                        {
                            count++;

                            // Get the file name of the current updated file.
                            file = node.InnerXml;
                            if (file.Length > 0)
                            {
                                Application.DoEvents();

                                // Download the file to our temp directory.
                                BackgroundWorker worker = new BackgroundWorker();
                                worker.DoWork += new DoWorkEventHandler(Utils.HttpDownload);
                                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                                BackgroundWorkerArgs args = new BackgroundWorkerArgs();
                                args.DownloadSource = url + file;
                                args.DownloadDestination = AppDir + UpdatesTempDir + file;
                                workers.Add(worker);
                                workers[workers.Count -1].RunWorkerAsync(args);
                            }
                        }
                    }

This above results a call made to HttpDownload in Utils class.  The mothods are below

        public static void HttpDownload(object sender, DoWorkEventArgs e)
        {
            BackgroundWorkerArgs args = (BackgroundWorkerArgs)e.Argument;
            HttpDownload(args.DownloadSource, args.DownloadDestination);
        }

        public static bool HttpDownload(string source, string destination)
            {
                  bool downloaded = false;
                  HttpWebRequest request = null;
                  HttpWebResponse response = null;
                  FileStream newFile = null;      
                  BinaryReader reader = null;
                  BinaryWriter writer = null;        

                  try
                  {
                lock (syncLock)
                {
                    // Create the destination file. If it exists then overwrite it.
                    newFile = new FileStream(destination, FileMode.Create, FileAccess.Write);

                    // Request the source.
                    request = (HttpWebRequest)HttpWebRequest.Create(source);
                    response = (HttpWebResponse)request.GetResponse();
                    // Read the response.
                    reader = new BinaryReader(response.GetResponseStream());
                    writer = new BinaryWriter(newFile);                         
                }
                  }
            catch (Exception ex)
                  {
                        // Failed trying to create the local file or opening the request.
                        downloaded = false;
                  }

                  try
                  {
                        while (true)
                        {                    
                              // Write bytes to the new file.
                              writer.Write(reader.ReadByte());
                        }
                  }
                  catch (System.IO.EndOfStreamException)
                  {
                        // We reached the end of the file.
                        downloaded = true;
                  }
                  catch(System.Exception ex)
                  {
                        // Download failed.
                        downloaded = false;
                  }
                  finally
                  {
                        // Always close the files.
                        if (writer != null)
                        {
                              writer.Flush();
                              writer.Close();
                        }

                if (newFile != null)
                    newFile.Close();
                  }

                  return downloaded;
            }


On completing each file download, a method on the GUI gets called and is shown below along with methods (those dealing with files) called within it.

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                currentFileNumber++;

                if (currentFileNumber == totalFileCount + 1)
                {
                    // Install the new updates.
                    this.pbProgress.Value = 100;
                    this.UpdateProgressLabel("Installing updated files...");
                    System.Threading.Thread.Sleep(150);

                    this.CopyDirectoryNonRecurse(AppDir + UpdatesTempDir, AppDir, false);
                    // Update the client config file with the new version number.
                    this.UpdateProgressLabel("Reconfiguring client components...");
                    System.Threading.Thread.Sleep(150);
                    UpdateConfig(AppDir + ClientConfigFile, newVersion);

                    this.UpdateProgressLabel("New version installed.");
                    System.Threading.Thread.Sleep(150);

                    // Clear the temp download directory and files.
                    this.UpdateProgressLabel("Deleting temporary files...");
                    System.Threading.Thread.Sleep(150);
                    this.ClearDirectory(AppDir + UpdatesTempDir);

                    currentFileNumber = 0;
                    totalFileCount = 0;

                    DoUpdate(true);
                }
                else
                {
                    this.pbProgress.Value = 90 / totalFileCount * (currentFileNumber - 1);
                    Application.DoEvents();
                    this.UpdateProgressLabel("Downloading file " + currentFileNumber.ToString() + " of " + totalFileCount + "...\n");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Failed to auto-update", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                StartApp();
            }
        }

        private void CopyDirectoryNonRecurse(string sourcePath, string destPath, bool deleteFirst)
        {
            // Put paths into DirectoryInfo object to validate.
            DirectoryInfo dirInfoSource = new DirectoryInfo(sourcePath);
            DirectoryInfo dirInfoDest = new DirectoryInfo(destPath);

            // Check if destination dir exists.
            if (Directory.Exists(dirInfoDest.FullName))
            {
                if (deleteFirst)
                {
                    Directory.Delete(dirInfoDest.FullName, true);
                    Directory.CreateDirectory(dirInfoDest.FullName);
                }
            }
            else
            {
                // Create the new directory.
                Directory.CreateDirectory(dirInfoDest.FullName);
            }

            // Copy all files in the directory.
            CopyDirFiles(dirInfoSource.FullName, dirInfoDest.FullName);
        }

        private void CopyDirFiles(string sourcePath, string destinationPath)
        {
            try
            {
                // Get dir info which may be file or dir info object.
                DirectoryInfo dirInfo = new DirectoryInfo(sourcePath);
                if (!destinationPath.EndsWith(@"\"))
                {
                    destinationPath = destinationPath + @"\";
                }

                foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
                {
                    if (fsi is FileInfo)
                    {
                        // If file object just copy. Overwrites files!    
                        File.Copy(fsi.FullName, destinationPath + fsi.Name, true);
                    }
                    else
                    {
                        // Ignore sub directories.
                    }
                }
            }
            catch (Exception ex)
            {                
                throw;
            }
        }

        private void ClearDirectory(string dir)
        {
            try
            {
                // Check if the directory exists.
                if (Directory.Exists(dir))
                {
                    Directory.Delete(dir, true);
                }

                // Create the directory.
                Directory.CreateDirectory(dir);
            }
            catch (Exception)
            {
                throw;
            }
        }
 
What I am looking for is help to find the code that is leaving files locked.  I cannot tell if my application's process is still running.  VS remote tools are not working for me.  It cannot connect to device! don't know why!

Thanks
H




Start Free Trial
 
Loading Advertisement...
 
[+][-]01.21.2008 at 05:44AM PST, ID: 20705938

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 05:51AM PST, ID: 20705984

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 08:40AM PST, ID: 20707329

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 08:48AM PST, ID: 20707393

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 08:49AM PST, ID: 20707407

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Handheld and PDA Programming, .NET, C# Programming Language
Tags: sharing, cannot, violation, file, error
Sign Up Now!
Solution Provided By: PockyMaster
Participating Experts: 2
Solution Grade: B
 
 
[+][-]01.21.2008 at 08:53AM PST, ID: 20707448

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 09:10AM PST, ID: 20707587

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 09:16AM PST, ID: 20707638

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 09:25AM PST, ID: 20707732

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 10:15AM PST, ID: 20708100

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 02:13PM PST, ID: 20710195

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.22.2008 at 01:52AM PST, ID: 20712944

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.22.2008 at 02:07AM PST, ID: 20712991

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.22.2008 at 05:48AM PST, ID: 20714093

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.22.2008 at 06:03AM PST, ID: 20714201

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628