public override void ProcessRequest()
        {
            RenameContentReq req = mSocketTalker.ReceiveObject <RenameContentReq>();
            RenameContentRes res = new RenameContentRes();

            try
            {
                Content content = new Content(req.ContentPath);
                switch (content.Type)
                {
                case Content.TYPE_NOT_FOUND:
                    throw new KnownException("无法找到文件或目录 " + content.Path + " 。");

                case Content.TYPE_DRIVER:
                    throw new KnownException("不能重命名驱动器 " + content.Path + " 。");

                case Content.TYPE_FILE:
                    if (IsFileOrDirectoryNameValid(req.NewContentName) == false)
                    {
                        throw new KnownException("新的文件名称 " + req.NewContentName + " 是无效的。");
                    }
                    string newFilePath = GetRenamedFilePath(content.Path, req.NewContentName);
                    File.Move(content.Path, newFilePath);
                    break;

                case Content.TYPE_DIRECTORY:
                    if (IsFileOrDirectoryNameValid(req.NewContentName) == false)
                    {
                        throw new KnownException("新的目录名称 " + req.NewContentName + " 是无效的。");
                    }
                    string newDirectoryPath = GetRenamedDirectoryPath(content.Path, req.NewContentName);
                    Directory.Move(content.Path, newDirectoryPath);
                    break;
                }

                mSocketTalker.SendInt(ProtocolTypes.TYPE_RENAME_CONTENT);
                mSocketTalker.SendObject(res);
            }
            catch (Exception e)
            {
                res.ErorrOccured = true;
                res.ErrorMessage = e.Message;

                mSocketTalker.SendInt(ProtocolTypes.TYPE_RENAME_CONTENT);
                mSocketTalker.SendObject(res);
            }
        }
Ejemplo n.º 2
0
        private void mRenameMenuItem_Click(object sender, EventArgs e)
        {
            string      currentName = mContentList.SelectedItems[0].Text;
            InputDialog inputDialog = new InputDialog();

            inputDialog.Title          = "请输入新的名称";
            inputDialog.AllowEmpty     = false;
            inputDialog.Result         = currentName;
            inputDialog.SelectionStart = 0;
            int length = currentName.LastIndexOf('.');

            inputDialog.SelectionLength = length > 0 ? length : currentName.Length;
            if (inputDialog.ShowDialog() == DialogResult.OK)
            {
                if (inputDialog.Result == currentName)
                {
                    return;
                }
                RenameContentReq req = new RenameContentReq();
                req.ContentPath    = Path.Combine(mCurrentDirectory, currentName);
                req.NewContentName = inputDialog.Result;
                mClient.SendRequest(ProtocolTypes.TYPE_RENAME_CONTENT, req);
            }
        }