Launching program on system startup not working - c#

EDIT: I'm new to c#, I'm completely lost on this.
I have a program that is supposed to start every time windows starts up. I have it set it up so when you click a check box, it writes / deletes a regedit, code:
private void SetStartUp()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (LaunchCheckBox.Checked)
{
rk.SetValue("SystemStartupProgram", (defaultDirectory + "\\SystemStartupProgram.exe"));
}
else
{
rk.DeleteValue("SystemStartupProgram", false);
}
}
Where "defaultDirectory" is a custom set path. It's set up to be the directory in which the program itself is in, for example "c:\MyProgram\bin\Debug". The .exe file for this program is located in there.
This code works as far as I can tell, since if I go to task manager's start up section, it shows my program, it's location, and it's state, which is enabled. However, the program doesn't run on system start up.
I have a hunch it might be something to do with a .txt file my application uses. The file is located in the same directory. If I remove the .txt file and run my program manually, it tries to launch it but nothing happens. Maybe the windows start up doesn't find the .txt file, and thus can't start it? Is there any way around that?
EDIT 2: The program is a Windows Forms application. When run, it opens up a form. However, it doesn't show up when windows starts.
EDIT 3: Problem fixed, see answer below for details

After a long bug hunt, I found what the reason was. It was indeed what I suspected, when the program starts it tries to look for the .txt file located in the same directory as my .exe. Turns out when you start a program upon system startup, the directory it looks from is something like "Microsoft\windows32", which is not where my file is located at.
I set my program's directory to it's own folder (my case: bin\debug) whenever the program starts, and only then try to read the file.
The bug being fixed, I'm still going to make a windows service application version of it, since that seems to be the correct way to do things from what I managed to google. Thanks to everyone who helped :)

Related

External file write is triggering anti virus pop up

I have created a small game in C#. The players' location is saved into an external text file called:
GAME_FILE.txt
This file is located in the same folder as the main EXE.
The game loads fine but when the player moves from Location 1 to Location 2, it then writes that to the text file. But as soon as it writes it, Norton Antivirus flags up the following message:
Suspicious process attempted to open a file protected by Data Protector
Target: GAME_FILE.txt
Now, I need to try and fix this because I want to share my game with others and don't want to worry about antivirus warnings appearing on other users PC's.
The code I am using to write to the file, is as follows:
File.WriteAllLines(Application.StartupPath + "/" + "GAME_FILE.txt", PlayerLocation);
(where PlayerLocation is a string representing the players' location in the game).
Now, this works fine when run inside Visual Studio, but flags up the warning when trying to play the game via the EXE file.
Any help here would be much appreciated, as I want to be able to share my game with others.
Try to make sure you are writing in 'safe' paths. Try to write in public documents or temporary folders.
Also, take a look on this thread.
My C# app is getting flagged by anti-virus apps. How do I figure out why?

C# : capturing the file and path of a file when right I right click on the file

I am developing a windows application with c#. I need to get the file and folder name when I right click on a file to use for other functions. Right now I have made the registry keys to get my application running.
When I right click on a file (word, ppt, txt, etc) my context menu shows the option I added, Set Perm/ When that is selected my windows application opens up. In a text box on the form, I want to display the full file name and path of the file the user clicked on. Is there a way to do that?
I've added %0 and %1 in the registry to then end of the arguument to launch my app, but it does not work. I've tried h:\temp\myapp.exe %1 and h:\temp\myapp.exe %0. Putting the %1 or %0 in quotes did not change anything.
I am in Windows 8 using Visual Studio 2017. Searching brought me to this question, but it has not answered my problem yet.
if I understand what you're asking,
to have the path passed in the register,
void Main(string[] args)
the Path class has some useful methods to get to the folder and the string of a string
Path.GetFullPath("the path")
sometimes path is args[0](when you drag the file on icon), but in your case you have to iuse args[1].
I hope I understand what you're asking for
Thanks for the help. The question referenced earlier did provide the answer. I was having issues with the form I was using. After working it with for a while I must have really messed it up. I created a new form and used it, using the code provided in that post i referenced.

Process.Start() exits right away on Windows 7

Process.Start("d:/test.txt"); //simple .txt file works perfectly fine on Windows 8 onward but on Windows 7 (x64) it starts the process and immediately closes it.
I've already tried the following:
Calling through ProcessStartInfo and setting CreateNoWindow=true, UseShellExecute=true and Verb="runas" (though not sure why I had to set this one).
Tried attaching Exit event and it confirms that the process does start but it exits right away and I don't even see the Notepad window open for a blink of a second.
Edit: I've tried it with an image files and few other extensions and they open just perfect. Something wrong with just the .txt files (and/or probably other formats).
I was able to solve this bug just by changing build platform from AnyCPU to specifically x64 (my target machine is x64). This is strange but it solved the problem! Thanks Simon Mourier for this tip.
Its definitely an issue with file association. I have tried it windows 7 and it works fine. Try double clicking on the file and check if it opens in notepad, if it doesn't then configure it to open via notepad.Also you should check the exception that it throws,
If File association is missing then it will launch the Openwith dialog.
If it is associated with wrong program then you can change it manually.
If you want to find association type pragmatically then, I would suggest looking at this answer.
How to I get file type information....
You're saying your code is working fine in other OS and other file formats even in Win 7.
Let's try following checks to verify if things are correct
Verify if notepad.exe is in path
Start -> Run -> notepad.exe should launch Notepad
Double click on a .txt file and see if it automatically opens in Notepad
Verify if Process.Start("notepad.exe") starts an instance of Notepad
var process = Process.Start(file used in step 2); and verify the returned process info in the debug mode and see if says the newly created process is still running or not.
I've had this happen on Windows 7 before. It's likely that your Path environment variable has become corrupted. The maximum number of characters that can be used in the Path variable is 2047. Installing many executables on your machine can overflow the Path variable. Here is a SO discussion that shows some ideas to get around it:
How do you avoid over-populating the PATH Environment Variable in Windows?
If you just need to get notepad running again quickly, you can modify the Path environment variable and just put the system location to Notepad at the beginning of the variable. (ex. "c:\windows\system32\notepad.exe").
And if you're not sure how to modify your Path variable, here is a good how-to:
http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx
What about just using
Process.start("start", "d:\\test.txt")
or
Process.start("explorer", "d:\\test.txt")
or
Process.start("cmd", "/c notepad.exe d:\\test.txt")
If it still doesn't work, try using the straight shellexecute, as described here
Executing another program from C#, do I need to parse the "command line" from registry myself?
https://www.gamedev.net/topic/310631-shellexecuteex-api-call-in-c/

Started Process can't delete file

I have a program that start another program. In the second program, I try to delete a File.
If I run directly the second program, no problem, the file get deleted. But if I start the second program from the first program, I get a System.UnauthorizedAccessException.
My guess is that the second program doesn't get all the access of the first program.
I tried many suggestions I found but none of them worked.
Adding Process.StartInfo.Verb = "runas" didnt work.
Adding a manifest file didnt work either (or I did it wrong, not sure)
I set the .exe of the second file to "Run as Administrator" and it didn't work.
Now, how do I fix this?
The first program left the file open, so the second program cannot delete it.
You need to close the file in the first program.
You might be missing a Dispose() call on the FileStream (for example) used to access the file in the first program. That could leave the underlying file in use in that program, although you think the object instances associated with it are gone because they are out of scope.
Post some code if you want better feedback.

c# program works from cmd prompt but not run separately?

I would post a snippet, but I honestly have no idea what part of my code could possibly be doing this. The program is sizable, I don't want to make you all wade through it. What kinds of things could possibly be the cause of this? Everything works perfectly when called from the command prompt: "readoo.exe". But when I click the exe in its file. . . "readoo.exe has encountered a problem and needs to close. . ."
this is intended to eventually be a scheduled task -> i'm worried, will it work?
i've never debugged, all i've ever used is notepad. I am learning, and feel that this strengthens my understanding of a project.
it crashes nearly immediately. there are no shortcuts, though the file paths are relative.
trying this method: shortcut -> properties -> shortcut -> Start In. I don't have a "shortcut" option
my program reads log files, parses, and creates 4 new files based on the found content
Microsoft Error Report says file not found. But how can this be? the files are there, albeit relative.
Take a copy of your project, and then start hacking bits out of it. When it no longer crashes, you've removed the bit causing the problem.
At what point does it fail when you double-click on it? Immediately, or only when you take a certain action?
You could also add a lot of logging to it, which could indicate where the problem is too.
This is probably looking for a dll that it can't find or is finding a different version from what it wants.
You could try Process Monitor or Process Explorer from sysinternals to see what dlls it loads when it does work and where it finds them.
Try putting a System.Diagnostics.Debugger.Break()call as the first thing in Main() and you'll be asked to attach a debugger - this should definitely show you what is different betweent the 2 invocations.
I would start with identifying what is different in the two methods of execution. Is there a shortcut modifying anything?
The starting directory?
The execution account?
command line arguments?
There are 2 things that it could be:
The current directory could be different when you click on the program or run from the command prompt.
The settings and path could be different when you click on the programe you are using the standard command prompt, are you opening the visual studio command prompt when you run the program from the prompt.
If your application relies on some file that should be on the same path of that exe, that can occurr.
You will have to change the properties of the exe (or shortcut to the exe) to "Start In" the directory where your exe is. For a shortcut, right click on the shortcut -> properties -> shortcut -> Start In.
I guess that is what I think could be the cause.
EDIT: Add a Console.ReadLine towards the end of your code to make it pause for you to see any exception thrown. That should help when you run it using windows explorer.
Put a try/catch around your code and output the exception message to the console in the catch block. That should give you some clues.

Categories