Error while moving directory

Asked

Viewed 44 times

0

I have a folderBrowserDialog that when we selected the folder it would move it to a particular directory.

I’m making the following mistake:

Cannot create a file when that file already exists.

but the folder does not exist.

The code is as follows:

    private void BtnUpload_Click(object sender, EventArgs e)
    {
        string domain2 = Domain + folderBrowserDialog1.SelectedPath;
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            if (Directory.Exists(Domain))
            {
                Directory.Move(folderBrowserDialog1.SelectedPath, domain2);
            }
            else
            {
                Directory.CreateDirectory(Domain + "\\projects");

            }
        }
        else
        {

        }
    }

Update

forget to say the following Domain is the seguine string

string Domain = AppDomain.CurrentDomain.BaseDirectory + "projects";

I made this little change.

    private void BtnUpload_Click(object sender, EventArgs e)
    {
        string domain2 = Domain + folderBrowserDialog1.SelectedPath;
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            if (Directory.Exists(Domain))
            {
                try
                {
                    Directory.Move(folderBrowserDialog1.SelectedPath, domain2);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Faild to move the file maybe it already exist");
                }
            }
            else
            {
                Directory.CreateDirectory(Domain + "\\projects");

            }
        }
        else
        {

        }
    }

continues with the same problem

  • I have had similar problem with an application in another language (I believe it is a Windows problem, if it is the same situation). Please check if this folder was not created with another user and is not only visible to the current user, which would prevent a new one from being created with the same name, but at the same time via code identifies it as if the folder did not exist. I know - it’s bizarre.

  • This difference of folders between users I noticed happens by running the application in Administrator mode and creating a folder that the non-administrative user cannot view.

1 answer

0

I’ve solved the problem. I was trying to move the folder to
Domain + folderBrowserDialog1.SelectedPath;
or whatever I was trying to do was this
C: File path moves to C: Directorioc: File path again.
This is the code

    private void BtnUpload_Click(object sender, EventArgs e)
    {
        string domain2 = Domain + "\\Edit";
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            if (Directory.Exists(Domain))
            {
                try
                {
                    Directory.Move(folderBrowserDialog1.SelectedPath, domain2);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Faild to move the file maybe it already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                Directory.CreateDirectory(Domain);
                try
                {
                    Directory.Move(folderBrowserDialog1.SelectedPath, domain2);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Faild to move the file maybe it already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        else
        {

        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.