question

WillAutioItrax avatar image
0 Votes"
WillAutioItrax asked RobCaplan edited

How to determine the Mono Android version for preprocessing?

I would like to use some code that replaces the deprecated method Environment.GetExternalStoragePublicDirectory and found some that uses a preprocessor statement

 #if MONOANDROID10_0

for android 10 and higher.
How does MONOANDROID10_0 get set? It must be based on the device that it is being run on, but how? Any insight would be appreciated.
Thanks

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered WillAutioItrax commented

Hello,​

Welcome to our Microsoft Q&A platform!

How does MONOANDROID10_0 get set? It must be based on the device that it is being run on, but how?

For this function, try to detect the value of Build.VERSION.SdkInt like below.

var versionNumber = Android.OS.Build.VERSION.SdkInt;
if (versionNumber >= Android.OS.BuildVersionCodes.Q)
{
    // Your code
}
else
{
    //perform the work
}


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@WillAutioItrax May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·

@JarvanZhang-MSFT
Thanks for your reply. Sorry - I did not get a notification of a reply so it was only now that I came upon it and was able to try it out.

Your solution works fine for determining what version the app is running on from within the program. What I have seen is a pre-processor flag that must get set by the device. I am wondering how that works.

Thanks,
Will

BTW, I have clicked "Follow" near the top of the page. I trust that I will get notified of any activity on this question.

0 Votes 0 ·

What I have seen is a pre-processor flag that must get set by the device.

Do you mean the C# preprocessor directives? It provides some conditional compilation. Here is the related documentation about the function, please check:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

0 Votes 0 ·