LS 存取文件

partial void GetFile_Execute()
        {
            // Download file from selected record.
            var doc = this.DocumentsSet.SelectedItem;
            if (doc == null) return;

            Dispatchers.Main.BeginInvoke(() =>
            {
                SaveFileDialog sfd = new SaveFileDialog();
                string defaultFilter = "Images *.jpg, *.png, *.gif|*.jpg;*.png;*.gif|Documents *.doc, *.pdf|*.doc;*.pdf|All *.*|*.*";
                if (!string.IsNullOrEmpty(doc.FileName))
                {
                    string ext = Path.GetExtension(doc.FileName);
                    sfd.DefaultExt = ext;
                    defaultFilter = string.Format("*.{0}|*.{0}|", sfd.DefaultExt) + defaultFilter;
                }
                sfd.Filter = defaultFilter;

                bool? selected = sfd.ShowDialog();
                if (selected.HasValue && selected.Value)
                {
                    string fileName = sfd.SafeFileName;
                    long len = 0;
                    using (var fs = sfd.OpenFile())
                    {
                        // Write all bytes in field to file..
                        fs.Write(doc.Blob, 0, doc.Blob.Length);
                        len = fs.Length;
                    }

                    this.ShowMessageBox("File Get: " + sfd.SafeFileName + "\nSize:" + len);

                    // Could open file if you knew the path, but SafeFileName does not give path.
                    // string path = @"c:\temp\" + fileName;
                    // var shell = AutomationFactory.CreateObject("Shell.Application");
                    // shell.ShellExecute("mspaint.exe", path, "", "open", 1);
                }
                else
                    this.ShowMessageBox("Cancelled");
            });
        }

        partial void EditFile_Execute()
        {
            // Upload file to selected records image field.
            var doc = this.DocumentsSet.SelectedItem;
            if (doc == null) return;

            Dispatchers.Main.BeginInvoke(() =>
            {
                OpenFileDialog ofd = new OpenFileDialog();

                bool? selected = ofd.ShowDialog();
                if (selected.HasValue && selected.Value)
                {
                    byte[] ba = null;
                    using (var fs = ofd.File.OpenRead())
                    {
                        ba = new byte[fs.Length];
                        int pos = 0;
                        while (pos < fs.Length)
                        {
                            int read = fs.Read(ba, pos, ba.Length - pos);
                            pos += read;
                        }
                        this.ShowMessageBox("File Put: " + fs.Name + "\nSize:" + fs.Length);
                    }

                    // Set document and doc name in fields. To be Saved by user.
                    doc.FileName = ofd.File.Name;
                    doc.Blob = ba;
                }
                else
                    this.ShowMessageBox("Cancelled");
            });
        }

原文地址:https://www.cnblogs.com/ysharp/p/2140731.html