I have created a windows form program that does some Business Intelligence but have subsequently needed to access this via a webform. I am fairly new to programming so I don't know what can be achieved here. But essentially I am wanting someone to go on to the net and when the user presses a button it sends a message to the windows form executable in a file and tells it to run and then press a button on the form, which runs a method to generate images of graphs. Is this possible?
Here is the relevant code.
In the webform I have this button.
protected void rolloutSmartSheets(object sender, EventArgs e)
{
string message = string.Format("Starting processes");
ltMessage.Text = message;
Process process = new Process();
process.StartInfo.FileName = #"P:\Visual Studio 2013\Projects\Smartsheet\SmartsheetAPI\obj\Debug\SmartSheetAPI.exe";
process.Start();
message = string.Format("Ended all processes");
ltMessage.Text = message;
}
That runs the executable but it opens the windows form and I imagine if the executable is sitting on another computer wouldn't that open on that computer? In which case i want it to tell it to press this button on the windows form which runs the method I need and then the user doesn't need to worry about it.
public void commitToDatabase_Click(object sender, EventArgs e)
{
commitToDataBase();
}
If you are able to have the clients install something in advance, you can provide this functionality using a custom protocol handler.
Example protocol handler from MSDN Article
HKEY_CLASSES_ROOT
alert
(Default) = "URL:Alert Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "alert.exe,1"
shell
open
command
(Default) = "C:\Program Files\Alert\alert.exe" "%1"
Then add a link onto your webform like this
href="alert://test"
As long as the client has the handler installed, both in the registry and the executable file, it will run C:\Program Files\Alert\alert.exe, passing "test" to it as the first paramater.
You can easily change this to provide the ability to run the local graph generator, and pass any parameters from the webform you might need.
Related
i want to execute a .EXE file in mvc 3 application when we run this project locally on browser it work perfectly . Problem is that when we publish this project and host it on the IIS(8.5) server on window 8.1 when we click the button code executed and process start in Task Manger but app not shown on front .
For examples in this case we execute notepad.exe file .
Following is our code in html we make a one button when user click on that button controller method is called that run code .
when user click on button this is cod that is executed.
$("#Button11").click(function (event) {
$.post("/Account/Start_Appl", {}, function (data) {
});
event.preventDefault();
});
and start Start_Appl Method inside Account controller have following line of code .
public string Start_Appl()
{ string path="C:\\Windows\\System32\\notepad.exe";
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.LoadUserProfile = true;
psi.WorkingDirectory = path;
psi.FileName = path;
Process.Start(psi);
return "ok";
}
i want to execute .EXE file with iis(8.5) any solution to this problem .
i check on the internet but can not find any proper solution for this problem.
i also check this link but not give any help .
ASP.NET Process.Start not working on IIS8 (Windows 8.1)
OK, first off, this is a very odd thing to do - there is going to be a better approach for whatever your problem is. That said, what I would do in this situation is:
create a database with a JOB_QUEUE table. exactly what is in this table is up to you, but I'd suggest an ID, a date_added, something to indicate what the job should consist of (e.g. the name of the exe to run?) and a flag to indicate the status of the job (pending/in progress/failed/completed)
modify controller to insert a record into this table with a 'pending' status (this is ALL it does)
write a separate application that runs in a user process (windows forms or console application). this application could run on the server which hosts IIS or a separate one - all it needs it access to the same database
Make this separate application periodically check the job queue table for jobs with a pending status.
When a new job is detected, the application updates the database so the job is 'in progress' and runs the job. if successful it updates the job to be 'completed' and if it fails it updates it to be 'failed'.
It then goes back to simply monitoring the table.
There are lots of other things you could use in place of the database for a queue, but this would be quick to write and easy to debug/test as well as making it easy to add multiple clients and persist historical info, and add controller methods to report on jobs requested/completed and progress.
For execution external .exe file in iis(8.5) and window 8.1 . we used
Custom URL Protocol for Invoking Application Techniques check this link
Custom URL Protocol
i have change my Start_Appl function is as .
public string Start_Appl()
{
try
{
string myAppPath = "C:\\Windows\\System32\\notepad.exe";
RegistryKey key = Registry.ClassesRoot.OpenSubKey("myAppa"); //open myApp protocol's subkey
if (key == null) //if the protocol is not registered yet...we register it
{
key = Registry.ClassesRoot.CreateSubKey("myAppa");
key.SetValue(string.Empty, "URL: myApp Protocol");
key.SetValue("URL Protocol", string.Empty);
key = key.CreateSubKey(#"shell\open\command");
key.SetValue(string.Empty, myAppPath + " " + "%1");
}
key.Close();
}
catch (Exception e) {
return e.Message.ToString();
}
return "ok";
}
we first register a key if it is not already create and set value and after that we make a button on my aspx page is as
<input type="submit" name="Launch" id="Launch" value="Launch Custom URL" onclick="LaunchURLScript()">
When user click on button the function LaunchURLScript is call and inside this function we write following code in it
function LaunchURLScript() {
$.post("/Account/Start_Appl", {}, function (data) {
if (data = "ok") {
alert("ok");
var url = "myAppa:"; window.open(url); self.focus();
}
});
}
for inside we first call controller method to make URl protocol if not already exist and finally we open new window that lunch my note pad exe file .
in this way we solve our problem.
Background
Console Application:
I have a console application that retrieves data from a spreadsheet using the google sheets API. This application is automated by running it every 5 minutes with windows scheduler.
Form Application:
In the same solution I have created a windows form project that can be run manually, outside of the automation process to tweak any settings without disturbing the 5 minute process. (i.e. If we want to change spreadsheet ID to fetch data form a different spreadsheet, or if i want to change the output folder of where the data is being fetched)
My Goal
I'm trying to develop a form project that will edit the "settings.settings" file of another project in the same solution. Below is a screenshot of how i have my solution laied out:
What I've done so far
I've already added a reference from my sheetstocsv project in my SettingsUI Project, and i've successfully created a form that accesses sheetstocsv's Settings and edits them when a save button is clicked. Shown below is the function that's supposed to save the new settings from the form.
private void Save_Button_Click(object sender, EventArgs e)
{
sheetstocsv.Properties.Settings.Default.outputdir = OutputDirectory_TextBox.Text;
sheetstocsv.Properties.Settings.Default.spreadsheetID = SpreadsheetID_Textbox.Text;
sheetstocsv.Properties.Settings.Default.Entity = Entity_Texbox.Text;
sheetstocsv.Properties.Settings.Default.headernum = (int)Columns_NumericUpDown.Value;
string headers = "";
for (int i = 0; i < (int)Columns_NumericUpDown.Value; i++)
{
headers = headers + Columns_DataGridView.Rows[0].Cells[i].Value.ToString() + ",";
}
sheetstocsv.Properties.Settings.Default.headers = headers;
sheetstocsv.Properties.Settings.Default.configured = true;
sheetstocsv.Properties.Settings.Default.Save();
MessageBox.Show("Saving Complete!", "Settings",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
How I'm trying to do it
Below is a code snippet of how i'm only letting the sheetstocsv project continue if the configuration file has configured it first
//Check for if configured
if (Properties.Settings.Default.configured == false)
{
Console.WriteLine("Program has not been configured yet! Please Run SettingsUI first to start this program.");
Console.ReadLine();
Environment.Exit(0);
}
My Problem
Whenever I run my settingsUI, save, and then run my sheetstocsv project the changes that were supposed to be saved are not. and it will not allow the program move foward.
Edit
Below is the full form .cs that shows how i'm editing the properties of the other project
using sheetstocsv;
namespace SettingsUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Columns_DataGridView.RowCount = 1;
Columns_DataGridView.ColumnCount =(int)Columns_NumericUpDown.Value;
}
// Event handlers for other UI elemets here.......
private void Save_Button_Click(object sender, EventArgs e)
{
sheetstocsv.Properties.Settings.Default.outputdir = OutputDirectory_TextBox.Text;
sheetstocsv.Properties.Settings.Default.spreadsheetID = SpreadsheetID_Textbox.Text;
sheetstocsv.Properties.Settings.Default.Entity = Entity_Texbox.Text;
sheetstocsv.Properties.Settings.Default.headernum = (int)Columns_NumericUpDown.Value;
string headers = "";
for (int i = 0; i < (int)Columns_NumericUpDown.Value; i++)
{
headers = headers + Columns_DataGridView.Rows[0].Cells[i].Value.ToString() + ",";
}
sheetstocsv.Properties.Settings.Default.headers = headers;
sheetstocsv.Properties.Settings.Default.configured = true;
sheetstocsv.Properties.Settings.Default.Save();
MessageBox.Show("Saving Complete!", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
I don't quite have enough info from your question (see comment from Rufus), but I am guessing that your console app launches your WinForm? If this is the case, what is likely happening is this:
1) Console app starts, and loads the config file into memory.
2) Your Winform launches and changes the config file.
3) Console app overwrites the changes to the config file on exit since it read it before the changes were made.
You could just move the winform into your console project, then just do something like this:
Properties.Settings.Default.outputdir = OutputDirectory_TextBox.Text;
...
Adding More Info (Oct 1):
I must be honest, doing what you're doing is a little strange, and I would really rethink referencing an executable to change its default settings. Just because VS lets you do this, doesn't necessarily mean you should. But if you must, there are a couple other things you can try:
1) You can simply parse the xml settings file and change node values of the settings you want to change. System.Xml namespace will make very short work of this. The post below has good examples:
Change xml node value
2) Try wrapping the code that is currently in your button click event handler with some method inside the console project. This way, you're calling a method that changes the settings, rather than trying to change the settings from an external assembly.
Note: neither of those methods will work if your console app is running while you're making changes to its config file. On shut down, the console app will overwrite the settings file with the settings it loaded into memory when it started up. If the console app isn't running, 1) will definitely work.
We are develop a windows service for open a specific port.
Now this port can be custom for the user during the installation in a dialog.
I want know a possibility of capture this value and pass to the code of the service
if (myServer == null)
{
int port= int.Parse(ConfigurationManager.AppSettings["port1"]);
myServer = new NHttp.HttpServer
{
EndPoint = new System.Net.IPEndPoint(0, port)
};
}
myServer.Start();
I try using a value in app.config and editing this value in the installer:
public override void Install(System.Collections.IDictionary stateSaver)
{
string portServer= this.Context.Parameters["CTPUERTO"];
System.Configuration.ConfigurationManager.AppSettings.Set("port1", portServer);
base.Install(stateSaver);
}
CTPUERTO is the name of the textbox in the dialog install
You add the optional TextBoxes(A) dialog to your setup project and the user enters that text (in EDITA1 in the docs):
https://msdn.microsoft.com/en-us/library/e04k6f53(v=vs.100).aspx
Then in your custom action you'd add the parameter with something like:
/port1=[EDITA1]
in CustomActionData, then access it using the kind of code you showed, in an installer class.
These might be useful:
.net Setup Project: How to pass multiple CustomActionData fields
https://www.codeproject.com/Articles/12780/A-Setup-and-Deployment-project-that-passes-paramet
The main issue with this (because of the way VS setup projects work) is that you cannot validate it at the time it's entered. Custom actions in VS setup projects run after the UI and after everything is installed, so if your custom finds it's incorrect then you fail the whole install and roll back.
Process: I'm currently developing a Game Launcher through Visual Studio. After being successful with everything so far I have come across "A coding idea" that I just can't figure out how to code.
Attempts:
Base Line (Opens Teamspeak):
private void btnFoxedTs_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(#"C:\Program Files\TeamSpeak 3 Client\ts3client_win64.exe");
}
Joining Server (Through IP):
private void btnFoxedTs_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(#"C:\Program Files\TeamSpeak 3 Client\ts3client_win64.exe://voice.teamspeak.com:9987");
}
Thrown Error:
Any Ideas???
My Plans: I'm wanting to make the button "Auto-Connect" to the Teamspeak server upon, the "Teamspeak Client" opening. Without the user needing to input a Bookmark, Server IP, Anything like that.
If Possible: If this works, would I be able to include a Teamspeak server password, so when they click the button is "Auto-Connects" with a pre-imputed password (Without the user needing to type a password).
Just use the ts3server:// url to run the TS3 client, you can provide all nessecary parameters there.
Basic example:
System.Diagnostics.Process.Start("ts3server://voice.teamspeak.com?password=serverPassword");
The TeamSpeak client registeres itself for the protocol and will be started by Windows it is invoked. You don't even need to bother with the path to the client.
More parameters are described in the official FAQ.
After going through those posts that #GeraldSchneider gave me, I found found this "ts3server://ts3.hoster.com:9987" Parameter URL used in linking your Teamspeak to a Webpage. After playing around a bit, trying out different methods,
I came up with this code:
private void btnFoxedTs_Click(object sender, EventArgs e)
{
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\TeamSpeak 3 Client\\ts3client_win64.exe";
p.StartInfo.Arguments = "ts3server://voice.teamspeak.com";
p.Start();
}
One thing that I must say, after reading the Teamspeak Forums saying that "Command-Line Parameters are not possible", it feels good to get this working.
What it does:
The Code opens up your 'Teamspeak Client' and 'Auto-Connects' to the server that is implemented. The Server opens up in a "new-tab" (If you are connect to other servers) and joined with your pre-set 'Nickname'.
Disclaimer Note: I used the "Default Teamspeak Server Ip" in this post so my personal Teamspeak isn't displayed.
I am building a windows forms application using C# that needs to get launched when a user clicks on a file with custom extension(eg. filename.mycustomextension)
I plan to put a url in the filename.mycustomextension file. when user clicks on this file, we winform application should launch and also read contents of this file.
Is it possible to do this?
First and most obviously you'll need to associate the file extension with the application either by "open with" in the shell, through an installer or directly in the registry.
MSDN - Best Practices for File Associations
Then from there it's really pretty simple.
static class Program
{
[STAThread]
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
string text = File.ReadAllText(args[1]);
// ...
}
}
args[0] is the application path.
args[1] will be the file path.
args[n] will be any other arguments passed in.
Offhand I can't find any examples that show all of this together simply but Scott Hanselman has a nice example of loading files through a single instance WinForms application, about the same...
http://www.hanselman.com/blog/CommentView.aspx?guid=d2f676ea-025b-4fd6-ae79-80b04a34f24c
Yes, it is possible.
The idea is that when your application is clicked, you modify the registry key, to associate the extension file with your application.
Here are the sketches:
Use the FileAssociation class from here.
Initialize it, set all the parameters.
Here is an example:
var FA = new FileAssociation();
FA.Extension = "blah";
FA.ContentType = "application/blah";
FA.FullName = "blah Project File";
FA.ProperName = "blahFile";
FA.AddCommand("open", string.Format("\"{0}\" \"%1\"", System.Reflection.Assembly.GetExecutingAssembly().Location));
//"C:\\mydir\\myprog.exe %1");
FA.IconPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
FA.IconIndex = 0;
FA.Create();
If you plan to launch your application from a browser then there are security issues, but other than that - yes.
You just need to install the application on the user's machine and associate your .mycustomextension with the application. http://support.microsoft.com/kb/185453