Пример #1
0
        public static void OpenFileByUser(object sender, OpenFileByUserEventArgs e)
        {
            var openFileDialog = new OpenFileDialog();

            openFileDialog.FileName         = e.FileName;
            openFileDialog.CheckFileExists  = true;
            openFileDialog.Multiselect      = false;
            openFileDialog.InitialDirectory = e.InitialDirectory;
            openFileDialog.Filter           = e.Filter;
            openFileDialog.FilterIndex      = e.FilterIndex;
            openFileDialog.RestoreDirectory = e.RestoreDirectory;

            e.Answer   = openFileDialog.ShowDialog();
            e.FileName = openFileDialog.FileName;
        }
Пример #2
0
        public bool NewOutputDevice(OutputDeviceTypeInfo outputDeviceTypeInfo, out IOutputDevice newOutputDevice)
        {
            if (outputDeviceTypeInfo == null)
            {
                throw new NullReferenceException();
            }

            newOutputDevice = outputDeviceTypeInfo != null?
                              OutputDeviceBuilder.GetNew(outputDeviceTypeInfo) : null;

            if (newOutputDevice as IFileOutputable != null)
            {
                var openingFileDialogArgs = new OpenFileByUserEventArgs()
                {
                    InitialDirectory = (MainForm.SelectedIDE.ProgramFile.FileName.IsNotNullOrEmpty())
                        ? Path.GetDirectoryName(MainForm.SelectedIDE.ProgramFile.FileName)
                        : (MainForm.SelectedIDE.ParentWorkplace.MainForm.RecentFiles.Count > 2)
                            ? Path.GetDirectoryName(MainForm.SelectedIDE.ParentWorkplace.MainForm.RecentFiles[0])
                            : Path.GetDirectoryName(Application.ExecutablePath),
                    Filter           = "Text Files (*.txt)|*.txt|Binary Files (*.bin)|*.txt|All Files (*.*)|*.*",
                    FilterIndex      = 1,
                    RestoreDirectory = true
                };

                OpeningFileByUser?.Invoke(this, openingFileDialogArgs);

                if (openingFileDialogArgs.Answer == DialogResult.OK)
                {
                    try
                    {
                        if (!(System.IO.File.Exists(openingFileDialogArgs.FileName) &&
                              (System.IO.File.GetAttributes(openingFileDialogArgs.FileName) & FileAttributes.ReadOnly) != 0))
                        {
                            (newOutputDevice as IFileOutputable).FileName = openingFileDialogArgs.FileName;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            if (newOutputDevice != null)
            {
                this.OutputDevices.Add(newOutputDevice);
                newOutputDevice.ParentWorkplace = this;
                newOutputDevice.Id = this.OutputDevices.Count - 1;
            }
            if (newOutputDevice as IInputDevice != null)
            {
                this.InputDevices.Add(newOutputDevice as IInputDevice);
            }
            return(true);
        }
Пример #3
0
        public bool NewInputDevice(InputDeviceTypeInfo inputDeviceTypeInfo, out IInputDevice newInputDevice)
        {
            if (inputDeviceTypeInfo == null)
            {
                throw new NullReferenceException();
            }

            newInputDevice = inputDeviceTypeInfo != null?
                             InputDeviceBuilder.GetNew(inputDeviceTypeInfo) : null;

            if (newInputDevice as IFileInputable != null)
            {
                var openingFileDialogArgs = new OpenFileByUserEventArgs()
                {
                    InitialDirectory = (MainForm.SelectedIDE.ProgramFile.FileName.IsNotNullOrEmpty())
                        ? Path.GetDirectoryName(MainForm.SelectedIDE.ProgramFile.FileName)
                        : (MainForm.SelectedIDE.ParentWorkplace.MainForm.RecentFiles.Count > 2)
                            ? Path.GetDirectoryName(MainForm.SelectedIDE.ParentWorkplace.MainForm.RecentFiles[0])
                            : Path.GetDirectoryName(Application.ExecutablePath),
                    Filter           = "Text Files (*.txt)|*.txt|Binary Files (*.bin)|*.txt|All Files (*.*)|*.*",
                    FilterIndex      = 3,
                    RestoreDirectory = true
                };

                OpeningFileByUser?.Invoke(this, openingFileDialogArgs);

                if (openingFileDialogArgs.Answer == DialogResult.OK)
                {
                    try
                    {
                        FileInfo fInfo = new FileInfo(openingFileDialogArgs.FileName);
                        if (fInfo.Exists)
                        {
                            (newInputDevice as IFileInputable).FileName = openingFileDialogArgs.FileName;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            if (newInputDevice != null)
            {
                this.InputDevices.Add(newInputDevice);
                newInputDevice.ParentWorkplace = this;
                newInputDevice.Id = this.InputDevices.Count - 1;
            }
            if (newInputDevice as IOutputDevice != null)
            {
                this.OutputDevices.Add(newInputDevice as IOutputDevice);
            }
            return(true);
        }