Ejemplo n.º 1
0
        /// <summary>
        /// Starts the file sharing process
        /// </summary>
        public void StartFileSharingProcess()
        {
            Boolean isAlreadyShared = false;                              //the file is already shared

            string fileFullPath = Path.Combine(_sharedFolder, _fileName); //file location

            string newFileName = _fileName;

            try
            {
                //Create the destination directory if it not exist
                if (!Directory.Exists(_destinationFolder))
                {
                    // Try to create the directory.
                    try
                    {
                        Directory.CreateDirectory(_destinationFolder);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("The directory couldn't been created : {0}", e.ToString());
                    }
                }
                else
                {
                    string tmpDestinationFile = Path.Combine(_destinationFolder, _fileName);
                    //check if the destination directory has another file with the same name
                    //if is true, check if the files are identical
                    if (File.Exists(tmpDestinationFile))
                    {
                        isAlreadyShared = FileAction.FilesCompare(fileFullPath, tmpDestinationFile);
                    }

                    if (!isAlreadyShared)
                    {
                        newFileName = SetFileName(); //rename the file if the name is already used
                    }
                }

                //if the file is already in destination, delete it from shared directory
                if (isAlreadyShared == true)
                {
                    FileAction.DeleteFile(fileFullPath);
                    //Move the file in the destnation direcotry
                }
                else
                {
                    string fullDestinationPath = Path.Combine(_destinationFolder, newFileName);

                    FileAction.MoveFile(fileFullPath, fullDestinationPath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }