Créer un raccourci sur le bureau
Ajouter un référence à IWshRuntimeLibrary (située C:WINDOWSsystem32wshom.ocx)
|
private void CreateShortCut(string
fileName)
{
string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
WshShell oWshShell = new
WshShell();
IWshShortcut oIWshShortcut = (IWshShortcut)oWshShell.CreateShortcut(Path.Combine(Desktop, Path.GetFileNameWithoutExtension(fileName) + ".lnk"));
oIWshShortcut.TargetPath = fileName;
oIWshShortcut.Save();
}
|
Utilisation :
|
CreateShortCut(« C:/Documents and Settings/romagny/Mes documents/monapplication.exe »);
|
Par Romagny13
0
-
Recommander
2 petites fonctions utilitaires de texte
Souvent je préférerais pouvoir entrer l’index de début et l’index de fin dans ma chaine plutôt que la longueur
|
public string Substring(string
sInput,int nStart,int nEnd)
{
string sResult = string.Empty;
if (nEnd <= nStart || nStart>sInput.Length ||
nEnd>sInput.Length)
{ }
else
{
int nLength = nEnd -
nStart;
sResult = sInput.Substring(nStart, nLength);
}
return sResult;
}
public string Replace(string sInput,string sReplacement, int nStart, int nEnd)
{
string sResult = string.Empty;
if (nEnd <= nStart || nStart > sInput.Length ||
nEnd > sInput.Length)
{ }
else
{
int nLength = nEnd -
nStart;
sResult = sInput.Remove(nStart, nLength);
sResult = sResult.Insert(nStart, sReplacement);
}
return sResult;
}
|
Par Romagny13
0
-
Recommander
Vérifier en C# qu'une chaine de caractères ne contient que des lettres
ou ne contient que des chiffres
1 – ma proposition
|
//
string
///<summary>
/// vérifie
que la chaine ne contient que des lettres
///</summary>
///<param
name="sReceive"></param>
///<returns></returns>
public bool isLetter(string sReceive)
{
bool bResult;
char[] cWork;
System.Collections.IEnumerator EnumeratorcWork;
bResult = true;
cWork = sReceive.ToCharArray();
EnumeratorcWork = cWork.GetEnumerator();
while (EnumeratorcWork.MoveNext()==true)
{
if (Char.IsLetter((Char)EnumeratorcWork.Current) == false)
{
bResult = false;
}
}
return bResult;
}
///<summary>
/// vérifie
que la chaine ne contient que des chiffres
///</summary>
///<param
name="sReceive"></param>
///<returns></returns>
public bool isDigit(string sReceive)
{
bool bResult;
char[] cWork;
System.Collections.IEnumerator EnumeratorcWork;
bResult = true;
cWork = sReceive.ToCharArray();
EnumeratorcWork = cWork.GetEnumerator();
while (EnumeratorcWork.MoveNext() == true)
{
if (Char.IsDigit((Char)EnumeratorcWork.Current) == false)
{
bResult = false;
}
}
return bResult;
}
|
Ou plus simplement
|
public bool isLetter(string
sReceive)
{
bool bResult;
bResult = true;
foreach (char cWork
in sReceive)
{
if (char.IsLetter(cWork) == false)
{
bResult = false;
}
}
return bResult;
}
public bool isDigit(string sReceive)
{
bool bResult;
bResult = true;
foreach (char cWork
in sReceive)
{
if (char.IsDigit(cWork) == false)
{
bResult = false;
}
}
return bResult;
}
|
2 – avec TryParse
Ex : vérifier qu’une chaine ne contient que des chiffres,
TryParse retourne un booléen indiquant si la conversion est possible
nResult (précédé par out) contiendra le résultat de la conversion
|
int nResult;
if (int.TryParse(textBox1.Text, out nResult) == false)
{
MessageBox.Show("la chaine ne peut etre convertit en entier");
}
|
3 – en VB.NET
La méthode IsNumeric() renvoie un booléen indiquant si la chaine de caractères passée en paramètre ne contient que des chiffres
4 – les expressions régulières peuvent servir également
Par Romagny13
0
-
Recommander