Beispiel #1
0
        private static enTask GetTask(string line)
        {
            enTask curTask = enTask.None;


            switch (line)
            {
            case "CreateSubfolder":
                curTask = enTask.CreateSubfolder;
                break;

            case "SetAttributes":
                curTask = enTask.SetAttributes;
                break;

            case "Copy":
                curTask = enTask.Copy;
                break;

            case "Delete":
                curTask = enTask.Delete;
                break;

            case "Rename":
                curTask = enTask.Rename;
                break;

            case "Move":
                curTask = enTask.Move;
                break;

            case "CreateShortcut":
                curTask = enTask.CreateShortcut;
                break;

            default:
                curTask = enTask.None;
                break;
            }

            return(curTask);
        }
Beispiel #2
0
        private static string ExecuteTask(enTask Task, string strPathSource, string strPathTarget)
        {
            string strRet = "";

            if (Task != enTask.None)
            {
                try
                {
                    switch (Task)
                    {
                    case enTask.CreateSubfolder:
                        if (System.IO.Directory.CreateDirectory(strPathTarget) == null)
                        {
                            strRet = "ERROR while trying: " + "CreatedSubfolder: At " + strPathTarget
                                     + " - Message: Cannot create folder";
                        }
                        else
                        {
                            strRet = "CreatedSubfolder: At " + strPathTarget;
                        }

                        break;


                    case enTask.SetAttributes:

                        string[] arrStrAttributes = strPathTarget.Split(';');

                        if (arrStrAttributes.Length > 2)
                        {
                            strRet = "SetAttributes: File " + strPathSource + " to attributes " + strPathTarget;

                            bool ReadOnly = bool.Parse(arrStrAttributes[0]);
                            bool Hidden   = bool.Parse(arrStrAttributes[1]);

                            /*strRet += "_" + CreatedDateTime.Year.ToString() + "-" + CreatedDateTime.Month.ToString() + "-" + CreatedDateTime.Day.ToString();
                             * strRet += "_" + CreatedDateTime.Hour.ToString() + "_" + CreatedDateTime.Minute.ToString();*/

                            string[] arrStrDateTime = arrStrAttributes[2].Split('_');

                            if (arrStrDateTime.Length > 3)
                            {
                                string[] arrStrDate = arrStrDateTime[1].Split('-');

                                if (arrStrDate.Length > 2)
                                {
                                    // Split OK
                                    string strYear  = arrStrDate[0];
                                    string strMonth = arrStrDate[1];
                                    string strDay   = arrStrDate[2];

                                    string strHour = arrStrDateTime[2];
                                    string strMin  = arrStrDateTime[3];

                                    DateTime CreatedDateTime = new DateTime(int.Parse(strYear), int.Parse(strMonth), int.Parse(strDay), int.Parse(strHour),
                                                                            int.Parse(strMin), 0);


                                    // Set Creation Time
                                    FileAttributes attributes = File.GetAttributes(strPathSource);
                                    File.SetAttributes(strPathSource, attributes & ~FileAttributes.ReadOnly);

                                    File.SetCreationTime(strPathSource, CreatedDateTime);      // May not be write Protected for this



                                    // Set Attributes

                                    if (ReadOnly)
                                    {
                                        attributes = attributes | FileAttributes.ReadOnly;
                                    }
                                    else
                                    {
                                        attributes = attributes & ~FileAttributes.ReadOnly;
                                    }

                                    if (Hidden)
                                    {
                                        attributes = attributes | FileAttributes.Hidden;
                                    }
                                    else
                                    {
                                        attributes = attributes & ~FileAttributes.Hidden;
                                    }

                                    File.SetAttributes(strPathSource, attributes);
                                }
                            }
                        }
                        else
                        {
                            strRet = "ERROR while trying: " + "SetAttributes: File " + strPathSource + " to attributes" + strPathTarget
                                     + " - Message: Invalid arguments";
                        }

                        break;


                    case enTask.Copy:
                        AddFolderPathsThatDontExist(strPathTarget);
                        strRet = "COPIED: From " + strPathSource + " to " + strPathTarget;
                        File.Copy(strPathSource, strPathTarget);
                        break;

                    case enTask.Delete:
                        // Be careful here
                        // get the file attributes for file or directory
                        FileAttributes attr = File.GetAttributes(strPathSource);

                        //detect whether its a directory or file
                        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                        {
                            //MessageBox.Show("Its a directory");

                            if (IsDirectoryEmpty(strPathSource))
                            {
                                strRet = "DELETED: " + strPathSource;

                                DirectoryInfo di = new DirectoryInfo(strPathSource);
                                di.Attributes = FileAttributes.Normal;

                                Directory.Delete(strPathSource);
                            }
                            else
                            {
                                strRet = "ERROR while trying: " + "DELETED: " + strPathSource
                                         + " - Message: This is not a file or empty Dir";
                            }
                        }
                        else
                        {
                            // Is a file
                            //MessageBox.Show("Its a file");
                            strRet = "DELETED: " + strPathSource;

                            File.SetAttributes(strPathSource, FileAttributes.Normal);     // for write protected files
                            File.Delete(strPathSource);
                        }

                        break;

                    case enTask.Move:
                        AddFolderPathsThatDontExist(strPathTarget);
                        strRet = "MOVED: From " + strPathSource + " to " + strPathTarget;
                        File.Move(strPathSource, strPathTarget);
                        break;

                    case enTask.Rename:
                        strRet = "RENAMED: From " + strPathSource + " to " + strPathTarget;
                        File.Move(strPathSource, strPathTarget);
                        break;

                    case enTask.CreateShortcut:
                        AddFolderPathsThatDontExist(strPathTarget);
                        strRet = "CREATED_SHORTCUT: At " + strPathSource + " to " + strPathTarget;

                        addShortcut(strPathSource, strPathTarget);
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("[CLIENT] Error: " + ex.Message);
                    strRet = "ERROR while trying: " + strRet + " - Message: " + ex.Message;
                }
            }
            else
            {
                strRet = "Invalid Task";
            }



            return(strRet + "\n");
        }
Beispiel #3
0
        static void BCLSystemIOPipeClient()
        {
            /////////////////////////////////////////////////////////////////////
            // Try to open a named pipe.
            //

            // Prepare the pipe name
            String strServerName = ".";
            String strPipeName   = "RoMoRDuP";

            NamedPipeClientStream pipeClient = null;

            try
            {
                pipeClient = new NamedPipeClientStream(
                    strServerName,              // The server name
                    strPipeName,                // The unique pipe name
                    PipeDirection.InOut,        // The pipe is bi-directional
                    PipeOptions.None,           // No additional parameters

                    //The server process cannot obtain identification information about
                    //the client, and it cannot impersonate the client.
                    TokenImpersonationLevel.Anonymous);

                pipeClient.Connect(60000); // set TimeOut for connection
                pipeClient.ReadMode = PipeTransmissionMode.Message;

                Console.WriteLine(@"The named pipe, \\{0}\{1}, is connected.",
                                  strServerName, strPipeName);



                /////////////////////////////////////////////////////////////////
                // Send a message to the pipe server and receive its response.
                //

                // A byte buffer of BUFFER_SIZE bytes. The buffer should be big
                // enough for ONE request to the client

                string strServer = "";
                byte[] bClient   = new byte[4096];       // Client -> Server
                int    cbRequestBytes;
                byte[] bServer = new byte[BUFFER_SIZE];  // Server -> Client
                int    cbBytesRead, cbReplyBytes;



                // __________Execute Tasks__________

                enMainState mainState     = enMainState.GetTask;
                enTask      curTask       = enTask.None;
                string      strPathSource = "";
                string      strPathTarget = "";


                while (pipeClient.IsConnected)
                {
                    // Receive one message from the pipe.
                    cbReplyBytes = BUFFER_SIZE;
                    int offset = 0;
                    int index  = -1;
                    strServer = "";
                    bool bSuccess = false;

                    do
                    {
                        cbBytesRead = pipeClient.Read(bServer, offset, 50);
                        offset     += cbBytesRead;

                        // Unicode-encode the byte array and trim all the '\0' chars
                        // at the end.
                        strServer = Encoding.Unicode.GetString(bServer, 0, offset); //.TrimEnd('\0'); // Trim End is insufficient
                        //strServer = strServer.Remove(offset); // !!!!!!!!!!!!Problem Unicode

                        if (strServer.Contains("*"))
                        {
                            index    = strServer.IndexOf('*');
                            bSuccess = true;
                        }
                        else
                        {
                            index = strServer.IndexOf('\0');
                        }

                        if (index >= 0)
                        {
                            strServer = strServer.Remove(index);
                        }
                    } while((offset < 4096) && (bSuccess == false));


                    if (bSuccess)
                    {
                        //Console.WriteLine("Receives {0} bytes; Message: \"{1}\"",
                        //    cbBytesRead, strMessage);

                        string[] listReceived = strServer.Split('\n');

                        foreach (string line in listReceived)
                        {
                            if (line.Length > 0)
                            {
                                string strline = line.TrimEnd('\0');
                                strline = strline.TrimStart('\0');

                                Console.WriteLine("[CLIENT] Echo: " + strline);


                                // Process Message
                                switch (mainState)
                                {
                                case enMainState.GetTask:
                                    curTask   = GetTask(strline);
                                    mainState = enMainState.GetPathSource;
                                    break;

                                case enMainState.GetPathSource:
                                    strPathSource = strline;
                                    mainState     = enMainState.GetPathTarget;
                                    break;

                                case enMainState.GetPathTarget:
                                    strPathTarget = strline;
                                    mainState     = enMainState.ExecuteTask;
                                    break;

                                default:
                                    mainState = enMainState.GetTask;
                                    break;
                                }


                                if (mainState == enMainState.ExecuteTask)
                                {
                                    // Send one message to the pipe.

                                    string strAnswer = ExecuteTask(curTask, strPathSource, strPathTarget);

                                    cbRequestBytes = Encoding.Unicode.GetBytes(strAnswer + "\0", 0, strAnswer.Length + 1, bClient, 0);


                                    if (pipeClient.CanWrite)
                                    {
                                        pipeClient.Write(bClient, 0, cbRequestBytes);
                                    }
                                    pipeClient.Flush();

                                    //Console.WriteLine("Sends {0} bytes; Message: \"{1}\"",
                                    //    cbRequestBytes, strMessage.TrimEnd('\0'));

                                    mainState = enMainState.GetTask;
                                }
                            }
                        }
                    }
                }


                bClient        = Encoding.Unicode.GetBytes("EXIT");
                cbRequestBytes = bClient.Length;
                if (pipeClient.CanWrite)
                {
                    pipeClient.Write(bClient, 0, cbRequestBytes);
                }
                pipeClient.Flush();
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine("Unable to open named pipe {0}\\{1}",
                                  strServerName, strPipeName);
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The client throws the error: {0}", ex.Message);
            }
            finally
            {
                /////////////////////////////////////////////////////////////////
                // Close the pipe.
                //

                if (pipeClient != null)
                {
                    pipeClient.Close();
                }
            }
        }
        public string ExecuteTaskString(string strServer)
        {
            // __________Execute Tasks__________

            enMainState mainState     = enMainState.GetTask;
            enTask      curTask       = enTask.None;
            string      strPathSource = "";
            string      strPathTarget = "";

            try
            {
                //Console.WriteLine("Receives {0} bytes; Message: \"{1}\"",
                //    cbBytesRead, strMessage);

                string[] listReceived = strServer.Split('\n');

                foreach (string line in listReceived)
                {
                    if (line.Length > 0)
                    {
                        string strline = line.TrimEnd('\0');
                        strline = strline.TrimStart('\0');

                        Console.WriteLine("[CLIENT] Echo: " + strline);


                        // Process Message
                        switch (mainState)
                        {
                        case enMainState.GetTask:
                            curTask   = GetTask(strline);
                            mainState = enMainState.GetPathSource;
                            break;

                        case enMainState.GetPathSource:
                            strPathSource = strline;
                            mainState     = enMainState.GetPathTarget;
                            break;

                        case enMainState.GetPathTarget:
                            strPathTarget = strline;
                            mainState     = enMainState.ExecuteTask;
                            break;

                        default:
                            mainState = enMainState.GetTask;
                            break;
                        }


                        if (mainState == enMainState.ExecuteTask)
                        {
                            // Send one message to the pipe.

                            string strAnswer = ExecuteTask(curTask, strPathSource, strPathTarget);


                            return(strAnswer);
                        }
                    }
                }
            }


            catch (Exception ex)
            {
                Console.WriteLine("The client throws the error: {0}", ex.Message);
            }


            return(null);
        }