C# Notitzen

· von Daschmi

Externes Programm mit C# aufrufen und Rückgabe verarbeiten (Die Funktion sollte mittels try/catch behandelt werden)

  • Pfad zur Programmdatei in C# ermitteln
    string pathBase = System.Threading.Thread.GetDomain().BaseDirectory;

  • Externes Programm mit C# aufrufen und Rückgabe verarbeiten (Die Funktion sollte mittels try/catch behandelt werden)

    public string Externcall(string cmd, string parameter)
     {
    
     ProcessStartInfo procStartInfo = new ProcessStartInfo(cmd, parameter);
    
     procStartInfo.RedirectStandardOutput = true;
     procStartInfo.UseShellExecute = false;
     procStartInfo.CreateNoWindow = true;
    
     System.Diagnostics.Process proc = new System.Diagnostics.Process();
     proc.StartInfo = procStartInfo;
     proc.Start();
    
     string result = proc.StandardOutput.ReadToEnd();
    
     return result;
    
     } // public string Externcall(string cmd, string parameter)

  • ini Datei mit C# einlesen und verarbeiten (Im Beispiel m1_app.ini im Programmverzeichnis)

    
    [DllImport("kernel32")]
     private static extern int GetPrivateProfileString(string section,
     string key, string def, StringBuilder retVal, int size, string filePath);
    
     public string GetConf(string section, string key)
     {
    
     const int size = 255;
     string file = System.Threading.Thread.GetDomain().BaseDirectory + "m1_app.ini";
     string def = "";
    
     StringBuilder sb = new StringBuilder(size);
     GetPrivateProfileString(section, key, def, sb, size, file);
    
     return sb.ToString();
    
     } // public string GetConf(string section, string key)
    
    

  • Datei mit C# schreiben

    public void writeFile(string file, string content)
     {
    
     StreamWriter mySaveFile = new StreamWriter(file);
     mySaveFile.Write(content);
     mySaveFile.Close();
    
     } // void public writeFile(string file, string content)

  • Datei mit C# einlesen und zurückgeben

    
    public string readFile(string file)
     {
    
     return File.ReadAllText(file, Encoding.Default);
    
     } // public string readFile(string file)

  • String Ersetzung mit C#

    strConf = strConf.Replace("###WINVERSION###", strWinVersion);