public void StopService(string ServiceName)
{
ServiceController sc = new ServiceController(ServiceName);
if (sc != null && sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
Collection and sharing of, interview questions and answers asked in various interviews, faqs and articles.....
Showing posts with label C# code. Show all posts
Showing posts with label C# code. Show all posts
c# code : Start Windows service
public void StartService(string ServiceName)
{
ServiceController sc = new ServiceController(ServiceName);
if (sc != null && sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
}
}
{
ServiceController sc = new ServiceController(ServiceName);
if (sc != null && sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
}
}
c# code : get created date time type metadata for a given filepath
///
/// Reads created date time type metadata for a given filepath
///
///
///createdDatetime of the file
private string ReadCreationDateMetadata(string filepath)
{
string Filepath = filepath;
string FileName = Path.GetFileName(Filepath);
DateTime CreatedDateTime = File.GetCreationTime(Filepath);
string createdDatetime = string.Format("{0:R}", CreatedDateTime);
return createdDatetime;
}
Please write your comments
/// Reads created date time type metadata for a given filepath
///
///
///
private string ReadCreationDateMetadata(string filepath)
{
string Filepath = filepath;
string FileName = Path.GetFileName(Filepath);
DateTime CreatedDateTime = File.GetCreationTime(Filepath);
string createdDatetime = string.Format("{0:R}", CreatedDateTime);
return createdDatetime;
}
Please write your comments
C# code : get content type metadata for a given filepath
///
/// Reads content type metadata for a given filepath
///
///
///Content type of the file
private string ReadContentTypeMetadata(string filepath)
{
const string DefaultContentType = "application/unknown";
RegistryKey regkey = null;
RegistryKey fileextkey = null;
string filecontenttype = null;
string fileextension = null;
//the file extension to lookup
fileextension = Path.GetExtension(filepath);
try
{
//look in HKClassRoot in registry
regkey = Registry.ClassesRoot;
//look for extension
fileextkey = regkey.OpenSubKey(fileextension);
//retrieve Content Type value
filecontenttype = fileextkey.GetValue("Content Type", DefaultContentType).ToString();
//cleanup
fileextkey = null;
regkey = null;
}
catch
{
filecontenttype = DefaultContentType;
}
//string convertedMediatype = convertMediaType(filecontenttype);
return filecontenttype;
}
/// Reads content type metadata for a given filepath
///
///
///
private string ReadContentTypeMetadata(string filepath)
{
const string DefaultContentType = "application/unknown";
RegistryKey regkey = null;
RegistryKey fileextkey = null;
string filecontenttype = null;
string fileextension = null;
//the file extension to lookup
fileextension = Path.GetExtension(filepath);
try
{
//look in HKClassRoot in registry
regkey = Registry.ClassesRoot;
//look for extension
fileextkey = regkey.OpenSubKey(fileextension);
//retrieve Content Type value
filecontenttype = fileextkey.GetValue("Content Type", DefaultContentType).ToString();
//cleanup
fileextkey = null;
regkey = null;
}
catch
{
filecontenttype = DefaultContentType;
}
//string convertedMediatype = convertMediaType(filecontenttype);
return filecontenttype;
}
c# code : get filename metadata for a given filepath
///
/// Reads Filename metadata for a given filepath
///
///
///File name
private string ReadFileNameMetadata(string filepath)
{
string FileName = Path.GetFileName(filepath);
return FileName;
}
/// Reads Filename metadata for a given filepath
///
///
///
private string ReadFileNameMetadata(string filepath)
{
string FileName = Path.GetFileName(filepath);
return FileName;
}
get InnerText as Result of the selected tag
///
/// Returns the InnerText as Result of the selected tag
///
///
///
public string GetEntry(string param)
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(_configFile);
System.Xml.XmlNodeList xmlNodes = xmlDoc.SelectNodes(param);
if (xmlNodes.Count == 1)
{
return xmlNodes.Item(0).InnerText;
}
else
{
return null;
}
}
/// Returns the InnerText as Result of the selected tag
///
///
///
public string GetEntry(string param)
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(_configFile);
System.Xml.XmlNodeList xmlNodes = xmlDoc.SelectNodes(param);
if (xmlNodes.Count == 1)
{
return xmlNodes.Item(0).InnerText;
}
else
{
return null;
}
}
c# code get category form ini file
iniFileinfo.IniHelp ihelp = new iniFileinfo.IniHelp();
string iniFile="c://my.ini";
List<string> categories = ihelp.GetCategories(iniFile);
foreach (string category in categories)
{
Console.WriteLine(category);
}
Out put :-
serverpath
localpath
File my.ini [serverpath] files=solaris9 operator=mmas [localpath]
iniFileinfo.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace iniFileinfo
{
using System;
using System.Runtime.InteropServices;
using System.Text;
class IniHelp
{
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
string lpReturnString,
int nSize,
string lpFilename);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFilename);
public List<string> GetCategories(string iniFile)
{
string returnString = new string(' ', 65536);
GetPrivateProfileString(null, null, null, returnString, 65536, iniFile);
char[] sep = {'\0'};
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetKeys(string iniFile, string category)
{
string returnString = new string(' ', 32768);
GetPrivateProfileString(category, null, null, returnString, 32768, iniFile);
char[] sep = { '\0' };
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public string GetIniFileString(string iniFile, string category, string key, string defaultValue)
{
string returnString = new string(' ', 1024);
GetPrivateProfileString(category, key, defaultValue, returnString, 1024, iniFile);
char[] sep = { '\0' };
return returnString.Split(sep)[0];
}
}
}
string iniFile="c://my.ini";
List<string> categories = ihelp.GetCategories(iniFile);
foreach (string category in categories)
{
Console.WriteLine(category);
}
Out put :-
serverpath
localpath
File my.ini [serverpath] files=solaris9 operator=mmas [localpath]
iniFileinfo.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace iniFileinfo
{
using System;
using System.Runtime.InteropServices;
using System.Text;
class IniHelp
{
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
string lpReturnString,
int nSize,
string lpFilename);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFilename);
public List<string> GetCategories(string iniFile)
{
string returnString = new string(' ', 65536);
GetPrivateProfileString(null, null, null, returnString, 65536, iniFile);
char[] sep = {'\0'};
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetKeys(string iniFile, string category)
{
string returnString = new string(' ', 32768);
GetPrivateProfileString(category, null, null, returnString, 32768, iniFile);
char[] sep = { '\0' };
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public string GetIniFileString(string iniFile, string category, string key, string defaultValue)
{
string returnString = new string(' ', 1024);
GetPrivateProfileString(category, key, defaultValue, returnString, 1024, iniFile);
char[] sep = { '\0' };
return returnString.Split(sep)[0];
}
}
}
C# code get all keys from ini file
iniFileinfo.IniHelp ihelp = new iniFileinfo.IniHelp();
string iniFile="c://my.ini";
List<string> categories = ihelp.GetCategories(iniFile);
List<string> keys = ihelp.GetKeys(iniFile, "serverpath");
foreach (string key in keys)
{
// here in key variable you can key value
console.writeline(key);
}
Out put :-
files
operator
File my.ini
[serverpath]
files=solaris9
operator=mmas
[localpath]
iniFileinfo.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace iniFileinfo
{
using System;
using System.Runtime.InteropServices;
using System.Text;
class IniHelp
{
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
string lpReturnString,
int nSize,
string lpFilename);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFilename);
public List<string> GetCategories(string iniFile)
{
string returnString = new string(' ', 65536);
GetPrivateProfileString(null, null, null, returnString, 65536, iniFile);
char[] sep = {'\0'};
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetKeys(string iniFile, string category)
{
string returnString = new string(' ', 32768);
GetPrivateProfileString(category, null, null, returnString, 32768, iniFile);
char[] sep = { '\0' };
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public string GetIniFileString(string iniFile, string category, string key, string defaultValue)
{
string returnString = new string(' ', 1024);
GetPrivateProfileString(category, key, defaultValue, returnString, 1024, iniFile);
char[] sep = { '\0' };
return returnString.Split(sep)[0];
}
}
}
string iniFile="c://my.ini";
List<string> categories = ihelp.GetCategories(iniFile);
List<string> keys = ihelp.GetKeys(iniFile, "serverpath");
foreach (string key in keys)
{
// here in key variable you can key value
console.writeline(key);
}
Out put :-
files
operator
File my.ini
[serverpath]
files=solaris9
operator=mmas
[localpath]
iniFileinfo.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace iniFileinfo
{
using System;
using System.Runtime.InteropServices;
using System.Text;
class IniHelp
{
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
string lpReturnString,
int nSize,
string lpFilename);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFilename);
public List<string> GetCategories(string iniFile)
{
string returnString = new string(' ', 65536);
GetPrivateProfileString(null, null, null, returnString, 65536, iniFile);
char[] sep = {'\0'};
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public List<string> GetKeys(string iniFile, string category)
{
string returnString = new string(' ', 32768);
GetPrivateProfileString(category, null, null, returnString, 32768, iniFile);
char[] sep = { '\0' };
List<string> result = new List<string>(returnString.Split(sep));
result.RemoveRange(result.Count - 2, 2);
return result;
}
public string GetIniFileString(string iniFile, string category, string key, string defaultValue)
{
string returnString = new string(' ', 1024);
GetPrivateProfileString(category, key, defaultValue, returnString, 1024, iniFile);
char[] sep = { '\0' };
return returnString.Split(sep)[0];
}
}
}
Subscribe to:
Posts (Atom)