Username:   Password:  

Store settings in XML file in C#

using System;
using System.IO;
using System.Xml;
 
namespace SomeNamespace
{
    static class SettingsProvider
    {
        private const string CONFIG_FILE = "config.xml";
        private static XmlDocument _xmlDoc = null;
 
 
        private static string ConfigPath
        {
            get { return Program.ApplicationFolder + @"\" + CONFIG_FILE; }
        }
 
 
        public static void Save(string key, string value)
        {
            XmlNode node = _xmlDoc.SelectSingleNode("./config/" + key);
 
            if (node != null)
            {
                node.InnerText = value;
                Save();
            }
            else
            {
                // XML node doesnt exist thus create a new one
                Get(key, value);
            }
        }
 
        /// <summary>
        /// Gets a value of the settings. If the setting does not exist, defaultValue is returned.
        /// </summary>
        public static string Get(string key, string defaultValue)
        {
            if (_xmlDoc == null)
            {
                _xmlDoc = new XmlDocument();
 
                // Load config.xml
                string fullPath = SettingsProvider.ConfigPath;
 
                if (!File.Exists(fullPath))
                {
                    // Xml declaration
                    XmlDeclaration declaration = _xmlDoc.CreateXmlDeclaration("1.0", null, null);
                    _xmlDoc.AppendChild(declaration);
 
                    // Root node <config>
                    XmlElement rootNode = _xmlDoc.CreateElement("config");
                    _xmlDoc.AppendChild(rootNode);
 
                    Save();
                }
                else
                {
                    _xmlDoc.Load(fullPath);
                }
            }
 
            XmlNode node = _xmlDoc.SelectSingleNode("./config/" + key);
 
            if (node != null)
            {
                return node.InnerText;
            }
            else
            {
                // Add default value
                XmlElement newSetting = _xmlDoc.CreateElement(key);
                newSetting.InnerText = defaultValue;
                _xmlDoc.ChildNodes[1].AppendChild(newSetting);
 
                Save();
 
                return defaultValue;
            }
        }
 
        public static void Save()
        {
            if (_xmlDoc != null)
            {
                _xmlDoc.Save(SettingsProvider.ConfigPath);
            }
        }
    }
}
 

Provides access to key/value pairs stored in a config file in XML format.

Tags

C# CSharp .NET config file XML Settings provider

File locks in CSharp

string filename = "c:\\sample.htm";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, 
FileShare.None); //locks file
stream.Close(); //unlocks file
 
// or another variant:
 
string filename = "c:\\sample.htm";
FileStream stream = File.Open(filename, FileMode.Open);
stream.Lock(0, stream.Length); //locks file  
stream.Unlock(0, stream.Length); //unlocks file

Locks a file for exclusive use.

Tags

C# CSharp file locks filelocking

Simple file dialog

String[] showFileDialog()
{
    OpenFileDialog dialog = new OpenFileDialog();
 
    dialog.InitialDirectory = ".";
    // Preselect .txt files
    dialog.Filter = "Txt-Dateien|*.txt";
    dialog.FilterIndex = 1;
    dialog.Multiselect = false;
    dialog.RestoreDirectory = true;
 
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        return dialog.FileNames[0];
    }
}

Shows a simple dialog to pick a file from the local filesystem. In this snippet, only .txt files are preselected.

Tags

C# CSharp file dialog

Shorten too long file path in VB

''' <summary>
''' Diese Funktion kürzt einen Pfad ab so das aus
''' "C:\Windows\System32\Test\Test.dll" dann "C:\Windows\...\Test.dll" wird
''' </summary>
''' <param name="Path">Der Pfad der gekürzt zurückgegeben werden soll</param>
''' <param name="Length">Die gewünschte Länge die nicht überschritten werden darf</param>
''' <param name="TextFont">Die Schriftart die angewendet wird</param>
Public Function PathShorten(ByVal Path As String, ByVal Length As Integer, ByVal TextFont As Font) As String
    Dim PathParts() As String = Split(Path, "\")
    Dim PathBuild As New System.Text.StringBuilder(Path.Length)
    Dim LastPart As String = PathParts(PathParts.Length - 1)
    Dim PrevPath As String = ""
 
    'Erst prüfen ob der komplette String evtl. bereits kürzer als die Maximallänge ist
    If TextRenderer.MeasureText(Path, TextFont).Width < Length Then
        Return Path
    End If
 
    For i As Integer = 0 To PathParts.Length - 1
        PathBuild.Append(PathParts(i) & "\")
        If TextRenderer.MeasureText(PathBuild.ToString & "...\" & LastPart, TextFont).Width >= Length Then
            Return PrevPath
        Else
            PrevPath = PathBuild.ToString & "...\" & LastPart
        End If
    Next
    Return PrevPath
End Function
 

Shortens a long path to a way shorter path. For example: "C:\Windows\System32\Test\Test.dll" to "C:\Windows\...\Test.dll"

Tags

path file VB.NET shorten

Filename checker

#!/usr/bin/php -q
<?php
 
/**
 * Filename checker.
 * Checks for invalid UTF-8 and/or ASCII names of files and folders,
 * as well as characters that causes issues with different operating systems.
 *
 * @author Hellkeepa
 * @link https://dl.getdropbox.com/u/228121/checkfile.php
 * @version 1.3
 * @copyright GPL v3
 */
 
// Follow links maks this deep.
define ('MAX_LEVEL', 50);
 
// Set this to true if symlinks should be followed.
define ('FOLLOW_SYMLINK', true);
 
function check_utf8 ($folder, &$count, $check = 1, $level = 0) {
	$errorlvl = 0;
	$level++;
 
	if ($level >= MAX_LEVEL) {
		echo "WARNING: $level nested levels, check for recursion.n";
		return 2;
	}
 
	if (substr ($folder, -1) != '/') {
		$folder .= '/';
	}
 
	$dh = opendir ($folder);
	while ($file = readdir($dh)) {
		if ($file == '.' || $file == '..' || $file == "proc") {
			continue;
		}
 
		if (!FOLLOW_SYMLINK && is_link ($folder.$file)) {
			echo "'$folder$file' is a symlink.... Skipping.n";
			continue;
		}
 
		if ($check & 1 && !is_utf8 ($file)) {
			$count[1]++;
			echo "UTF8: $folder$filen";
		}
 
		if ($check & 2 && !is_ascii ($file)) {
			$count[2]++;
			echo "ASCII: $folder$filen";
		}
 
		if (preg_match ('#[\/:?*<>"|]#', $file)) {
			$count[4]++;
			echo "Invalid character: $folder$filen";
		}
 
		$file = $folder.$file;
		if (is_dir ($file)) {
			if ($errorlvl = check_utf8 ($file, $count, $check, $level)) {
				return $errorlvl;
			}
 
			$count[3]++;
		} else {
			$count[0]++;
		}
	}
 
	return $errorlvl;
}
 
/**
 * Returns true if $string is valid UTF-8 and false otherwise.
 *
 * @since        1.14
 * @param [mixed] $string     string to be tested
 * @link http://no2.php.net/manual/en/function.utf8-encode.php#85866
 * @subpackage
 */
function is_utf8 ($string) {
	// From http://w3.org/International/questions/qa-forms-utf-8.html
	return preg_match (
		'%^(?:
              [x09x0Ax0Dx20-x7E]            # ASCII
            | [xC2-xDF][x80-xBF]             # non-overlong 2-byte
            |  xE0[xA0-xBF][x80-xBF]        # excluding overlongs
            | [xE1-xECxEExEF][x80-xBF]{2}  # straight 3-byte
            |  xED[x80-x9F][x80-xBF]        # excluding surrogates
            |  xF0[x90-xBF][x80-xBF]{2}     # planes 1-3
            | [xF1-xF3][x80-xBF]{3}          # planes 4-15
            |  xF4[x80-x8F][x80-xBF]{2}     # plane 16
        )+\z%xs', $string);
}
 
function is_ascii ($string) {
	return preg_match ("#^[x09x0Ax0Dx20-x7E]+\z#s", $string);
}
 
function show_usage () {
	echo "Usage: " . escapeshellcmd ($argv[0]) . " <folder> [type]nn";
	echo "  Where type is either 'ASCII', 'UTF8', or 'full',n";
	echo "  defaults to UTF-8 check only.nn";
	return 1;
}
 
if ($argc < 2) {
	echo "ERROR: Folder not specified.nn";
	return show_usage ();
}
 
if ($argc > 3) {
	echo "ERROR: Too many arguments.nn";
	return show_usage ();
}
 
// Check if scan type is selected, and make sure it's valid.
$check = 1;
if ($argc == 3) {
	switch (strtolower ($argv[2])) {
		case "both":
		case "full":
			$check = 3;
			break;
		case "ascii":
			$check = 2;
			break;
		case "utf8":
		case "unicode":
			$check = 1;
			break;
		default:
			echo "ERROR: Unknown check type.nn";
			return show_usage();
	}
}
 
// Make sure argument 1 is an existing folder.
$folder = $argv[1];
if (!is_dir ($folder)) {
	echo "ERROR: Not a folder.nn";
	return show_usage ();
}
 
// Set counters to zero, and start the check.
$count = array (0, 0, 0, 0, 0);
$retval = check_utf8 ($folder, $count, $check);
 
// Print out results.
echo <<<EOL
 
{$count[0]} files checked.
{$count[3]} folders checked.
 
EOL;
if ($check & 1) { echo "{$count[1]} invalid UTF-8 names.n"; }
if ($check & 2) { echo "{$count[2]} invalid ASCII-names.n"; }
echo "{$count[4]} filename(s) with invalid charactersnn";
 
// Return standard error level.
return $retval;
 
?>
 

Checks for invalid UTF-8 and/or ASCII names of files and folders, as well as characters that causes issues with different operating systems.

Tags

PHP file check utf-8