0

I'm working on a project and made a code. This code does works fine on my laptop but when I tried to set the .exe file on an other desktop it would not work. After pressing the button it just crashed and closed itself. How could I fix this?

I have tried several thinks. Like copying the whole solution folder. I tried also to make an installer for the program. And I also tried to use the right framework what was used on the desktop. But all those efforts did not work at all. I have checked and in the Resources folder which is located in the solution all images are fine. Sorry for all the code perhaps it is a little mistake in a big code. (Perhaps it could be a problem what systems I am using)

MainWindow Xaml cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Drawing;

namespace Escapehaarlem
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    string hint = "";
    string verwijder = "";
    Window1 win1 = new Window1();


    bool FlagBeer1 = true;
    bool FlagBeer2 = true;
    bool FlagBeer3 = true;
    bool FlagBeer4 = true;
    bool FlagBeer5 = true;
    bool FlagBeer6 = true;

    int index = 1;


    public MainWindow()
    {
        InitializeComponent();
        if (System.Windows.Forms.Screen.AllScreens.Length <= index)
        {
            System.Windows.MessageBox.Show("Tweede scherm is niet aangesloten");
            System.Environment.Exit(1);
        }
        else
        {
            win1.WindowStartupLocation = WindowStartupLocation.Manual;
            System.Windows.Forms.Screen s = System.Windows.Forms.Screen.AllScreens[1];

            System.Drawing.Rectangle r = s.WorkingArea;
            win1.WindowState = System.Windows.WindowState.Normal;
            win1.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
            win1.Top = r.Top;
            win1.Left = r.Left;

            win1.Show();
            win1.WindowState = System.Windows.WindowState.Maximized;
        }

    }


    private void Verstuur_Click(object sender, RoutedEventArgs e)
    {
        hint = Textbox.Text;
        win1.UpdateLabel(hint);
        win1.achtergrondfoto();

        if (FlagBeer1 && FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer1 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer2 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer3 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer4 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer5 && FlagBeer6)
        {
            FlagBeer5 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer6)
        {
            FlagBeer6 = false;
            win1.RemoveImage();
        }

    }



    private void Verwijder_Click(object sender, RoutedEventArgs e)
    {
        Textbox.Text = verwijder;
        win1.UpdateLabel(verwijder);
        win1.Achtergrondfotoplaatsten();
    }

    private void Herstel_Click(object sender, RoutedEventArgs e)
    {           

            if (FlagBeer6 == false && FlagBeer5 == false && FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer6 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer5 == false && FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer5 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer4 = true;
                win1.HerstelImage();                    
            }
            else if (FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer3 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer2 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer1 == false)
            {
                FlagBeer1 = true;
                win1.HerstelImage();
            }         

    }
    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);

        App.Current.Shutdown();
    }
}
}

Secondwindow xaml cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
using Timer = System.Threading.Timer;


namespace Escapehaarlem
{
public partial class Window1 : Window
{

    bool FlagBeer1 = true;
    bool FlagBeer2 = true;
    bool FlagBeer3 = true;
    bool FlagBeer4 = true;
    bool FlagBeer5 = true;
    bool FlagBeer6 = true;


    bool Beer1 = true;
    bool Beer2 = true;
    bool Beer3 = true;
    bool Beer4 = true;
    bool Beer5 = true;
    bool Beer6 = true;



    private static System.Timers.Timer aTimer;

    public Window1()
    {
        InitializeComponent();
    }

    public void RemoveImage()
    {


        if (FlagBeer1 && FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer1 = false;
            Image.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer2 = false;
            Image1.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer3 = false;
            Image2.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer4 = false;
            Image3.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer5 && FlagBeer6)
        {
            FlagBeer5 = false;
            Image4.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer6)
        {
            FlagBeer6 = false;
            Image5.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
    }

    private void SetTimer()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(1000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
    }


    private void OnTimedEvent(Object source, ElapsedEventArgs e)
    {

        if (Beer1 && Beer2 && Beer3 && Beer4 && Beer5 && Beer6)
        {
            Beer1 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer2 && Beer3 && Beer4 && Beer5 && Beer6)
        {
            Beer2 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image1.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer3 && Beer4 && Beer5 && Beer6)
        {
            Beer3 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image2.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer4 && Beer5 && Beer6)
        {
            Beer4 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image3.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer5 && Beer6)
        {
            Beer5 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image4.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer6)
        {
            Beer6 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image5.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }


    }

    public void HerstelImage()
    {


        if (FlagBeer6 == false && FlagBeer5 == false  && FlagBeer4 == false  && FlagBeer3 == false  && FlagBeer2 == false  && FlagBeer1 == false)
        {
            FlagBeer6 = true;
            Image5.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer5 == false && FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer5 = true;
            Image4.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer4 = true;
            Image3.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer3 = true;
            Image2.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer2 = true;
            Image1.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer1 == false)
        {
            FlagBeer1 = true;
            Image.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
    }

    private void SetTimer1()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(1000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += Verlopenevent;
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
    }

    private void Verlopenevent(Object source, ElapsedEventArgs e)
    {
        if (Beer6 == false && Beer5 == false && Beer4 == false && Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer6 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image5.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer5 == false && Beer4 == false && Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer5 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image4.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer4 == false && Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer4 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image3.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer3 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image2.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer2 == false && Beer1 == false)
        {
            Beer2 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image1.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer1 == false)
        {
            Beer1 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
    }



    public void achtergrondfoto()
    {
        achtergrondfoto1.Source = null;
    }

    public void Achtergrondfotoplaatsten()
    {
        achtergrondfoto1.Source = new BitmapImage(new Uri(@"./Resources/escapehaarlem_logo1.png"));
    }

    public void UpdateLabel(string text)
    {
        lbl_Tekst.Text = text;
    }
}
}

Mainwindow xaml code

<Window x:Class="Escapehaarlem.MainWindow"
    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"
    xmlns:local="clr-namespace:Escapehaarlem"
    mc:Ignorable="d"
    Title="MainWindow" 
    Height="350" 
    Width="560" 
    ResizeMode="CanResizeWithGrip" 
    Topmost="False" WindowState="Normal" 
    WindowStartupLocation="CenterOwner" 
    Icon="./Resources/escapehaarlem_logo.ico"
    MinWidth="300"
    MinHeight="200">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.3*" />
        <RowDefinition Height="0.3*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="0.3*" />
    </Grid.RowDefinitions>
    <Button Name="Verstuur" Grid.Column="1" Margin="2" Click="Verstuur_Click">Verstuur</Button>
    <Button Name="Verwijder" Grid.Row="1" Grid.Column="2" Margin="2" Click="Verwijder_Click">Verwijder</Button>
    <Button Name="Herstel" Grid.Row="3" Grid.Column="2" Margin="2" Click="Herstel_Click">Herstel</Button>
    <TextBox MaxLength="185" FontSize="20"  TextWrapping="Wrap" Grid.RowSpan="4" Name="Textbox" Margin="2"></TextBox>
</Grid>

</Window>

Secondwindow xaml code

<Window x:Class="Escapehaarlem.Window1"
    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"
    xmlns:local="clr-namespace:Escapehaarlem"
    mc:Ignorable="d"
    Title="SecondWindow" Height="1024" Width="1280" ResizeMode="NoResize" Topmost="True" WindowState="Maximized" Icon="./Resources/escapehaarlem_logo.ico" WindowStartupLocation="Manual" WindowStyle="None">

<Grid Background="Black">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>


    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="0.3*" />
    </Grid.RowDefinitions>
    <Image Name="achtergrondfoto1" Grid.ColumnSpan="6" Source="./Resources/escapehaarlem_logo1.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <TextBlock Grid.ColumnSpan="6"  Name="lbl_Tekst" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="White" Height="1010" TextWrapping="Wrap" FontSize="130" FontFamily="Broken Planewing"/>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="5" Name="Image" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="4" Name="Image1" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="3" Name="Image2" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="2" Name="Image3" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="1" Name="Image4" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="0" Name="Image5" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
</Grid>
</Window>

Sorry for all the code but I have a feeling that it could be somethink small. I hope that it would be able to run on a other desktop.

Please let me know if any information is required

Thanks in advance.

  • Did you also copy the all of the DLL files? – Kristóf Tóth Feb 14 '19 at 12:12
  • 1
    There are a few preconditions of each application. First of all it is the .NET Framework itself that has to exist in the correct version on the target machine. Next, all referenced libraries (beyond .NET dlls) have to be copied also. Then you have to take care, if your application depends on any additional files like e. g. resource files. Ah and what I forgot nearly: catch the error, using a try/catch block, to find out what is missing! – Nicolas Feb 14 '19 at 12:14
  • @KristófTóth I made a zip file from the whole solution, so if it was in it, it would have copied them to. But when I look in the folder I can't seem to find any DLL – Bart franken Feb 14 '19 at 12:16
  • @Nicolas perhaps stupid qeustion but why do I not have DLL, I thougt there were made automatic and how could I add them? – Bart franken Feb 14 '19 at 12:17
  • @Bart you will get a dll if you create a library project, if not there isn't a custom dll for you. But if you added references to external dlls that don't belong to .NET, you will have to take them with your exe to the target machine. But instead to guess what is missing, just wrap your code in your button event handler into a try/catch block. – Nicolas Feb 14 '19 at 14:40

1 Answers1

0

You should copy all the binaries from the bin/{YourBuildConfiguration} folder as your application only copies the ones that is NEEDS, not just the .exe file.

Update as per your comment: You don't have to create a zip from a solution. A solution is to develop the code. Building a solution produces an application as an output, which output defaults to the {SolutionFolder}/{ProjectName}/bin/Debug or Release. You have to zip the contents of that folder, copy it to your desired place (maybe another machine) and the unzip and run the exe. All the necessary DLL-s should be right next to the .exe file which you want to run.

Try reproducing the steps mentioned in this questions answers.

Kristóf Tóth
  • 791
  • 4
  • 19
  • I did do that when I made an installer and when I used a zip file to tranfer it. As alternative options – Bart franken Feb 14 '19 at 12:19
  • Please provide a file list from your bin folder after you execute a solution build. I saw that you mentioned that there are no DLLs – Kristóf Tóth Feb 14 '19 at 12:44
  • app.publish folder within .exe file, program application manifest,.exe file, XML config file, Manifest-file and program debug – Bart franken Feb 14 '19 at 12:54