I have a Login application that will disable after 3 times of wrong attempts..I just wanted to know how to make that application still running after i restart my computer..my application is already in .exe ...thanks in advance :)
Going on the assumption that you're talking about Windows here, a simple google search for "windows run application at startup" would yield a plethora of simple ways to make an application run when the OS starts.
Here is one of those results...
http://windows.microsoft.com/en-us/windows/run-program-automatically-windows-starts#1TC=windows-7
I guess you can add application executable in windows startup. Following method help to create app shortcut.
public static void CreateStartupShortcut(string applicationName)
{
string _startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
WshShell _shell = new WshShell();
//
string _shortcutAddress = _startupFolder + #"\" + applicationName + ".lnk";
IWshShortcut _shortcut = (IWshShortcut)_shell.CreateShortcut(_shortcutAddress);
_shell.RegDelete(_shortcutAddress);
//
_shortcut.Description = "Start up shortcut link for application " + applicationName + ". Delete shortcut to if you dont want to run application on stat up";
_shortcut.WorkingDirectory = Application.StartupPath;
_shortcut.TargetPath = Application.ExecutablePath;
_shortcut.Save();
}
Simply pass your application exe path to the method and it will add shortcut in windows startup.
Related
What I currently have is a MIM .dll extension for the metaverse that needs to call the command line to run some console commands. These console commands are for a web tool call GAM that will create G-Suite users. I have a test console app built-in .NET Framework 4.7.2 that works just fine. When running the code with my .dll it seems to process just fine, even returns a positive exit code of 0, but does not perform the function in cmd that is expected. The .dll is in .NET Framework 4.5. The code block is the same for both applications:
using (Process gamProcess = new Process())
{
gamProcess.StartInfo.UseShellExecute = false;
gamProcess.StartInfo.RedirectStandardOutput = true;
gamProcess.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
gamProcess.StartInfo.CreateNoWindow = true;
gamProcess.StartInfo.Arguments = "/c gam create user " + username + " firstname " + firstname + " lastname " + lastname + " password " + pass;
gamProcess.Start();
gamProcess.WaitForExit();
}
If I call this block of code directly from the .dll, it will act as if it performs the action but nothing happens. If I call this code from the standalone application, it works just fine. I have even attempted to just call this application from the .dll and pass arguments directly to it. This will work if I set the arguments in Visual Studio but it will not work if I call the application from the .dll. I'm at a loss here, I cannot figure out what is the difference between running from my extension and running from the application.
Another side note, I built an even more simplistic console application that just writes to a file. I was able to call this application from the .dll, and it works just fine. It is only the commands above that the .dll seems to not want to process correctly.
I managed to fix the above code. I did two things at the same time, so I'm not sure which was the key to this issue but it is working flawlessly now. I changed the /c in the code to a /C uppercase. The code now looks as follows:
using (Process gamProcess = new Process())
{
gamProcess.StartInfo.UseShellExecute = false;
gamProcess.StartInfo.RedirectStandardOutput = true;
gamProcess.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
gamProcess.StartInfo.CreateNoWindow = true;
gamProcess.StartInfo.Arguments = "/C gam create user " + username + " firstname " + firstname + " lastname " + lastname + " password " + pass;
gamProcess.Start();
gamProcess.WaitForExit();
}
The second action I performed was a restart of the dev server I was working on. I'm not sure which worked, especially since I was running a lowercase /c in my console app, but the code is working now. Thank you all who viewed this question.
If you type "regedit" in the Start menu's edit box and mash the Enter key, Registry Editor will be invoked. The same is true for "cmd" and the Command Line, and doubtless several other apps.
How can I get my app to respond the same way, so that if the user enters "Platypus" in the Start menu edit box, Platypus.exe will be invoked?
Does it require manipulation of the Registry / adding an entry somewhere there, and if so, just what key and value needs to be added?
I would be satisfied with the user needing to run the app manually once (2-clicking its icon; it's a Winforms app), at which time startup code (no pun intended) would do whatever was necessary to make the app henceforth Startsmartable (Windows key, "Platypus", to start the app).
I know that it's just as easy/easier for the user to simply 2-click a desktop icon when they want to run the app, but this particular functionality is not my idea, so complaints about the oddity of this question would be to no avail.
UPDATE
I added the code recommended by Chandan (with my executable's name):
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("RoboReporter", "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
}
}
...called it from the main form's load event:
private void FormRoboReporter_Load(object sender, EventArgs e)
{
RoboReporterConstsAndUtils.AddToStartup();
}
...shut down the app, went to the Start menu and entered the program's name ("RoboReporter"), and all it did was bring up search results of related file names.
UPDATE 2
What it does do is cause my app to run whenever the computer is restarted. That's not what I want. The code above adds an entry to HKEY_CURRENT_USER.Software.Microsoft.Windows.CurrentVersion.Run as can be seen here (along with a couple of other entries that predated it):
I don't want the app to start up every time the computer restarts, so I removed the entry. The question remains: how can I make the app runnable from the Start menu?
You can add your application's parent directory's path to the environment variable called PATH.
string pathvar = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PATH", pathvar + ";" + Application.StartupPath + "\\", EnvironmentVariableTarget.Machine);
(Note that the paths added to this variable should end with a backslash \, and each path is separated by a semicolon ;)
Adding the parent directory's path to the environment variable will make all it's contents quickly accessible from the Start Menu's search field, from Run and from CMD.
You can also change EnvironmentVariableTarget.Machine to EnvironmentVariableTarget.User to modify the variable for the current user only.
EDIT:
A note: Setting a variable for the entire machine (by using EnvironmentVariableTarget.Machine) seems to require elevated privileges when done from one's application.
you might want to run this
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("Name_of_your_Program", "\"" + Application.ExecutablePath + "\"");
}
}
Is there any way to recognize if the application has been run from a shortcut instead of executable file? I need to make my users to copy exe file to their desktops rather than create shortcuts to it due to personalization issues. Any ideas?
Edit: creating the installer is not an option.
I don't know if this helps, but if you want your exe file to be on the desktop, this could work:
string path = Directory.GetCurrentDirectory();
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (!path.Equals(desktopPath))
{
Console.WriteLine("file is not at desktop");
}
If you do have an app in the windows shared folder you can configure it to prevent execution of the applications.
Or you can provide user just with link to .bat file instead of .exe and it would do something like this (using robocopy):
robocopy \\remote\server\exe %AppData%\your\folder app.exe /XO
start %AppData%\your\folder\app.exe
And on the C# side you can just check application path and do something like this:
public class Program
{
public int Main()
{
string original_path = System.IO.Path.GetFullPath(#"\\remote\app.exe");
string current_path = System.IO.Path.GetFullPath(
System.Reflection.Assembly.GetExecutingAssembly().Location);
if(original_path == current_path){
System.IO.File.Copy(original_path, #"C:\foo\bar\app.exe", true);
System.Diagnostics.Process.Start(#"C:\foo\bar\app.exe");
return 0;
}
// Run program normally here
}
}
I have searched everywhere to find out how to make a custom minecraft launcher. I managed to create this code, which should work, but sadly it does not. I login but it never starts, however for a second I get the loading ring next to my mouse. This is my code:
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = #"-Xmx1G -Djava.library.path=%APPDATA%\.minecraft\versions\1.6.2\1.6.2-natives -cp %APPDATA%\.minecraft\libraries\net\sf\jopt-simple\jopt-simple\4.5\jopt-simple-4.5.jar;%APPDATA%\.minecraft\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;%APPDATA%\.minecraft\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;%APPDATA%\.minecraft\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;%APPDATA%\.minecraft\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;%APPDATA%\.minecraft\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;%APPDATA%\.minecraft\libraries\argo\argo\2.25_fixed\argo-2.25_fixed.jar;%APPDATA%\.minecraft\libraries\org\bouncycastle\bcprov-jdk15on\1.47\bcprov-jdk15on-1.47.jar;%APPDATA%\.minecraft\libraries\com\google\guava\guava\14.0\guava-14.0.jar;%APPDATA%\.minecraft\libraries\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;%APPDATA%\.minecraft\libraries\commons-io\commons-io\2.4\commons-io-2.4.jar;%APPDATA%\.minecraft\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;%APPDATA%\.minecraft\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;%APPDATA%\.minecraft\libraries\com\google\code\gson\gson\2.2.2\gson-2.2.2.jar;%APPDATA%\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\2.9.0\lwjgl-2.9.0.jar;%APPDATA%\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.0\lwjgl_util-2.9.0.jar;%APPDATA%\.minecraft\versions\1.6.2\1.6.2.jar net.minecraft.client.main.Main --username playername --session token:"+ words[3] + #":" + words[4]+ #" --version 1.6.2 --gameDir %APPDATA%\.minecraft --assetsDir %APPDATA%\.minecraft\assets";
start.FileName = #"c:\Program Files (x86)\java\jre7\bin\javaw.exe";
// Do you want to show a console window?
start.CreateNoWindow = true;
System.Diagnostics.Process.Start(start);
This just does the loading ring by my mouse for a second, then nothing opens. No logs, crashes, errors, nothing wrong. This is Visual c# compiled on Visual Studio 2012.
The arguments you are giving have an environment variable in them - %APPDATA%.
The command line will expand this by default, but the .net library won't.
See How do I ensure c# Process.Start will expand environment variables?
As Pete Kirkham mentioned you need to set up environment variable.
You can set it before starting the Process like:
var appDataPath = "your path";
start.EnvironmentVariables.Add("APPDATA", appDataPath);
Is it possible to make an application in C# that will be able to delete itself in some condition.
I need to write an updater for my application but I don't want the executable to be left after the update process.
There is an official .Net OneClick but due to some incompatibilities with my HTTP server and some problems of OneClick itself I'm forced to make one myself.
George.
[EDIT]
In more details:
I have:
Application Executable which downloads the updater ("patch", but not exactly) this "patch" updates the application executable itself.
Application executes as folowed:
Application: Start -> Check Version -> Download new Updater -> Start Updater -> exit;
Updater: Start -> do it's work -> start Application Executable -> self delete (this is where I get stuck);
If you use Process.Start you can pass in the Del parameter and the path to the application you wish to delete.
ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info);
Code snippet taken from this article
I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards
public static class Updater
{
public static void Main()
{
string path = #"updater.bat";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("updater.exe");
sw.WriteLine("delete updater.exe /y");
sw.WriteLine("delete updater.bat /y");
}
System.Process.Start(path);
}
else
{
RunUpdateProcess();
}
}
private void RunUpdateProcess()
{
.....
}
}
It's tricky without introducing yet another process (that you'd then want to delete as well, no doubt). In your case, you already have 2 processes - updater.exe and application.exe. I'd probably just have the Application delete updater.exe when it's spawned from there - you could use a simple command line arg, or an IPC call from updater.exe to application.exe to trigger it. That's not exactly a self deleting EXE, but fulfills the requirements I think.
For the full treatment, and other options you should read the definitive treatment of self deleting EXEs. Code samples are in C (or ASM), but should be p/invokable.
I'd probably try CreateFile with FILE_FLAG_DELETE_ON_CLOSE for updater.exe with something like (psuedo code):
var h = CreateFile(
"updater.exe",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE
);
byte[] updaterBytes = GetUpdaterBytesFromWeb();
File.WriteAllBytes("updater.exe", updaterBytes);
Process.Start("updater.exe");
Once application.exe exits, updater.exe has a file handle of 1. When updater.exe exits, it drops to 0 and should be deleted.
Couldn't you simply delete the updater from within the application? i.e.:
Application: Start -> [Delete old updater if present] -> Check version -> Download new updater -> Start updater -> exit;
Updater: Start -> Perform update -> Start application -> exit;
Application: Start -> [Delete old updater if present] -> ...
Mhh so let me get this straight. You got some application.exe and your updater application updater.exe?
So when you start your application.exe it checks some webserver for a newer version and then starts updater.exe. And you want updater.exe to delete itself after it has finished updating? Or do you want to delete the downloaded patch (or similar)? Please be a bit more precise.
Consider that when you are deleting updater.exe you must recreate it for the next update process.
your second line can be
Updater: Star -> do it's work -> start Application Executable -> Updater Exits -> Application deletes your Updater.exe
public void uninstall() {
string app_name = Application.StartupPath + "\\" + Application.ProductName + ".exe";
string bat_name = app_name + ".bat";
string bat = "#echo off\n"
+ ":loop\n"
+ "del \"" + app_name + "\"\n"
+ "if Exist \"" + app_name + "\" GOTO loop\n"
+ "del %0";
StreamWriter file = new StreamWriter(bat_name);
file.Write(bat);
file.Close();
Process bat_call = new Process();
bat_call.StartInfo.FileName = bat_name;
bat_call.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
bat_call.StartInfo.UseShellExecute = true;
bat_call.Start();
Application.Exit();
}
self delete by an external executable file ".bat" for windows form applications.