c# 枚举安卓系统中所有目录及文件名

using Android.App;
using Android.Widget;
using Android.OS;
using System.Runtime.InteropServices;

namespace App2
{
    [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        [DllImport("TestDynamicSharedLib.so")]
        public extern static int getClickCount();

        protected override void OnCreate(Bundle bundle)
        {
            var c = getClickCount();

            string folderPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
printAllSubFiles(folderPath); base.OnCreate(bundle); // Set our view from the "main" layout resource // SetContentView (Resource.Layout.Main); } void printAllSubFiles(string path) { System.Diagnostics.Debug.Print("dir:" + path); var dir = new System.IO.DirectoryInfo(path); try { foreach (var f in dir.GetFiles()) { System.Diagnostics.Debug.Print("file:" + f.FullName); } } catch { //throw; } try { foreach (var d in dir.GetDirectories()) { printAllSubFiles(d.FullName); } } catch (System.Exception) { //throw; } } } }

 如果是android 6.0 以上系统,须代码主动申请权限,此时需要Android.Support.v4。

nuget command : Install-Package Xamarin.Android.Support.v4 -Version 28.0.0.1

void checkPermission(string permissionName)
        {
            if (Android.Support.V4.App.ActivityCompat.CheckSelfPermission(this, permissionName)
                != (int)Android.Content.PM.Permission.Granted)
            {
                //用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释
                if (Android.Support.V4.App.ActivityCompat.ShouldShowRequestPermissionRationale(this, permissionName))
                    Toast.MakeText(this, "请开通相关权限,否则无法正常使用本应用!", ToastLength.Short).Show();

                //申请权限
                Android.Support.V4.App.ActivityCompat.RequestPermissions(this, new String[] { permissionName }, 0);
                Toast.MakeText(this, "请开通相关权限,否则可能无法正常使用本应用!", ToastLength.Short);
            }
            else
            {
                Toast.MakeText(this, "授权成功!", ToastLength.Short);
            }
        }

调用:

checkPermission(Android.Manifest.Permission.ReadExternalStorage);
checkPermission(Android.Manifest.Permission.WriteExternalStorage);

 

转载于:https://www.cnblogs.com/nanfei/p/10299458.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值