Problem description: Yesterday I had a small program that could automatically switch the desktop, which was inconvenient to use. Because every time I add a new image, I manually modify the XML file and restart the service, so I created a configuration program as follows:
The functions shown above basically implement the functions required by the program. Three knowledge points are involved:
(1): Execute the command line statement and return results, which I did not know before. I used to know how to execute the command line command, but I do not know if the returned result is:
Code
# Region executes the doscommand and returns the doscommand output.
/// <Summary>
/// Execute the doscommand and return the doscommand output
/// </Summary>
/// <Param name = "doscommand"> doscommand </param>
/// <Param name = "milliseconds"> the waiting time (unit: milliseconds) for command execution. If it is set to 0, the waiting time is unlimited. </param>
/// <Returns> returns the output. If an exception occurs, an empty string is returned. </returns>
Public static string execute (string doscommand, int milliseconds)
{
String output = "";
If (doscommand! = NULL & doscommand! = "")
{
Process = new process ();
Processstartinfo startinfo = new processstartinfo ();
Startinfo. filename = "cmd.exe ";
Startinfo. Arguments = "/C" + doscommand;
Startinfo. useshellexecute = false;
Startinfo. redirectstandardinput = false;
Startinfo. redirectstandardoutput = true;
Startinfo. createnowindow = true;
Process. startinfo = startinfo;
Try
{
If (process. Start ())
{
If (milliseconds = 0)
Process. waitforexit ();
Else
Process. waitforexit (milliseconds );
Output = process. standardoutput. readtoend ();
}
}
Catch
{
}
Finally
{
If (process! = NULL)
Process. Close ();
}
}
Return output;
}
# Endregion
(2): writing and reading XML data. It has been used for a long time. The Code record is as follows for Future Search:
Code
# Region btnsave_click
Private void btnsave_click (Object sender, eventargs E)
{
If (this. lstfilepath. Items. Count> 0)
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (xml_path );
Xmldoc. selectsinglenode ("paths"). removeall ();
Xmlnode root = xmldoc. selectsinglenode ("paths ");
Xmlelement xmlele = (xmlelement) xmldoc. selectsinglenode ("paths ");
Xmlele. setattribute ("time", this.txt time. Text );
For (INT I = 0; I <this. lstfilepath. Items. Count; I ++)
{
Xmlelement xmle = xmldoc. createelement ("path ");
Xmle. innertext = This. lstfilepath. items [I]. tostring ();
Root. appendchild (xmle );
}
Xmldoc. Save (xml_path );
MessageBox. Show ("saved successfully! Restart the Service (close the service first) to take effect ");
}
}
# Endregion
(3): Install the service and determine how to install the service successfully. As follows:
Code
# Region btninstall_click
Private void btninstall_click (Object sender, eventargs E)
{
Directory. createdirectory (app_path + "dynamicdesktop ");
File. Copy (environment. currentdirectory. tostring () + "/dynamicdesktop/background.exe", app_path + "dynamicdesktop \ background.exe", true );
File. Copy (environment. currentdirectory. tostring () + "/dynamicdesktop/background.exe. manifest", app_path + "dynamicdesktop \ background.exe. manifest", true );
File. Copy (environment. currentdirectory. tostring () + "/dynamicdesktop/butterfly .bmp", app_path + "dynamicdesktop \ butterfly .bmp", true );
File. copy (environment. currentdirectory. tostring () + "/dynamicdesktop/desktop. XML ", environment. getfolderpath (environment. specialfolder. system) + "\ Desktop. XML ", true );
If (! File. exists (txtsdk. Text. Trim () + "\ installutil.exe "))
{
MessageBox. Show ("Please select the correct V2.0 SDK path! ");
Return;
}
String result = string. empty;
Result = execute (txtsdk. Text + "\ installutil.exe" + app_path + "dynamicdesktop \ background.exe", 0 );
If (result. Contains ("transaction processing and installation completed "))
{
MessageBox. Show ("installation successful! ");
Initshowdesktop ();
}
Else
{
MessageBox. Show (result );
}
}
# Endregion