示例#1
0
        internal static bool Exists(string path, HeadType expectedObjectType)
        {
            if (IsLocal(path))
            {
                switch (expectedObjectType)
                {
                case HeadType.Directory:
                    return(System.IO.Directory.Exists(path));

                case HeadType.File:
                    return(System.IO.File.Exists(path));

                default:
                    throw new Exception("The path <" + path +
                                        "> is local/UNC but no type switch (dir/file) provided.");
                }
            }
            bool found = false;

            string[] majorSegments;

            if (IsMtp(path, out majorSegments))
            {
                var pair = GetDriveByMtpPathSegments(majorSegments);
                using (/*device*/ pair.Item1)
                    using (var drive = pair.Item2)
                    {
                        var pathSegments =
                            majorSegments[3].Split(new[] { System.IO.Path.DirectorySeparatorChar },
                                                   StringSplitOptions.RemoveEmptyEntries);
                        WpdFilesystemItem target = drive;
                        var disposables          = new List <IDisposable>();
                        for (int i = 0; i < pathSegments.Length; i++)
                        {
                            var children = target.GetChildren();
                            disposables.AddRange(children);
                            target = children.FirstOrDefault(
                                c => c.Name.Equals(pathSegments[i]) &&
                                (expectedObjectType == HeadType.Directory
                                     ? c.IsFolder //all segments point to folders, even the last one
                                     : (expectedObjectType == HeadType.File
                                         ? (c.IsFolder == (i != pathSegments.Length - 1))
                                        //all folders except the last one, which is file
                                         : (c.IsFolder || (i == pathSegments.Length - 1)))));
                            //all folders, excet the last one, which can be both
                            if (target == null)
                            {
                                break;
                            }
                        }
                        found = target != null;
                        disposables.ForEach(d => d.Dispose());
                    }
            }
            return(found);
        }
示例#2
0
        public FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, ulong fileSizeForMtpWrite = 0)
        {
            string[] mtpSegments;
            if (Path.IsLocal(path))
            {
                _isMtp          = false;
                _baseFileStream = new System.IO.FileStream(path, mode, access, share);
            }
            else if (Path.IsMtp(path, out mtpSegments))
            {
                if (mode != System.IO.FileMode.Create && mode != System.IO.FileMode.CreateNew)
                {
                    throw new System.IO.IOException("Reading MTP is not supported yet");
                }
                if (fileSizeForMtpWrite == 0)
                {
                    throw new System.IO.IOException("Writing MTP requires projected file size");
                }

                var dName = Path.GetDirectoryName(path);
                if (Directory.Exists(dName))
                {
                    _isMtp = true;
                    var pair = Path.GetDriveByMtpPathSegments(mtpSegments);
                    _device = pair.Item1;
                    using (var drive = pair.Item2)
                    {
                        var pathSegments        = mtpSegments[3].Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                        var disposables         = new List <IDisposable>();
                        WpdFilesystemItem child = drive;
                        for (int i = 0; i < pathSegments.Length - 1; i++)
                        {
                            var children = child.GetChildren();
                            disposables.AddRange(children);
                            child = children.Single(c =>
                                                    c.IsFolder &&
                                                    c.Name.Equals(pathSegments[i], StringComparison.OrdinalIgnoreCase));
                        }
                        _baseProxy = child.CreateChildFile(Path.GetFileName(path), fileSizeForMtpWrite);
                        disposables.ForEach(d => d.Dispose());
                    }
                }
                else
                {
                    throw new System.IO.IOException("Part of the directory path <" + dName + "> is not found");
                }
            }
            else
            {
                throw new System.IO.IOException("The path <" + path + "> is not recognized as IO.Path or WPD.Path");
            }
        }
示例#3
0
 public static void CreateDirectory(string path)
 {
     if (Path.IsLocal(path))
     {
         System.IO.Directory.CreateDirectory(path);
         return;
     }
     string[] mtpSegments;
     if (Path.IsMtp(path, out mtpSegments))
     {
         var pair = Path.GetDriveByMtpPathSegments(mtpSegments);
         using (/*device*/ pair.Item1)
             using (var drive = pair.Item2)
             {
                 var  pathSegments = mtpSegments[3].Split(new [] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                 var  disposables = new List <IDisposable>();
                 bool creationMode = false;
                 WpdFilesystemItem child = drive, existingDir = null;
                 foreach (var segm in pathSegments)
                 {
                     if (!creationMode)
                     {
                         var children = child.GetChildren();
                         disposables.AddRange(children);
                         existingDir = children.FirstOrDefault(c =>
                                                               c.IsFolder &&
                                                               c.Name.Equals(segm, StringComparison.OrdinalIgnoreCase));
                     }
                     if (existingDir != null)
                     {
                         child = existingDir;
                     }
                     else
                     {
                         creationMode = true;
                         child        = child.CreateChildFolder(segm);
                         disposables.Add(child);
                     }
                 }
                 disposables.ForEach(d => d.Dispose());
             }
     }
     else
     {
         throw new System.IO.IOException("Path is not identified as IO.Path or MTP.Path");
     }
 }
示例#4
0
        public static string GetFileSystemOpenPath(string uioPath)
        {
            if (Path.IsLocal(uioPath))
            {
                return(uioPath);
            }

            string[] mtpSegments;
            if (!Path.IsMtp(uioPath, out mtpSegments))
            {
                throw new System.IO.IOException("Path is not identified as IO.Path or MTP.Path");
            }

            var finalPath = new StringBuilder(@"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\");
            var pair      = Path.GetDriveByMtpPathSegments(mtpSegments);

            using (/*device*/ pair.Item1)
                using (var drive = pair.Item2)
                {
                    finalPath.Append(pair.Item1.GetId())
                    .Append('\\')
                    .Append(pair.Item2.Name)
                    .Append('\\');
                    var pathSegments        = mtpSegments[3].Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                    var disposables         = new List <IDisposable>();
                    WpdFilesystemItem child = drive;
                    foreach (var segm in pathSegments)
                    {
                        var children = child.GetChildren();
                        disposables.AddRange(children);
                        child = children.Single(c => c.IsFolder &&
                                                c.Name.Equals(segm, StringComparison.OrdinalIgnoreCase));
                        finalPath.Append(child.Name).Append('\\');
                    }
                    disposables.ForEach(d => d.Dispose());
                }
            return(finalPath.ToString());
        }
示例#5
0
        private static string[] GetDirectoryItems(string uioPath, bool directories)
        {
            uioPath = uioPath.TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar);
            string[] mtpSegments;
            if (!Path.IsMtp(uioPath, out mtpSegments))
            {
                throw new System.IO.IOException("Path is not identified as IO.Path or MTP.Path");
            }

            var pair = Path.GetDriveByMtpPathSegments(mtpSegments);

            using (/*device*/ pair.Item1)
                using (var drive = pair.Item2)
                {
                    var pathSegments        = mtpSegments[3].Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                    var disposables         = new List <IDisposable>();
                    var result              = new List <string>();
                    WpdFilesystemItem child = drive;
                    for (int i = 0; i <= pathSegments.Length; i++)
                    {
                        var children = child.GetChildren();
                        disposables.AddRange(children);
                        if (i < pathSegments.Length)
                        {
                            child = children.Single(c => c.IsFolder &&
                                                    c.Name.Equals(pathSegments[i],
                                                                  StringComparison.OrdinalIgnoreCase));
                        }
                        else
                        {
                            result.AddRange(children.Where(c => c.IsFolder == directories)
                                            .Select(c => uioPath + System.IO.Path.DirectorySeparatorChar + c.Name));
                        }
                    }
                    disposables.ForEach(d => d.Dispose());
                    return(result.ToArray());
                }
        }
示例#6
0
        private static void InteropTest()
        {
            //const string path = @"\Mike's Lumia\Phone\Documents\New Text Document.txt";
            const string path     = @"\Mike's MX5\Память телефона\tmp\New Folder";
            var          segments = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

            using (var managert = new Manager())
            {
                var devices = managert.GetDevices();
                var device  = devices.Single(d => d.Name == segments[0]);
                var drives  = device.GetStorage();
                WpdFilesystemItem target = drives.Single(d => d.Name == segments[1]);
                var disposables          = new List <WpdFilesystemItem>();
                var children             = target.GetChildren();
                target = children.Single(c => c.Name == segments[2]);
                disposables.AddRange(children);

                const string content   = "This is a text file!";
                var          binary    = System.Text.Encoding.UTF8.GetBytes(content);
                var          fileProxy = target.CreateChildFile("TestFile from C#.txt", (ulong)binary.Length);
                fileProxy.WriteContent(binary, (uint)binary.Length);
                var item = fileProxy.CommitAndDispose();
                item.Dispose();

                foreach (var disposable in disposables)
                {
                    disposable.Dispose();
                }
                foreach (var drive1 in drives)
                {
                    drive1.Dispose();
                }
                foreach (var device1 in devices)
                {
                    device1.Dispose();
                }
            }
        }
示例#7
0
        internal static void Delete(string path, bool dir, bool recursive4Dirs = true)
        {
            if (IsLocal(path))
            {
                if (dir)
                {
                    System.IO.Directory.Delete(path, recursive4Dirs);
                }
                else
                {
                    System.IO.File.Delete(path);
                }
            }

            string[] segments;
            if (!IsMtp(path, out segments))
            {
                throw new System.IO.IOException("Path is not identified as IO.Path or MTP.Path");
            }

            var pair = GetDriveByMtpPathSegments(segments);

            if (pair == null)
            {
                return;               //no such device or disk available anymore
            }
            using (/*device*/ pair.Item1)
                using (var drive = pair.Item2)
                {
                    var pathSegments = segments[3].Split(new[] { System.IO.Path.DirectorySeparatorChar },
                                                         StringSplitOptions.RemoveEmptyEntries);
                    WpdFilesystemItem target = drive;
                    var disposables          = new List <IDisposable>();
                    for (int i = 0; i < pathSegments.Length; i++)
                    {
                        var children = target.GetChildren();
                        disposables.AddRange(children);
                        target = children.FirstOrDefault(
                            c => c.Name.Equals(pathSegments[i]) &&
                            c.IsFolder ||
                            (!dir && i == pathSegments.Length - 1));
                        //all folders, excet the last one, which depends on the switch
                        if (target == null)
                        {
                            break;
                        }
                    }
                    Action disposeAll = () => disposables.ForEach(d => d.Dispose());
                    if (target != null)
                    {
                        if (target == drive)
                        {
                            disposeAll();
                            throw new System.IO.IOException("Cannot delete a drive on MTP device");
                        }
                        if (dir && !recursive4Dirs)
                        {
                            var temp = target.GetChildren();
                            if (temp.Length > 0)
                            {
                                disposables.AddRange(temp);
                                disposeAll();
                                throw new System.IO.IOException("The target directory contains child entries!");
                            }
                        }
                        target.Delete();
                    }
                    disposeAll();
                }
        }