Import and Save file in Visual Studio LightSwitch


Visual Studio Lightswitch is a Microsoft tool, which is used for to build business applications. If you want to browse a file in your LightSwitch application then follow these steps.

Step by step solution

Step 1 : Open Visual Studio LightSwitch->Create new table (like as FileInformation).

image1.png

Step 2 : Right click on screens->Add screens.

image2.png

Step 3 : Select editable grid screen->Select screen data (FileInformation)->Ok.

image3.png

Step 4 : Now click on add in screen commandbar->Select new button.

image4.png

Step 5 : Write method name (ImportFile)->Ok.

image5.png

Step 6 : Right click on ImportAFile button->Edit execute code->Write the below code.

image6.png

Code :

using
System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using LightSwitchApplication.UserCode;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using Microsoft.LightSwitch.Threading;
namespace LightSwitchApplication
{
    public partial class EditableFileInformationsGrid
    {
       partial void ImportAFile_Execute()
        {
             Dispatchers.Main.BeginInvoke(() =>
            {
                SelectFileWindow selectFileWindow = new SelectFileWindow();
                selectFileWindow.Closed += new EventHandler(selectFileWindow_Closed);
                selectFileWindow.Show();
            });
        }
        void selectFileWindow_Closed(object sender, EventArgs e)
        {
            SelectFileWindow selectFileWindow = (SelectFileWindow)sender;
          if (selectFileWindow.DialogResult == true && (selectFileWindow.DocumentStream != null))
            {
                byte[] fileData = new byte[selectFileWindow.DocumentStream.Length];
                using (StreamReader streamReader = new StreamReader(selectFileWindow.DocumentStream))
                {
                    for (int i = 0; i < selectFileWindow.DocumentStream.Length; i++)
                    {
                    fileData[i] = (byte)selectFileWindow.DocumentStream.ReadByte();
                    }
                }
                FileInformation fileInformation = this.DataWorkspace.ApplicationData.FileInformations.AddN();
                fileInformation.Name = selectFileWindow.SafeFileName;
                fileInformation.Miscellaneous = "Size of file in bytes: " + fileData.Length
                fileInformation.Data = fileData;
                selectFileWindow.DocumentStream.Close();
                selectFileWindow.DocumentStream.Dispose();
            }

        }
    }
}

Step 7 : Go to file view in solution explorer.

image7.png

Step 8 : Expand client->Right click on user code->Add->New item.

image8.png

Step 9 : Select text file->Write name (SelectFileWindow.xaml)->Add->Write the below code.

image9.png

Code :

<
UserControl x:Class="LightSwitchApplication.UserCode.SelectFileWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth
="400">
    <Grid x:Name="LayoutRoot" Background="White">
    </Grid>
</
UserControl>
<
controls:ChildWindow x:Class="LightSwitchApplication.UserCode.SelectFileWindow"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="394" Height="305"
           Title
="Select File Dialog" >
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23"
HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
        <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23"
HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
        <Button Content="Browse" Height="23" HorizontalAlignment="Left" Margin="291,92,0,0" Name="BrowseButton"
VerticalAlignment="Top" Width="75" Click="BrowseButton_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="66,92,0,0" Name="FileTextBox"
VerticalAlignment="Top" Width="219" IsEnabled="True"/>
    </Grid>
</
controls:ChildWindow>

Step 10 : Again right click on user code->Add->New item.

image8.png

Step 11 : Select class->Write name (SelectFileWindow.cs)->Add->Write the below code.

image10.png

Code :

using
System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace LightSwitchApplication.UserCode
{
    public partial class SelectFileWindow : ChildWindow
    {
        public SelectFileWindow()
        {
            InitializeComponent();
        }
        private FileStream documentStream;
        public FileStream DocumentStream
        {
            get { return documentStream; }
            set { documentStream = value; }
        }
        private String safeFileName;
        public String SafeFileName
        {
            get { return safeFileName; }
            set { safeFileName = value; }
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                this.FileTextBox.Text = openFileDialog.File.Name;
                this.safeFileName = openFileDialog.File.Name;
                this.FileTextBox.IsReadOnly = true;
                FileStream myStream = openFileDialog.File.OpenRead();
                this.documentStream = myStream;
            }
        }
    }
}

Step 12 : Now right click on client->Add references.

image11.png

Step 13 : Select System.Windows.Controls->Ok.

image12.png

Step 14 : Run application (Press F5)->Click on ImportAFile button.

image13.png

Step 15 : Now you will browse a file from system drive then Ok.

image14.png


Similar Articles