Vorlesungen
  Download     DBS     Sprachen     Oberflächen     File Formats     Impressum     Datenschutz  
1. Sem
2. Sem
3. Sem
4. Sem
5. Sem
Wahlpflicht-SoSe
Wahlpflicht-WiSe
Projektwochen
Android mit Kotlin und JetPack
WPF
iOS mit Swift
Design Pattern
Winforms
Java Collection Framework
Core Wars
Einführung in GUI
Sprechen Sie D?
HTML, CSS, Formulare und PHP
Internationalisierung von Programmen
Allgemein:
Startseite
Vorlesungen
Labore
Sinnvolle Programme
Lineare Regression
GUI-Entwurfsarbeit
Single-Format
Design Pattern-Termine
Observer1
Bsp2
Json-Array
Json-Dialogelemente
Webtechnologien

Basis.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



using System.Windows;  // MessageBox
using Microsoft.Win32;

using System.IO;
using System.Windows.Controls;

namespace <b>PROJECT</b> {
    public class Basis {


        public static void setWindowPositionFromRegistry(Window dialog, String regadr, String title) {
            // 1:  ein Bildschirm nur PrimaryScreenWidth/PrimaryScreenHeight
            // 2:  zwei Bildschirme  VirtualScreenWidth  0 bis Width(1)+Width(2)
            // 3:  zwei Bildschirme  VirtualScreenWidth mit 0 bis Width(1)+Width(2)
            //             width ist aber   -Width(2) bis Width(1)
            int iTop = Basis.ReadRegInt(regadr, title + "_Top1");
            int iLeft = Basis.ReadRegInt(regadr, title + "_Left1");
            int iWidth = Basis.ReadRegInt(regadr, title + "_Width1");
            int iHeight = Basis.ReadRegInt(regadr, title + "_Height1");
            int w = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
            int h = (int)System.Windows.SystemParameters.PrimaryScreenHeight;

            int virtualLeft = (int)System.Windows.SystemParameters.VirtualScreenLeft;
            int virtualW = (int)System.Windows.SystemParameters.VirtualScreenWidth;  // beide Breiten zusammen positiv
            int virtualH = (int)System.Windows.SystemParameters.VirtualScreenHeight;
            // iLeft kann negativ sein   und zwar  -SecondWidth
            if (iTop > 0 && (iTop - (iHeight >> 1) <= virtualH) && (iLeft >= virtualLeft) && (iLeft <= virtualLeft + virtualW) && iWidth > 50) {
                dialog.Top = iTop;
                dialog.Left = iLeft;
                dialog.Width = iWidth;
                dialog.Height = iHeight;
            }
            else {
                dialog.Top = 100;
                dialog.Left = 20;
            }
        }  // setWindowPositionFromRegistry

        public static void setWindowPositionFromRegistryWidthHeight(Window dialog, String regadr, String title) {
            // 1:  ein Bildschirm nur PrimaryScreenWidth/PrimaryScreenHeight
            // 2:  zwei Bildschirme  VirtualScreenWidth  0 bis Width(1)+Width(2)
            // 3:  zwei Bildschirme  VirtualScreenWidth mit 0 bis Width(1)+Width(2)
            //             width ist aber   -Width(2) bis Width(1)
            int iWidth = Basis.ReadRegInt(regadr, title + "_Width1");
            int iHeight = Basis.ReadRegInt(regadr, title + "_Height1");
            int w = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
            int h = (int)System.Windows.SystemParameters.PrimaryScreenHeight;

            int virtualW = (int)System.Windows.SystemParameters.VirtualScreenWidth;  // beide Breiten zusammen positiv
            int virtualH = (int)System.Windows.SystemParameters.VirtualScreenHeight;
            // iLeft kann negativ sein   und zwar  -SecondWidth
            if (iWidth > 50) {
                dialog.Width = iWidth;
                dialog.Height = iHeight;
            }
            else {
                dialog.Top = 100;
                dialog.Left = 20;
            }
        }  // setWindowPositionFromRegistry



        public static void writeWindowPosition2Registry(Window dialog, String regadr, String title) {
            Basis.WriteRegInt(regadr, title + "_Top1", (int)dialog.Top);
            Basis.WriteRegInt(regadr, title + "_Left1", (int)dialog.Left);
            Basis.WriteRegInt(regadr, title + "_Width1", (int)dialog.Width);
            Basis.WriteRegInt(regadr, title + "_Height1", (int)dialog.Height);
        }  // writeWindowPosition2Registry



        /// <summary>
        /// liefert den Programmpfad mit Backslash, z. B. fuer das Erstellen von Ordnern
        /// </summary>
        /// <returns></returns>
        public static String getProgPath() {
            string[] args = System.Environment.GetCommandLineArgs();
            String exepath = args[0];
            int i = exepath.LastIndexOf("\\");
            String path = exepath.Substring(0, i + 1);
            if (!path.EndsWith("\\")) path += "\\";
            return path;
        }


        /// <summary>
        /// Kapselt YesNoCancel
        /// MessageBoxResult result = Basis.readYesNoCancel("Platte formatieren","Virus");
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static MessageBoxResult readYesNoCancel(string message, string title) {
            return MessageBox.Show(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);   // Exclamation
        }

        /// <summary>
        /// Kapselt YesNo
        /// bool result = Basis.readYesNo("Platte formatieren","Virus");
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static bool readYesNo(string message, string title) {
            return MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
        }

        /// <summary>
        /// Kapselt die Ausgabe einer Meldung
        /// Basis.Message("Platte formatieren","Virus");
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static void Message(String text, String title) {
            MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information);
        }

        /// <summary>
        /// Kapselt die Ausgabe einer Fehlermeldung
        /// Basis.ErrorMsg("Platte formatieren","Virus");
        /// </summary>
        /// <param name="text"></param>
        /// <param name="title"></param>
        public static void ErrorMsg(String text, String title) {
            MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error);
        }


        /// <summary>
        /// Automatisches Anlegen der kompletten Pfade
        /// if (!Basis.createPath("c:\\daten\\test"))  Fehler
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool createPath(String path) {
            try {
                Directory.CreateDirectory(path);
                return true;
            }
            catch {
                return false;
            }
        }



        /// <summary>
        /// einlesen eines String aus der Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static String ReadRegString(string root, string KeyName) {
            string sKey;
            RegistryKey regist;
            try {
                regist = Registry.CurrentUser.OpenSubKey(root);
                if (regist == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return "";
                }
                else {
                    if (regist.GetValue(KeyName) == null)
                        sKey = "";
                    else {
                        sKey = regist.GetValue(KeyName).ToString();
                    }
                    return sKey;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                Basis.ErrorMsg(e.ToString(), "Writing registry " + KeyName.ToUpper());
                return "";
            }
        }  // readRegString

        /// <summary>
        /// einlesen eines Long aus der Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static long ReadRegLong(string root, string KeyName) {
            long lKey;
            string sKey;
            RegistryKey regist;
            try {
                regist = Registry.CurrentUser.OpenSubKey(root);
                if (regist == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return 0;
                }
                else {
                    if (regist.GetValue(KeyName) == null)
                        lKey = 0;
                    else {
                        sKey = regist.GetValue(KeyName).ToString();
                        lKey = (long)Convert.ToUInt64(sKey);
                    }
                    return lKey;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Reading registry long" + KeyName.ToUpper());
                return 0;
            }
        }  // readRegLong

        /// <summary>
        /// einlesen eines Int aus der Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static int ReadRegInt(string root, string KeyName) {
            int lKey;
            string sKey;
            RegistryKey regist;
            try {
                regist = Registry.CurrentUser.OpenSubKey(root);
                if (regist == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return 0;
                }
                else {
                    if (regist.GetValue(KeyName) == null)
                        lKey = 0;
                    else {
                        sKey = regist.GetValue(KeyName).ToString();
                        lKey = (int)Convert.ToInt32(sKey);
                        //.ToUInt64(sKey);
                    }
                    return lKey;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Reading registry int" + KeyName.ToUpper());
                return 0;
            }
        }  // readRegInt

        /// <summary>
        /// einlesen eines ULong aus der Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static ulong ReadRegULong(string root, string KeyName) {
            string sKey;
            ulong lKey = 0;
            RegistryKey regist;
            try {
                regist = Registry.CurrentUser.OpenSubKey(root);
                if (regist == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return 0;
                }
                else {
                    if (regist.GetValue(KeyName) == null)
                        lKey = 0;
                    else {
                        sKey = regist.GetValue(KeyName).ToString();
                        lKey = Convert.ToUInt64(sKey);
                    }
                    return lKey;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Reading registry double" + KeyName.ToUpper());
                return 0;
            }
        }  // readRegLong


        /// <summary>
        /// einlesen eines Double aus der Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static double ReadRegDouble(string root, string KeyName) {
            string sKey;
            double dKey = 0;
            RegistryKey regist;
            try {
                regist = Registry.CurrentUser.OpenSubKey(root);
                if (regist == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return 0.0;
                }
                else {
                    if (regist.GetValue(KeyName) == null)
                        dKey = 0.0;
                    else {
                        sKey = regist.GetValue(KeyName).ToString();
                        dKey = Convert.ToDouble(sKey);
                    }
                    return dKey;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Reading registry double" + KeyName.ToUpper());
                return 0.0;
            }
        }  // readRegDouble

        /// <summary>
        /// lesen eines Boolean aus der Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static bool ReadRegBool(string root, string KeyName) {
            bool bKey;
            RegistryKey regist;
            try {
                regist = Registry.CurrentUser.OpenSubKey(root);
                if (regist == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return false;
                }
                else {
                    if (regist.GetValue(KeyName) == null)
                        bKey = false;
                    else {
                        int value = (int)regist.GetValue(KeyName);
                        bKey = value == 1;
                    }
                    return bKey;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Reading registry bool" + KeyName.ToUpper());
                return false;
            }
        }  // readRegBool


        // WriteWriteWriteWriteWriteWriteWriteWriteWriteWriteWriteWrite

        /// <summary>
        /// schreiben eines String in die Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteRegString(string root, string KeyName, string Value) {
            try {
                RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
                if (rKey == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return false;
                }
                else {
                    rKey.SetValue(KeyName, Value, RegistryValueKind.String);
                    return true;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Writing registry string" + KeyName.ToUpper());
                return false;
            }
        }


        /// <summary>
        /// schreiben eines ULong in die Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteRegULong(string root, string KeyName, ulong Value) {
            try {
                RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
                if (rKey == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return false;
                }
                else {
                    rKey.SetValue(KeyName, Value, RegistryValueKind.DWord);
                    return true;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Writing registry long" + KeyName.ToUpper());
                return false;
            }
        }

        /// <summary>
        /// schreiben eines Int in die Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteRegInt(string root, string KeyName, int Value) {
            try {
                RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
                if (rKey == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return false;
                }
                else {
                    rKey.SetValue(KeyName, Value, RegistryValueKind.DWord);
                    return true;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Writing registry int" + KeyName.ToUpper());
                return false;
            }
        }


        /// <summary>
        /// schreiben eines Double in die Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteRegDouble(string root, string KeyName, double Value) {
            try {
                RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
                if (rKey == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return false;
                }
                else {
                    rKey.SetValue(KeyName, Value.ToString(), RegistryValueKind.String);
                    return true;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Writing registry double" + KeyName.ToUpper());
                return false;
            }
        }


        /// <summary>
        /// schreiben eines Bool in die Registry
        /// </summary>
        /// <param name="root"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteRegBool(string root, string KeyName, bool Value) {
            try {
                RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
                if (rKey == null) {
                    //MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
                    return false;
                }
                else {
                    if (Value)
                        rKey.SetValue(KeyName, 1, RegistryValueKind.DWord);  // Bool geht nicht                    
                    else
                        rKey.SetValue(KeyName, 0, RegistryValueKind.DWord);  // Bool geht nicht                    
                    return true;
                }
            }
            catch (Exception e) {
                // OOOOH, an Error
                ErrorMsg(e.ToString(), "Writing registry bool" + KeyName.ToUpper());
                return false;
            }
        }


        /// <summary>
        /// Kapselt das Einlesen, konvertieren inkl. Fehlermeldung
        /// if (!Basis.tryDecimal("Fehlerhafte Zahl",tInput, out ergebnis))
        ///     return
        /// </summary>
        /// <param name="error"></param>
        /// <param name="tItem"></param>
        /// <param name="deci"></param>
        /// <returns></returns>
        public static bool tryDecimal(String error, TextBox tItem, out decimal deci) {
            if (!decimal.TryParse(tItem.Text.Trim().Replace(',', '.'), System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out deci)) {
                Basis.ErrorMsg(error, "Hinweis");
                tItem.Focus();
                return false;
            }
            if (deci < 0.0M) {
                Basis.ErrorMsg("Bitte geben Sie einen positiven Wert ein", "Hinweis");
                tItem.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// Kapselt das Einlesen, konvertieren inkl. Fehlermeldung
        /// if (!Basis.tryDecimal("Fehlerhafte Zahl",tInput, out ergebnis))
        ///     return
        /// </summary>
        /// <param name="error"></param>
        /// <param name="tItem"></param>
        /// <param name="deci"></param>
        /// <returns></returns>
        public static bool tryDecimalPosNeg(String error, TextBox tItem, out decimal deci) {
            if (!decimal.TryParse(tItem.Text.Trim().Replace(',', '.'), System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out deci)) {
                Basis.ErrorMsg(error, "Hinweis");
                tItem.Focus();
                return false;
            }
            return true;
        }

        public static void ShowExplorer(String path) {
            Process Prog = new Process();
            Prog.StartInfo.FileName = "Explorer.exe";
            if (path.Length > 0)
                Prog.StartInfo.Arguments = "/e," + path;
            else
                Prog.StartInfo.Arguments = "/e,";
            Prog.Start();
        }


        public static String getTempFile() {
            return System.IO.Path.GetTempFileName();
        }

        public static String getTemporaryFile() {
            String filename = System.IO.Path.GetTempFileName();
            FileInfo fileInfo = new FileInfo(filename);

            // Set the Attribute property of this file to Temporary. 
            // Although this is not completely necessary, the .NET Framework is able 
            // to optimize the use of Temporary files by keeping them cached in memory.
            fileInfo.Attributes = FileAttributes.Temporary;

            return filename;
        }

        /// <summary>
        /// Ausgabe des aktuellen Datum im Stringformat
        /// String s = Basis.NowDate()  // 01.02.2042
        /// </summary>
        /// <returns></returns>
        public static string NowDate() {
            return System.DateTime.Now.Day.ToString("00") + "." + System.DateTime.Now.Month.ToString("00") + "." + System.DateTime.Now.Year;
            //return 2012;
        }

        public static String NowdateYYMMDDMHHMMSS() {
            int year = Basis.NowYear();
            int month = Basis.NowMonth();
            int day = Basis.NowDay();
            int std = Basis.NowStd();
            int minute = Basis.NowMinute();
            return "_" + year.ToString("0000") + "_" + month.ToString("00") + "_" + day.ToString("00") + "_" + std.ToString("00") + "_" + minute.ToString("00") + "\\";
        }



        public static string NowDateYY() {
            return System.DateTime.Now.Day.ToString("00") + "." + System.DateTime.Now.Month.ToString("00") + "." + (System.DateTime.Now.Year % 100).ToString("00");
            //return 2012;
        }
        public static string NowDateYYYYMMDD() {
            return System.DateTime.Now.Year + "." + System.DateTime.Now.Month.ToString("00") + "." + System.DateTime.Now.Day.ToString("00");
            //return 2012;
        }

        /// <summary>
        /// split a filename into path, name and extention ( C:\\daten\\,  test1,  .asm )
        /// </summary>
        /// <param name="sPathFilename"></param>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static void splitFilename(String sPathFilename, out String path, out String name, out String ext) {
            String filename = sPathFilename;

            int k = -1;
            if (filename.IndexOf("\\") >= 0) {
                k = filename.LastIndexOf('\\');
            }
            else if (filename.IndexOf("/") >= 0) {
                k = filename.LastIndexOf('/');
            }

            if (k >= 0) {
                path = filename.Substring(0, k + 1);
                filename = filename.Substring(k + 1);
            }
            else {
                path = "";
            }
            k = filename.LastIndexOf('.');
            if (k >= 0) {
                name = filename.Substring(0, k);
                ext = filename.Substring(k);  // .asm
            }
            else {
                name = filename;
                ext = "";
            }
        }  // splitFilename


    }
}


MessageBox
Dialoge