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);
复制代码

 

posted on   空明流光  阅读(280)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示