Dobrý deň. 1. Chcem sa spýtať že cez C# ako spravím aby sa program spustil automaticky? npr.: Program spustím 1x ja a potom sa bude spúšťať sám.
procedure SetRunOnStart(AllUsers: Boolean);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
with Registry do
try
if AllUsers then
RootKey := HKEY_LOCAL_MACHINE
else
RootKey := HKEY_CURRENT_USER;
if OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run', False) then
// název položky = titulek aplikace
// hodnota položky = EXE soubor aplikace včetně cesty
WriteString(Application.Title, Application.ExeName)
else
raise Exception.Create('Chyba při otevírání klíče registrů.');
finally
Free;
end;
end;
Čo mám napísať namiesto název položky a hodnota položky? Tento kód zapisuje ten .exe súbor do registrov - myslím si že keby som ten .exe súbor premiestnil niekde inak tak by ho nenašlo.
Tak ak môžem poprosiť aby tento kód hľadal .exe pri sebe a nie inde. Ďakujem.
2. Ako spravím inštaláciu? Vlastne spustím .exe a ono my to nainštaluje do súboru ktorý si tam zadám alebo ho vytvorí.
a) Môže byť aj z tabuľkami.
b) Môže byť aj tak že spustím .exe a len my to ie dotyčné súbory dá kam som to v C# naprogramoval. Ďakujem.
Našiel som tento kód. Kde a čo mám prepísať aby to spúšťalo čo má?
//Author: Winston Gubantes
//Created: November 26, 2009
//Purpose: to Create or starting a Task by TaskScheduler or By Process
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskScheduler;
using System.Windows.Forms;
using System.Diagnostics;
//using System.Runtime.InteropServices; //used to call API windows
namespace BusinessLogic
{
public class TaskHelper
{
public TaskHelper()
{
}
//[DllImport("shell32.dll")]
//private extern int ShellExecute(int hwnd, string operation, string filex, string parameters, string directory, int command);
public static void createTaskImmediateExec(string TaskName, string Comment, string AppName, string Parameters, int SecondsDelay)
{
//Updated By: Winston Gubantes
#region TaskScheduler
TaskScheduler.ScheduledTasks oSt = new TaskScheduler.ScheduledTasks();
TaskScheduler.Task oTask = null;
try
{
oTask = oSt.CreateTask(TaskName);
}
catch (ArgumentException)
{
try
{
//oSt.DeleteTask(“AgentUpdate”);
oSt.DeleteTask(TaskName);
oTask = oSt.CreateTask(TaskName);
}
catch
{
}
}
//oTask.Comment = “MovingPro Agent”;
oTask.Comment = Comment;
//oTask.ApplicationName = “MSIEXEC.EXE”;
oTask.ApplicationName = AppName;
//oTask.WorkingDirectory = Application.StartupPath.Trim();
//oTask.WorkingDirectory = strUpdatePath;
oTask.SetAccountInformation(System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(), (string)null);
//oTask.Parameters = @” /CLEANUP “;
//oTask.Parameters = ” /I ” + strUpdatePath + “\\agentsetup.msi /QN “;
oTask.Parameters = Parameters;
oTask.Flags = TaskFlags.RunOnlyIfLoggedOn;
//oTask.Hidden = true;
if (SecondsDelay > 0)
{
oTask.Triggers.Add(new RunOnceTrigger(DateTime.Now.AddSeconds(SecondsDelay)));
}
oTask.Save();
oTask.Hidden = true;
oTask.Run();
oTask.Close();
oSt.Dispose();
#endregion
}
public static void createTaskOnlogon(string TaskName, string Comment, string AppName, string Parameters)
{
//Updated By: Winston Gubantes
#region TaskScheduler
TaskScheduler.ScheduledTasks oSt = new TaskScheduler.ScheduledTasks();
TaskScheduler.Task oTask = null;
try
{
oTask = oSt.CreateTask(TaskName);
}
catch (ArgumentException)
{
return;
}
//oTask.Comment = “MovingPro Agent”;
oTask.Comment = Comment;
//oTask.ApplicationName = “MSIEXEC.EXE”;
oTask.ApplicationName = AppName;
//oTask.WorkingDirectory = Application.StartupPath.Trim();
//oTask.WorkingDirectory = strUpdatePath;
oTask.SetAccountInformation(System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(), (string)null);
//oTask.Parameters = @” /CLEANUP “;
//oTask.Parameters = ” /I ” + strUpdatePath + “\\agentsetup.msi /QN “;
oTask.Parameters = Parameters;
oTask.Flags = TaskFlags.RunOnlyIfLoggedOn;
//oTask.Hidden = true;
oTask.Triggers.Add(new OnLogonTrigger());
oTask.Save();
//oTask.Hidden = true;
oTask.Run();
oTask.Close();
oSt.Dispose();
#endregion
}
public static void startProcess(string DirPath, string ExeName, string Arguments)
{
startProcess(DirPath, ExeName, Arguments, false);
}
public static void startProcess(string DirPath, string ExeName, string Arguments,bool isHideWindow)
{
//System.Diagnostics.ProcessStartInfo psi = new ProcessStartInfo();
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.FileName = ExeName;
if (isHideWindow)
{
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.CreateNoWindow = true;
}
if (DirPath != “”) { psi.WorkingDirectory = DirPath; }
psi.Arguments = Arguments;
//psi.Arguments = ” /I ” + @” “”" + strFileName + @” “” ” + ” /QN”;
Process.Start(psi);
}
}
}
//When used in code We used task scheduler for Vista/Win7
BusinessLogic.TaskHelper.createTaskImmediateExec(“Report”,
“Sample Title”,
Application.StartupPath + “\\Your.exe”,
“Sample Args”,
0);