- Home /
System.IO missing methods (Path.GetExtension() and more)
Hi,
I want to use the following method in a script : http://docs.unity3d.com/ScriptReference/Path.GetExtension.html
In Mono I imported the related namespaces but I get the following error when I try to use Path.GetExtension() :
error CS0117 : 'Path' does not contain a definition for 'GetExtension'
How can I solve this ? I'm not very familiar with this kind of problems and I didn't find any satisfying solution online for the moment...
Answer by OswaldP · Feb 19, 2016 at 02:21 PM
Ok after several hours of searching I figured out that I was able to use the method by typing the whole path of the method (inside the runtime directory) :
using System.IO;
void Start()
{
//Absolute path to the method (working, no need to import the System.IO namespace) :
System.IO.Path.GetExtension("file.jpg");
//Relative path to the method (error, even if the namespace has been imported) :
Path.GetExtension("file.jpg");
}
I should add that I still don't know why the "relative" path method call is not working, and I'd be glad to know !
Do you have any other includes or classes that are named Path? This could have a conflict with System.IO.Path, in which case it would take the other class which wouldn't have the GetExtension method.
Exactly, that's usually the reason for such errors. Things declared in the project itself usually takes precedence of things in other namespaces. So if there's a Path class (or any other type definition called "Path") the compiler will use that ins$$anonymous$$d of things declared in other namespaces.
Imported namespaces are always checked last. Likewise if you import two namespaces which both contains a class with the same name, the compiler can't tell which one you want to use as two imported namespaces have the same priority. A common example in Unity is the "Random" class. Unity's Random class is declared in the UnityEngine namespace which is imported by default. However the System namespace also has a Random class. So if you have a using UnityEngine;
and a using System;
in the same script and you try to use "the" / "a" Random class the compiler will throw an error "ambiguous reference".
Same problem exists with UnityEngine.Object
and System.Object
if you import both namespaces.
Thank you both for your answers. That was the problem indeed, one of the assets I imported must contains a different Path class. When I use the method in an empty project it works perfectly fine.