Move or copy a file with C#

Hi, I have a wombat component which does not work (error: Path access denied). I tried a different path but that results in the same error.

I believe this can also be done in C#, hoping that someone can help me with that.
I’m looking for a copy/move from path entry and a copy/move to path entry with a boolean to trigger it.

Thank you.

Since you are using Google Drive have you tried pausing the syncing of files while you need to save locally?

I tried to do it with a local path too, not related to google drive, but it results into the same error.

I don’t have high hopes for a C# script to succeed here, but you want to use File.Copy from the System.IO namespace.

2 Likes

I read in another post that indeed people referred to that as well. Unfortunately I don’t have any knowledge on how to write it. I was hoping people with more experience could help me with that tbh :slight_smile:

Why you don’t have hopes? Your suggestion works!
2021-09-18 22_25_17-Window

c# copy file.gh (5.1 KB)

using System.IO;

  private void RunScript(string from_path, string to_path, bool go, ref object A)
  {
    if(go){
      File.Copy(from_path, to_path);
    }
  }

I meant in the case of the OP. There already is an access denied problem - there may be a deeper problem. Of course, if it works it is great!

Thank you. Unfortunately it does not work as well. I’ve saved the grasshopper definition on my local drive too to test it out but the result is the same. Do you guys have any other possible solutions to try out?

As Nathan said, it’s a permission problem.
Where the .gh file is stored is irrelevant.
The problem is rhino (and so grasshopper, that execute the .gh code) that have not enough permissions to work with files in the machine.
Maybe OSx have ways to give more permissions to a program?
On windows you can “Run as Administrator” any program.
I know nothing about anything related to Apple.
Maybe open rhino with a sudo? :sweat_smile:

I see, hoping someone knows more about this! :slight_smile:

Rhino has full access to my filesystem. I can’t add Grasshopper in there I believe.
I’ve tried to open it with sudo but unfortunately the same result in grasshopper.

@dan, this sounds a bit like the user from last week with weird access problems. Was it for the user a case of rebooting his machine to get everything going again, or were there other steps he had to do first?

This is indeed odd. I’m unable to reproduce this behavior here. I can use the simple example @maje90 provided to copy a file, as expected.

If we’re thinking of the same case @nathanletwory, it’s the one where the user couldn’t copy the Rhino app bundle into /Applications, but could copy to ~/Desktop…that one? The user ended up just rebooting his machine and it worked fine.

I’m not sure what to try in this case.

I’ve restarted my MacBook a few times and tried it out on local paths too.
Here is the saved file, perhaps that is of any use.

test.gh (5.2 KB)

Perhaps not related but part of the definition also creates some empty folders. So writing to my filesystem is possible.

In your attached script you are copying folders. On Windows that gives the error message 1. The target file "C:\Users\Nathan\Downloads\B" is a directory, not a file. (line: 0).

Can you test with copying files instead of directories?

image

Hi Remy-

Thanks for the sample test.gh file.

I’ll just echo @nathanletwory 's comment here. In your test.gh, your paths are to folders, not files. The paths need to be files.

If that doesn’t work, then I suspect it has something to do with the creation of those folders using your C# script. As a test, if you create a folder using macOS’s Finder (not a C# script) does the File.Copy operation succeed (using a fully-expanded path to a file)?

hi guys,

It was my mistake as I only used the directory path instead of the file path. I look right over it (a bit dumb of me). I think I was thrown off by the error messages and went searching in the wrong direction. It works as expected now.

Thank you for your patiences and help. I appreciated it a lot!

2 Likes

Good to hear you can continue. Enjoy!