automatically run program when computer boots - c#

I have designed an application which should greet the user when he turns on his computer.The program works well on my pc ,but when I transferred the .exe file of the program on my brother's computer it doesn't work meaning that "it doesn't work automatically when the pc boots"...How can I overcome this problem?? this is my code
Note:the program runs well if i executed it manually,but I want it to work automatically.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
namespace helloMSG
{
public partial class Form1 : Form
{
RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
public Form1()
{
InitializeComponent();
reg.SetValue("My app", Application.ExecutablePath.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
`

If you want the program to start after Windows has booted then copy the executeable to the Startup directory:
Click the Start button Picture of the Start button , click All Programs, right-click the Startup folder, and then click Open.
Open the location that contains the item you want to create a shortcut to.
Right-click the item, and then click Create Shortcut. The new shortcut appears in the same location as the original item.
Drag the shortcut into the Startup folder.
Here is the source: http://windows.microsoft.com/en-gb/windows/run-program-automatically-windows-starts#1TC=windows-7.
If the program does not run on your friends PC, then make sure the .NET version is correct. There may be more information in Event Viewer telling you what the problem is.

The target computer should match requirements, at least the correct .net version should be installed and other dependencies, if any.Besides admin privileges might be required since you are writing to registry.
One more thing. You really don't need a winforms application for that. A Console one should do.

Related

Selenium C# click "Save" on edge

I have seen a lot of posts about how to click "Enter" on the "Save as" window in Edge but none seem to be working. I am trying to download a file on my local machine but it seems that the ctrl + s works but enter doesn't. I am using the following:
using Framework.Core;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;
using System.IO;
using System.Linq;
using OpenQA.Selenium.Interactions;
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
using SeleniumExtras.WaitHelpers;
The code that doesn't seem to be executing:
driver.SwitchTo().Window(driver.WindowHandles[1]);
Thread.Sleep(4000);
Actions savePDF = new Actions(driver);
savePDF.KeyDown(Keys.Control)
.SendKeys("s")
.KeyUp(Keys.Control)
.SendKeys("test.pdf")
.SendKeys(Keys.Return)
.Build()
.Perform();
The thing is when the invoice I want to download generates it automatically opens and focuses the new tab so I am not sure if I even need the driver.SwitchTo().Window(driver.WindowHandles[1]);
The first part where it clicks control s works but the second part where it supposed to, when the "Save as" window appears rename the file to "test.pdf" then click "Return" doesn't work. The "Save as" window appears but the test passes and the browser closes while the "Save as" window stays open.
I have also tried AutoIt and it same exact thing happens.
I have tried multiple ways but in the end I just stuck with disabling it asking for a download in pdf's. Edge sadly looks to be really limited compared to Chrome.

WPF MainWindow won't open when run by outside source, such as a separate program or windows 10 startup folder. Why is this happening?

I'm trying to make a program that will, on startup, open a WPF window and open a browser window. It is able to open the browser window and will still open the WPF visual-studio window thing when run by clicking on it, but when placed in startup it will only open the browser and will not open the other window.
When just placing the file in startup didn't work, I tried making a different program in startup to the original file, which would be somewhere else, but that has the same problem, even when not used on startup. I also tried adding a line to make the window show in addition to the InitializeComponent() step Visual Studio already added for me, but that didn't do anything. I couldn't find anything online that answered my question. Segments of code below:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
public MainWindow()
{
InitializeComponent(); //Supposedly will open the window but doesn't
Window window = Application.Current.MainWindow; //Create an object reference to MainWindow
System.Diagnostics.Process.Start("https://www.website.extension"); //Open Browser Window
System.Media.SoundPlayer player = new System.Media.SoundPlayer("song.wav");
player.PlayLooping(); //Play song
window.Show(); //Force MainWindow to show (doesn't actually work)
}
The program is supposed to open on startup, but instead it only opens the website. When run in visual studio, it returns no error message and functions as normal. The problems only start when it is run by an outside source, such as startup or a different program.
Thanks for any help,
Nat
(I'm also pretty new to programming so try to explain what you mean if you use any big programming terms.)

Console Applications in C# produce strange effect on other applications

Hello and thanks always for your help.
I am having a strange problem here and I would like to hear your advice, comment or ideas about this. My trouble is that I can not reproduce the problem in my development computer
The situation
I have built a console application (in C#). It works great. Now other people are using it.
In the computer of one of the users, there is another application. By the sight of it, it is a windows application. I don't know the code of it, and I have been told in rather vague terms that "it was built in C#".
That application is basically some initialization in a window, close that window and then a login window appear with a button, two text boxes one which is for a password.
The problem that has been reported to me (and attributed to my console application) is that when the console application is running and not minimized, and then the other application is started the login window that appears does not have the first textbox in focus. Meaning that if I start writing nothing happens and I have to explicitly select that textbox to write in it.
When my console application is minimized or not running, the other application starts with the first textbox in focus (I can simply start writing- no need to select the textbox)
Now, my console application is rather complicated so I built a very simple console application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleFocus
{
class Program
{
private static volatile bool keepRunning = true;
static void Main(string[] args)
{
//We manage to set the way to finish this program with CTRL+C
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true; //execution continues after the delegate
//we leave the whole program entirely
keepRunning = false;
};
while(keepRunning)
{
Console.WriteLine("I am still here");
Thread.Sleep(2000);
}
Console.WriteLine("Hit enter to finish...");
Console.Read();
}
}
}
and also a very simple windows application.
I tried the effect of both in the other application.
As expected even the simplest of the console application causes the loss of focus on the other application. Meanwhile the windows application does not cause anything.
So it seems that somehow console applications that catch ctrl-c to finish interfere with this application. (I have built my own applications and this does not happen at all)
Any idea why this could be happening??
Thanks in advance

Deployment of a C# Windows Forms Application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am attempting to create a simple install-able form application using Visual Studio Community 2015.
I have followed this walkthrough: https://msdn.microsoft.com/library/k3bb4tfd%28v=vs.100%29.aspx
As the setup project templates were not originally present, I used https://visualstudiogallery.msdn.microsoft.com/f1cc3f3e-c300-40a7-8797-c509fb8933b9 to add them.
I am able to successfully make the installer, as well as successfully install the app. However, when I try to run the application (by going to its installed folder), windows explorer just sits there, like it is trying to do something, but never does. I end up needing to restart the computer in order to cancel this process that never starts.
The same behavior is seen when I try to run the built application on its own, without the installer, running the executable generated from building the actual application. (diving down into the bin/ of the project).
I have seen similar behavior from new, indev programs before (https://github.com/Storj/driveshare-gui/releases), but would not know if the issues are necessarily related.
The app itself runs fine when being debugged by Visual Studio. I used the Visual C# "Windows Forms Application" template to create the original app.
Please let me know if there is anything you would need to see, I would not know.
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Parking_Variable_Editor {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
Constructor & onload from MainForm.cs (the only form in the project)
public MainForm() {
InitializeComponent();
}
private void onShown(object sender, EventArgs e) {
refreshForm();
}
Update:
I have gotten the project to run without debugging. I did this by instead refreshing the form when it is shown, as onLoad seemed to try to do it before things were initialized. Now, however, the same issue is only happening for the installed application.
I've seen this problem before -- and others have too:
C# app runs with debugging, but not without
Here are some ideas.
Most likely when the app starts it is failing when attempting instantiate your main form. If you run in Visual Studio, it may self-heal the problems you've created and make you think it'll run by double-clicking the EXE.
Look in the constructor of your main Windows Form.
Look in the OnLoad of your windows form.
Set a breakpoint in the Program.cs file where the your main form is instantiated and then step into the form code -- run using F5 in visual studio so it will run the debug copy.
Post the code from your Program.cs and your main form constructor and OnLoad methods so we can see it.
Edit:
The RefreshForm() code is 99% most likely the problem. Need to see that code, but I'm betting if you comment out that line the app will run.
The first thing I would do is take that RefreshForm() method and put it in a OnLoad event, wrapped in a try..catch block
try
{
RefreshForm();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
That way if RefreshForm is the issue then you will get the reason, and the app will at least start.
If you don't see a message box, then it is something more fundamental.
So it is clear for future reference, what solved the problem:
removed function that played with form elements from constructor and onload, moved it to an onShow event-tied function
Cleaned and rebuilt everything.
I also needed to add "Runtime Implementation from 'project name here'" and "Localized resources from 'Project name here'" in order for it to run. (this was not specified in the guide I followed)

can't access Keyboard class under System.Windows.Input

I'm working on a simple console app that just show stuff on the screen.
I got into a problem with System.Windows.Input classes, I just cant get to them.
Here is the code I'm trying:
using System.Windows.Input;
public class KeyboardHandler
{
public void UseKeyboard() => System.Windows.Input.Keyboard // Its like the class Keyboard not exsit.
}
I'm using a librery TestStack.White to do some manipulation on a window that I start from my application.
TestStack.White itself has a Keyboard class (White.Core.InputDevices.Keyboard), but could it be that this class is stopping me from accessing the System.Windows.Input.Keyboard class?
Since this is a Console Application, you'll need to add a reference to PresentationCore.dll. This assembly will not be referenced by default in a Console Application.
You can see this in the documentation for Keyboard:
Namespace: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
This is one of the main WPF assemblies, and included by default in WPF projects, but not console applications.

Categories

Resources