how to get variable in desktop c# program from web javascript application - c#

I met a problem that there are two applications. one is desktop application in c#, and another is web application in javascript. some variables or informations in running desktop application are needed to transfer to web application.
Does any one know how to solve this?
Does any one would like to provide more details to solve this?
Regards,

I think the processes are very well separated and unless you use a sole handler for c# + js (c#.net to render your page for instance) it is going to be impossible to retrieve data.
Maybe you can get something from the webbrowser object: running it on your page you might gain access to some of your web variables...
Also, if the c# app writes data in a file, the js could read it...
Have fun
Rgds

When you open the web application, pass the variables in as query string parameters and you can get them using javascript various ways, some of which are noted here

Related

Get Html from Web page and create Setup project for Wpf Application (C#)

I'm trying to create a wpf application such as a movies library because i would like to manage and sort out my movies with a pretty interface.
I'd like to create a library with all my movies getting information from the web, but i don't know how very well.
I thought to get the information from a web site, for example imdb, but i don't know if it's legally to capture html from page to get the nested information.
It's my first desktop application and I would also like to know if it is necessary to create a database within the project and then create a setup project with specified script for deploy it.
Sorry for the confusion but i would like to know too much things :)
Thanks a lot in advance.
The legality of web scraping is a grey area. See my question, "Legality of Web Scraping vs Normal Use" and the corresponding answers for some insight.
Even if the legality is not a problem, web scraping is a flimsy approach because the webpage structure may change without notice, making your application suddenly useless until you update it to the new format. You are much better off using some sort of web API (if the site providing the information offers it).
Whether you need a database or not depends entirely on what your application will be doing and how you design it - it's not something any of us can tell you.
Same goes for the setup project - in fact I wouldn't worry about that until you actually have a working application. Take it step by step and keep the scope within control.
Yes I did not think about api.
It's a great idea, maybe use "themoviedb".
But if i create an application based on it, that has to show all the movies that you have stored on your hdd and get , for example, the posters, the description and the ranking, i have to create a database according to you?
Thanks a lot.

monitoring most common web browsers c#/vc++/c

i'm trying to do a download manager just for learning cos i'm new in windows programming,
could someone tell me how to monitor most common web browsers,
i'd like to implement something like:
http://www.iwisoft.com/videodownloader/video-downloader-features.php
everytime you visit a web page in common browsers detects all video files in the web page and allow you to download or not the file, any idea how to do that without building an app for every browser, which is the best language to do it c#/vc++/managed/unmanaged,
i'm learning and using a mix of all to do other parts like download files, add rules to firewall or modify the registry
thanks a lot
I don't really know a neat way of doing this, but you could try the following :
Enumerate the name of the current window using GetForegroundWindow.
Check if the name you get using GetWindowText matches the usual name of the browser.
If it is a browser, moniter the clipboard and check for hyperlinks
then do your download stuff.
I program in C++ and assembly, but I wouldn't be able to advice you on the programming language since I don't have any experience with C#. But since you are new, I would suggest starting out with basic stuff. As pointed out in your comment, this is not something that can be achieved easily.

how to execute C#.net code in html file?

This will probably sound dumb, but I need to execute a C# code from my html file. For example I just want to execute this
System.Diagnostics.Process.Start(#"D:\Movies\HurtLocker.avi");
Not any server side code.
I can't create aspx page, because to open an aspx page in a browser it needs to be hosted in IIS.
You can't execute server side code from a client side page. If your page is aspx you can use a webservice or click a serverside button from javascript.
Edit: If you want to embed a video player please check this link. You don't need server side code for this. You'll be able to do it with javascript.
Where do you want the c# code to run?
If you want it to run in the browser that is being used to render the html then I'd say that was basically not possible. Something, presumably javascript code in the html page, would have to somehow instantiate a .net clr and pass the c# code to it for execution. The clr does have a COM-based hosting interface that would allow instantiation, but even if you could call this from javascript I think that any sensible browser security settings would prevent it.
If you want the c# to run on the server supplying the html page then you should use asp.net.
EDIT
Ok, you want to run it in the browser. I'm not aware of any examples for hosting a clr in the browser process, sorry.
You can create activex/com objects in js using something like var obj=new ActiveXObject("<comclassname>");, and you might be able to create a CLR that way by instantiating one of the COM classes (maybe CLRRuntimeHost) listed on this page. You could then pass your c# code to your clr for execution. More info here and here. I'm really not sure if that would work, though. I've never used the hosting api, I just know it exists!
Seems like an interesting project to try if you are curious, but deploying this in a real environment would likely present lots of problems. Good luck!
Not C#, but how about .NET dynamic language in the browser with Gestalt? http://gestalt.codeplex.com/
You can create a code block in your ASP file but if this is simple HTML file this is not possible.
At least you will have some application that will read the content of the page compile it and execute.
ASP code block
As Pabuc mentioned you can't execute server side code on the client machine in HTML. If you were to use Silverlight you could execute the code client side, but then the client need to have Silverlight installed and it is not strictly HTML anymore.
Silverlight could be used to play movies client side with C#.
The only way to have C# on client side is Silverlight application [update] or any other browser plugin as #kenny mentioned.
I have tried to find a software for this some time. I have software, which has needs, that are hard to make with traditional Web programming.
Now I have made simple demo, how to create HTML5 online application with C# or VB.NET.
It is Scot library which translates C# to Javascript on time when executing .NET application. It also supports events on Browsers, which is executed in c# code.
To original question:
On the Html page you will need to add single line after :
<script src="myclass.cs"> </script>`
to connect .Net class:
using Scot;
//..
public myclass:Document
{
protected override OnConnect()
{
Elements["mybutton"].OnClick+=new JsInputEventHandler(click);
//your initialization //....
}
private void click(object sender, JsInputEventArgs e)
{
Window.Alert("Click()");
}
}
Demos are quite simple, but actually I needed this library for another project.
It would be nice to have any feedback.

How can I differentiate between the .net WebBrowser component and an actual browser?

OK, so we have an online downloads store accessed via our software. Recently we've had requests to allow downloads via normal browsers and it's fairly easy just to slap a download page on. The problem is that it would be confusing to people having two download links, one for the software and one for their web browser, so we want to differentiate between the two and only show the relevant download link.
From what I've gathered, the .net WebBrowser component is the same as IE and uses the same User Agent, so we can't use that unless we subclass the WebBrowser in the software to make it use a specific User Agent. It's the more sensible option, but we'd have to roll out another updated version, which is less than ideal.
Are there any other ways to tell if someone's accessing a site via the .net component? My only other alternative is to copy the store to a different address with the different download links and send people there. Again this is doable, but not ideal.
Check if window.external is null. IE implements window.external to have methods like AddSearchProvider where most of time WebBrowser.ObjectForScripting is null.
I'm not sure if there is any better way to do this, but here is one idea... The WebBrowser control has a property Document that gives you access to the DOM object representing the loaded document (after the page is loaded). This object has InvokeScript method that you can use to run some JavaScript in the loaded page.
You could write a simple JavaScript function, say hideWebDownload() that would switch the view to a view used when the application runs locally and invoke it from your WinForms application that hosts the WebBrowser control:
webCtrl.Document.InvokeScript("hideWebDownload");
The default view of the page would show the download link for web and calling this function in the local application would switch the view to local download link.
Have your software pass in an invisible (to the user) value in the querystring of the URL.
Trivial to look if that's present.

Silverlight and a form application

I wondering if a can make a form with silverlight like an HTML form and submit its data to a server to store them on a database.
Can I do that?
Thanks!
You can definately do this. But you cannot talk to a server directly via Silverlight like you can with ASP.Net. You have to use web services to achieve this.
There are a number of ways to do this:
1. Use Web Services (Old ASMX; This has security issues)
2. Use WCF (For complex systems)
3. Use ADO.Net Data Services (This is probably the easiest and fastest way to achieve this) using ADO.Net Entity Framework.
4. Use RIA Services (In CTP now for Silverlight 3)
Just take a look at some videos over here:
http://silverlight.net/learn/videocat.aspx?cat=2#HDI2WebServices
basically, you build your data model with Linq (or some other orm), expose that data through Select/Update/Delete/... methods with web service (new WCF or old one, ASMX), and consume that in silverlight. Silverlight automatically make proxy classes for communication. In Silverlight, you can use it's rich databinding capabilities, so you do not need to worry about how data are transferred, serialized, read from UI and similar.
Video tutorials on silverlight.net web explains most of stuff regarding programming SL2 really good.
You can always host the sliverlight app in a web page which the forms app loads.
It may be possible to directly intergrate WPF and forms as well.
Or you aren't running ASP.NET on the server like everybody always seems to assume you are and therefore don't use all these darned "Web Service" things, you can just use WebClient and make your POST that way.
...I dont know why everybody thinks there is a need to layer on so many heaps of acronym goo between you and your webserver. Your old javascript code wasn't interfacing with your backend using WCF, SOAP or whatever, why should Silverlight? Keep it simple! Silverlight is more than happy to make regular, old-fashioned, time-tested HTTP POST's.
And not to be shameless, I've been working on a library I use in my projects to make it easier to call your backend like you would have if you were using javascript: WebBuddy.
Actually there is an easy way to do make an html page in silverlight and fill it with data and post it to the server. it will use Silverlight browser interop to programmatically create an HTML and set elements to it.
//Creates a blank html document
var htmldoc = System.Windows.Browser.HtmlPage.Document;
// Returns a Reference type to the body of html page
var body = htmldoc.Body;
// Create a <form> element and add it to the body
var newForm = htmldoc.CreateElement("form");
newForm.SetAttribute("action", targetUrl);
newForm.SetAttribute("method", "post");
body.AppendChild(newForm);
//Add your elements to your form
HtmlElement input1 = htmldoc.CreateElement("input");
input1.SetAttribute("type", "hidden");
input1.SetAttribute("name", "someName");
input1.SetAttribute("value", "someValue");
newForm.AppendChild(input1);
//submit your form
newForm.Invoke("submit");
That Simple!
original Answer: This Answer

Categories

Resources