Application start on Windowsstart with Parameter - c#

I have programmed an Application, which puts your Directory to HTML. You have to put in three strings: the first for the where to safe Path, the second for the which Path should been put to HTML and the third for the name, which the first HTML file gets.
My Question now is, how can I make my program start on Windows startup and then give it the parameters to Update the HTML files? I somehow need to safe the paths in a file and read this on startup which then executes my program with these parameters.
Do you need any more information? I'm not sure if I was clear enought, but I hope on fast response.

As far as I understand you want to do 2 things:
First, you want to add your program to the startup. Therefore, follow: how to add programs to startup
Secondly, you might want to consider to use (app) config files, which are read by your program when it executes.

Related

Bind two files in one executable

I'm currently in the process of developing a small multi utilities tool in C# and one of the tools I am wanting to implement is a tool that will bind (File binder) one executable with another so that executing one executable will execute both of them in a way of merging the executables into one file.
Please take a look at this screenshot to understand more.
Figure 1:
Button 1 is the button which you click to select the first executable.
Button 2 is the button which you click to select the executable you want to bind with the first executable
button 3 is the button which you click to bind the executables and save the newly built executable to the desktop.
I need some advice on how I can do this, I am going to use codedom to build the file.
If by binding you mean chain-launching, then TheGeneral gave you the most simple suggestion in the comments, which to to generate a batch file with 2 lines of code (possibly more, for usability reasons):
Start "C:\...\...\executable1.exe"
Start "C:\...\...\executable2.exe"
Or better yet, use relative direcotries, which will start the executables that are in the working directory of the bat file:
Start "executable1.exe"
Start "executable2.exe"
Also, keep in mind that it might fail (if it needs admin rights at any point at all) without any errors on Windows 10, unless launched as administrator.
If you are looking for a method to modify an executable to launch a second executable, I don't believe you can tackle this project if you have no idea where to begin. If you want to modify executables to work on any system, you need to decompile the first file, modify it to launch the second one and recompile it, which is probably not difficult to do manually, but not easy to create an algorithm for.
Your 3rd option is to look into executable embedding (essentially creating a program, that contains your main and "bound" program, and when executed copies them out and launches them). After a quick google search, I've found that it is possible, but much like modification of executable, will require some effort to learn and implement.

C# add Embedded Resources to project while running

This is a weird question because i tought about it for some time and it doesnt really make sence to me because the program would need to re-compile, but, because if it is possible it would make my program much simpler, i need to know it there is a way to do it.
Basicly i have a .exe program.
The program has a combobox, that gets its value off a .txt file.
Its pretty simple to edit the values on the combobox, all the user has to do is to edit the .txt file and restart the program, but I need to make so that, if we move the .exe to another computer, it brings the .txt with him, so i added the .txt to my project.
The problem:
I need the user to, if needed, edit my .txt file.
If he edits the .txt file, it means i would have to update the file embedded in my .exe and from what i have searched, its not possible to do that.
My idea to solve this:
When the program starts, i copy the .txt out of the program, delete the embedded file on the project, and when the user closes my program, it copies the .txt that's on the outside, inside, like if i were to manually add it to the program again, that way if the user did edit it, if he moved the .exe to another computer, it would have its combobox values updated. It makes some sence to me, thats why im curious to see if it is possible, and if so, how do i add a external file to my project while it is running.
Thanks!
You could try the other way around.
You can add an application configuration file and in that you can maintain the values for your comboBox inside appSettings section with comma separated values. During the load it will take from config and during closure of the application you just need to update the appSettings section with the updated value.
Code to update the configuration file:
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["comboKey"].Value = "comboValues with comma separated";
config.Save(ConfigurationSaveMode.Minimal);
When you are copying your application to some other system then you should copy the app.exe.config along with the app.exe file.
If you are trying with any installer file in different system and you would to have a user settings, then you could also use the Settings.setting file with User level scope variables.

C# Change File Location For Next Time Program Runs

I am relatively new to C#, however I do have some basic knowledge of code from courses in high school and university. However, there is one thing I have not been able to figure out over the years. I am currently making a Form Application for a database system that stores information in a List using Visual Studios 2010.
On my main form; when the save button is pressed, the information is then serialized into an XML file. When the information is loaded, the information is then deserialized and put into the List for use in the code. All this is working correctly.
This process of saving and loading is done based on a string which contains the file path. This string is the location of a folder on my desktop (I put it there for easy access), and I am able to change the string in the code to basically move where the information is stored.
However, I have a separate "Admin" form which is able to change this file path string. When the user clicks the button to change the file path, I get the input from a text box, check its formatting, move the current file to the new location and update the location for the save method so changes can be saved before the program is closed. From there, the program reacts the same way as if I had changed the string from inside the code.
The problem occurs when I close the program. I do not know how to tell the program when it runs again that the location has been changed from the default and look for the file in the new location. The program reacts just like the file was missing (like it should) when it looks in the default location.
So basically, how do I tell the program that the save location was changed from when it was last run so it knows to load the info from a new location?
I have tried looking for an answer since high school (about 2 years ago) and have not found a solution. As a result I usually just keep the save location as the default (which I set it to) and don't try to change it. But this time, its important that the save location can be customized. My experience with Visual Studios is limited, as everything I know is from messing around with the program and looking up stuff when needed.
If needed, I can post snippets of my code. Thank you in advance!
It seems like what you really want is to save some user-defined settings for recall at run-time. Here is a MSDN link describing some basic conventions for storing / retrieving these settings.
https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
A *.config file would suffice (depending on the scale of the application).
Otherwise, you may want to go down the route of storing these settings in a database (if the scale is rather large, or if user-authentication is required for the application).
Here is another previous question dealing with this same subject (regarding App.config files):
What is App.config in C#.NET? How to use it?
I recommend using a config file where the .exe is, and write the location there, then read it in on program startup.
In particular .net provides this class which can manage your config file for you (assuming you have an app.config in your solution, otherwise create one)
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

Writing a Dynamically built HTML to a file in ASP.NET 4.0

I have a web form which takes in user information. The value of various text boxes is used to build a html file. I write this html to a file( with specific name) and then prompt user to Save this file.This html is used for creating outlook email signatures. Currently I have this html within the application.This has been deployed to the server. I had to set write permission on this file for all users for it to work.
Are there any security risks? What happens if multiple users access this applications and write to the file at the same time.
When you say the file has "a specific name", do you mean that it is always the same name? If so, then yes, there will be problems if multiple users use this functionality at the same time. They'll be overwriting the one file and downloading each other's data. You would need to generate a unique filename each time the process runs to avoid this.
But do you actually need to save the file?
Or is your goal purely to produce some HTML for the user to download, and the way you are doing this is by writing it to a file, and then prompting them to download that file?
If you don't need to save the file, but rather just need to generate HTML and prompt the user to save, just serve it up as a normal page, and set response headers such that their browser will download it. Something along these lines:
Response.AddHeader("content-disposition", "attachment;filename=my_file.html");
From what I understand, the user fills the web form and submits. Immediately, an html file pops up for download from the server. I think this is very neat implementation of this scenario. You just need to make sure that resources are released properly in order to prevent locking of files.
When multiple users access this application, it should not break since separate files are created with a specific name (as you have mentioned). I don't know what logic has been used to create unique names. At some peculiar situation (this is purely dependent on your name calculation logic) if the calculated specific file name somehow becomes similar to an existing file, you should have code in place to replace or create a different version of the same file name. Locking could occur if you are writing captured data from web form into the same file again and again without disposing your stream/File objects . Make sure you dispose your objects after use in the code.
It would be great if you give access to the application pool of the web application to a user who has write access to that file/folder instead of giving everyone the write access. In this way, your application gets full rights to perform write operations rather than users having rights. If users have write access on the file/folder, it is very easy for anyone to peek in and do something unexpected.
Hope this helps.

Generate an .exe file with embedded settings

I have a window application which performs some tasks, One of which is opening some files and we all know .net provides exe file for the application, which can be used as click to start.
I am calling this application application1.
Now I want to generate one more window application(simple exe), let us call it application2, which will open a form with some options(say the names of the files to be opened by application1) and a generate button.
On clicking the generate button, it should generate the exe file for the application1 with the data passed from application2.
Please suggest how can I do it.
EDIT
I need to generate exe which will be available on different systems which will perform some task on regular intervals. and the interval colud be different for different computers.
so I am asked to generate which will accept the time interval and will generate the exe for that interval
There are a number of ways to consider doing this:
use a reg key with the name of the settings file to read in, and then store the settings you write from app2, for app1 in the file, so app 1 can run it
you call app1 with a parameter with either the name of a file, or commandline parameters, and it updates its own applications settings file.
put the settings in a database, so any copy of app1 anywhere can find it, assuming all users would be able to see the db server
if app1 is always to be running while app2 is you could go with some interprocess communication but probably this is the more complex of the 4
Rather than recompiling an exe, it would make sense to have a config file that goes with.
Failing that, compiling .net is only that, you can have an exe that generates a .cs file (or updates one, and reruns the whole compile and outputs an exe.. take a google, on command line compilation) but I wouldnt be my choice.
I think what your looking for is for application1 to be able to receive command line arguments and application2 to allow you to pick files and run application1 and pass in those arguments.
I don't think its wish to be generating .exe's
Another way, although not that easy, would be to
write application 1 to try and read settings from class that does not exist, say SettingsOverride, by reflection; if not found, fall back to its own hard-coded settings
application 2 uses CodeDOM or similar to create a new assembly that provides a SettingsOverride class with the new saved settings
application 2 uses ILMerge to build the new .exe from application 1 and the settings assembly; the reflection code in application 1 should now pick up the new settings.
It's probably also possible to do this with embedded resources, though I'm not sure how. Finally you could put a string constant in your .exe, say 400 X characters, and then application 2 could scan the file to find that (as Unicode/UTF-16 text) and replace that with a string containing the new settings - but I'm not 100% if you then need to recompute a checksum or similar.

Categories