private byte[] StringToByteArray(string sourceString)
{
System.Text.ASCIIEncoding sourceEncoding = new System.Text.ASCIIEncoding();
return sourceEncoding.GetBytes(sourceString);
}
private string ByteArrayToString(byte[] sourceArray)
{
System.Text.ASCIIEncoding sourceEncoding = new System.Text.ASCIIEncoding();
return sourceEncoding.GetString(sourceArray);
}
Mit diesem Code wird verhindert man, das eine Applikation mehr als einmal innerhalb einer Session (Anmeldung) ausgeführt werden kann:
private const string AppGuid = "97E652FF-73A5-3ED6-A4E5-DA2AF2132C98";
[STAThread]
private static void Main()
{
using (Mutex progMutex = new Mutex(false, AppGuid))
{
if (!progMutex.WaitOne(0, false))
{
MessageBox.Show("Dieses Programm ist bereits geöffnet");
return;
}
Application.Run(new Form1());
}
}
Soll die Anwendung in allen Sessions nur einmal ausgeführt werden können, muss man den Mutex in den global Namespace schreiben:
private const string AppGuid = "97E652FF-73A5-3ED6-A4E5-DA2AF2132C98";
[STAThread]
private static void Main()
{
using (Mutex progMutex = new Mutex(false, @"Global\" + AppGuid))
{
if (!progMutex.WaitOne(0, false))
{
MessageBox.Show("Dieses Programm ist bereits geöffnet");
return;
}
Application.Run(new Form1());
}
}
[DllImport("WININET", CharSet = CharSet.Auto)]
static extern bool InternetGetConnectedState(int lpdwFlags, int dwReserved);
public bool CheckInternetConnection()
{
return InternetGetConnectedState(0, 0);
}
Um eine Enumeration in einem WCF Sefvice an den Client zu übertragen, muss das [ServiceKnownType(typeof(Enum))] Attribute in den Service Contract eingefügt werden:
namespace TestService.Interfaces
{
[ServiceContract]
[ServiceKnownType(typeof(MyEnum))]
public interface ITestService
{
[OperationContract]
....
}
}
Die Enumeration sieht dann so aus:
namespace TestService.Classes
{
[DataContract]
public enum MyEnum
{
[EnumMember]
Undefined = 0,
[EnumMember]
Okay = 1,
[EnumMember]
Error = 2
}
[DataContract]
public class MyClass
{
...
}
}
public static string Left(string strText, int intLength)
{
try
{
if (intLength < 0)
throw new ArgumentOutOfRangeException("Length", intLength, "Length must be > 0");
else if (intLength == 0 || strText.Length == 0)
return "";
else if (strText.Length <= intLength)
return strText;
else
return strText.Substring(0, intLength);
}
catch {}
}
public string getMD5Hash(string strSource)
{
String strResult;
if ((strSource == null) || (strSource.Length == 0))
{
strResult = string.Empty;
}
else
{
System.Security.Cryptography.MD5CryptoServiceProvider objMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] arrTextToHash = Encoding.Default.GetBytes(strSource);
byte[] arrResult = objMD5.ComputeHash(arrTextToHash);
strResult = System.BitConverter.ToString(arrResult);
}
return strResult;
}