DEV Community

Cover image for Xamarin.Forms System.ArgumentException: Illegal characters in path
Matt Kenefick
Matt Kenefick

Posted on

Xamarin.Forms System.ArgumentException: Illegal characters in path

If you’ve seen this, you’ve probably been banging your head against the wall figuring out what it means. If you’re fairly sure there aren’t actual illegal characters in your project, then it’s likely about how long your path string becomes.

Error XARDF7004: System.ArgumentException: Illegal characters in path.
Enter fullscreen mode Exit fullscreen mode

Assuming you’re using Visual Studio on Windows 10 when encountering this issue, it's likely related to MAX_PATH which caps off at 260 characters on Windows due to compatibility.

How did I find out?

At first I thought it might be related to spaces in folders, but that couldn’t be possible since my primary user on my machine has a space in it. In such an instance, every project would fail.

Then I thought maybe it was an invisible character in a folder name, so I copied my solution straight to C:\ and it worked.

Then I thought maybe it was related to length, so I started creating subfolders. I found that the project build would fail around 65 characters for path. That didn’t make any sense so I assumed it had to be something deep inside that exceeded some predetermined length.

Eventually, I was able to get this error to show up:

Error XA4209: Failed to create JavaTypeInfo for class: Android.Support.V4.View.Accessibility.AccessibilityManagerCompat/ITouchExplorationStateChangeListenerImplementor due to System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
Enter fullscreen mode Exit fullscreen mode

This explicitly talks about 260 characters and 248 characters.

I continued researching and found this thread:

So this is definitely related to the MAX_PATH problem on Windows. This will hopefully be solved with up coming changes in .net and MSBuild which will remove the MAX_PATH as a limit [1]. This looks to be in d16 however it does require the user enable a setting on their machine via a Group Policy (see [1] for details).

After investigating that, it seems to be a pretty common issue:

The issue will be always present in Windows, to keep compatibility with old software. Use the NT-style name syntax "\?\D:\very long path" to workaround this issue.


To conclude, if you run into the Illegal characters in path issue when compiling Android in Xamarin and you search for “xamarin system.argumentexception illegal characters in path”, I hope you find this article and realize it’s actually about how many subfolders you have your project nested in.

Good luck.

Top comments (1)

uchitesting profile image

This is an important information to have.
Fortunately I was lucky enough to have my instructor tell it to us.
So I did not have to struggle with this one too much. But for sure not having this piece of info can make things difficult especially if you know about Windows 10 long filename support that supposedly supports over 32K chars. Thus making this option look unlikely.
Best regards.