getting System.IO.DirectoryNotFoundException: 'Could not find a part of the path '/C:\'.' in Xamarin

Tom 21 Reputation points
2021-04-05T02:05:37.927+00:00

I am using visual studio 2019 and cannot read the list of files in a directory. I have changed my code to keep it very simple with the same error.
I keep gettin this error: System.IO.DirectoryNotFoundException: 'Could not find a part of the path '/C:\'.' not sure why

Here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.IO;

namespace HMSsystem.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PDFViews : ContentPage
{
public PDFViews()
{
InitializeComponent();
}

    private async void OnPDBBtnClicked(object sender, EventArgs e)
    {
        var Paths = new List<String>();

        //string path = "C:\\";


        foreach (string path in Directory.EnumerateFiles("C:\\"))
        {
            Console.WriteLine("IN C DIRECTORY: " + path);
        }

        //DirectoryInfo dir = new DirectoryInfo(path);
        //foreach (FileInfo flInfo in dir.EnumerateFiles("*",SearchOption.AllDirectories))
        //{
        //    String name = flInfo.Name;
        //    long size = flInfo.Length;
        //    DateTime creationTime = flInfo.CreationTime;
        //    Paths.Add(name);
        //}


        //if (Directory.Exists(path))
        //{
        //    foreach (var file1 in System.IO.Directory.GetFiles(path))
        //    {
        //        if (file1 != null)
        //        {
        //            Paths.Add(file1);
        //        }
        //    }
        //}

        await Shell.Current.GoToAsync("//LoginPage");


    }

}

}
Any help would be appreaciated
Thank You
Tom

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-04-06T03:00:35.32+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The directory path is totally different on each platform .

    Android

    We access file in Internal Storage and External Storage ,check here .

    iOS

    The file is stored in sandbox directory , check here .

    Refer to https://stackoverflow.com/a/61056422/8187800 .

    None of them contains the path "C:\\" , but we can access the path from UWP(desktop application access C disk) .

    ---

    UWP

    Plsease check the usage in this link.

    In my test

    84679-capture.png

    But first we need to enable the permission.


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.