Tuesday, March 27, 2012

LightSwitch desktop app needs dialog to select and copy a file

Problem Description

The user of a LightSwitch intranet application needs a button to select a file from his pc and copy it over to the Windows server where another process will pick it up and process it.

I have read a lot of articles about uploading downloading files in LightSwitch for web based and desktop based apps. Here is what works for me. The solution comes from an article by Dan Seefeldt.

Solution

The user needs a button on one of his screens with which he can select a file that will then be copied by the application into a particular location on a Windows server. Clicking on the button will open an open file dialog, after selecting the file and clicking on the Open button, the app will copy the selected file into another windows folder.

  1. to create the button on one of the screens, with the text ‘Load File’
    image
  2. In VS open the screen design, under ‘Rows layout / Screen command bar’ click on ‘+ Add’ to add a new button. the button is called ‘LoadFile’. By using this naming convention, it will be displayed with spaces between the words.
    image
  3. To add code behind the button, right click on the button ‘Load file’ and from the popup menu select ‘Edit Execute Code’. This will open the event handler code that handles the button click event.
    image
  4. the method LoadFile_Execute() will be created, I have added 2 using statements for the openfiledialog windows control and for the Ligthswitch threading. 

    using System.Windows.Controls;



    using Microsoft.LightSwitch.Threading;



    namespace LightSwitchApplication



    {



        public partial class EditablePersonnelsGrid



        {



            partial void LoadFile_Execute()



            {



                System.IO.FileInfo file = null;            // OpenFileDialog() must be opened on the UI thread



                Dispatchers.Main.Invoke(() =>



                {



                    OpenFileDialog dlg = new OpenFileDialog();



                    dlg.Filter = "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt";



                    if (dlg.ShowDialog() == true)



                    {



                        file = dlg.File;



                    }



                });



            }



        }



    }





  5. The previous code only shows the file object being selected, there is no code yet to copy the file to another folder. The code shown is rudimentary.







    if (dlg.ShowDialog() == true) 
    {
        file = dlg.File;
        string destfilename = System.IO.Path.Combine("H:\\downloads", file.Name);
        file.CopyTo(destfilename, true);
    }






Conclusion



The file copying works.



Important remark



The above code will work in the protected area of LightSwitch, for instance in My Documents on your pc, it will not work on an external or on network drive. To fix this problem I simply had to write an Windows Forms (or WPF) app that I launch from the LightSwitch command button, this app then opens the OpenFileDialog and copies the file.

3 comments:

Unknown said...

louis vuitton outlet, sac longchamp, ralph lauren pas cher, replica watches, louboutin outlet, louboutin shoes, christian louboutin outlet, prada outlet, ugg boots, longchamp pas cher, tiffany and co, louis vuitton, louboutin, air jordan pas cher, tory burch outlet, cheap oakley sunglasses, nike outlet, polo ralph lauren outlet, ray ban sunglasses, polo ralph lauren outlet, louis vuitton outlet, nike air max, replica watches, louis vuitton, air max, longchamp outlet, michael kors, oakley sunglasses, chanel handbags, nike free, nike roshe run, oakley sunglasses, burberry, tiffany jewelry, ray ban sunglasses, kate spade outlet, prada handbags, oakley sunglasses, nike air max, louis vuitton, louboutin pas cher, uggs on sale, ray ban sunglasses, oakley sunglasses, longchamp, jordan shoes, gucci outlet, ugg boots, nike free, longchamp outlet

Unknown said...

lancel, celine handbags, jimmy choo shoes, bottega veneta, nike roshe, asics running shoes, gucci, giuseppe zanotti, nike huarache, mcm handbags, herve leger, hollister, hollister, new balance, ray ban, p90x workout, soccer shoes, babyliss, longchamp, mac cosmetics, nike trainers, chi flat iron, mont blanc, vans, vans shoes, ghd, iphone cases, nike air max, hollister, ferragamo shoes, ralph lauren, louboutin, nike air max, beats by dre, valentino shoes, converse outlet, lululemon, north face outlet, instyler, soccer jerseys, birkin bag, insanity workout, baseball bats, north face outlet, abercrombie and fitch, timberland boots, reebok shoes, nfl jerseys, oakley, wedding dresses

Unknown said...

hollister, louis vuitton, canada goose outlet, moncler, pandora jewelry, swarovski, louis vuitton, bottes ugg, coach outlet, moncler, supra shoes, montre pas cher, moncler, ugg,uggs,uggs canada, ugg,ugg australia,ugg italia, canada goose, links of london, pandora jewelry, karen millen, doudoune canada goose, juicy couture outlet, moncler, pandora charms, marc jacobs, swarovski crystal, moncler, ugg pas cher, thomas sabo, louis vuitton, moncler outlet, moncler, canada goose, canada goose uk, canada goose outlet, ugg boots uk, juicy couture outlet, wedding dresses, moncler, canada goose, toms shoes, louis vuitton, replica watches, sac louis vuitton pas cher, pandora charms, canada goose