I’m trying to use System.Path.Combine(string, string), but the compiler says It’s inaccessible due to its protection level. Why?
I’m using Monodevelop on Mac. Latest Unity release. And Path.Combine works when in a different MonoDevelop solution.
I’m trying to use System.Path.Combine(string, string), but the compiler says It’s inaccessible due to its protection level. Why?
I’m using Monodevelop on Mac. Latest Unity release. And Path.Combine works when in a different MonoDevelop solution.
You might be inadvertently accessing a different Path class. Try using the full path to the method: string combinedPath = System.IO.Path.Combine(string1, string2);
Thanks for the suggestion, but the full path to the method isn’t working.
Elaborate?
Post your exact code, including the declarations of any variables that are being fed into the method.
Here is the exact code: http://pastie.org/private/xtuumlloxu7ziqi2t6uyog
If I create a new MonoDevelop solution using the Console Project template, System.IO.Path works correctly. So I would assume that the problem is something specific to the unity project itself.
configPath = System.IO.Path.Combine(Application.dataPath + "/Voxel/");
This is trying to access a single parameter Combine method. The public Combine method takes two parameters.
Ah, I see. I added the comma in place of the + operator and the problem disappeared. Thanks for the help
Autocomplete can catch issues like this. You might want to try visual studio as well as it lets you jump directly to the declaration/method
(Rez old thread that’s top search result with my problem. Hope this helps someone else.)
“Path.Combine is inaccessible due to its protection level” can also happen when using more than two arguments to Combine:
// works
string combinedPath = System.IO.Path.Combine(string1, string2);
// error
string combinedPath = System.IO.Path.Combine(string1, string2, string3);
You can either loop over them or use Aggregate:
using System.Linq; // required at top of file
string combinedPath = (new String[ ]{ string1, string2, string3 }).Aggregate(Path.Combine);
Seems this is because Unity uses a version of Mono that predates Combine that takes multiple arguments. Context and more information here.
(The specific error message for me on Unity 5 is especially confusing because it seems to recognize that Combine has overloads that take 3 args or take variable/param args.)
I know is old but is on top on Google search.
If the string2 contains a filename and extension, then Path.Combine
will not work
Example
string string2 = "\\StreamingAssets\\file\\myFile.exe"
@AlanMattano I think the issue is the slash \ at the start of the path.
Try like this:
exePath = System.IO.Path.Combine(Application.dataPath, "StreamingAssets/file/myFile.exe");