예제 #1
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");
            }
        }
예제 #2
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();
                }
            }
        }