I would like to give my users the ability to configure some php script to interact with my applycation.
I would like to give the user a Memo. The user writes some code with a php syntax, then press execute. The php engine executes the code, so I can print a result.
For example I would like to write something like:
PHPassembly = Assembly.LoadFrom("php5ts.dll");
ExecutePHPScript(PHPassembly,StringContainingAScript);
ResultVar=GetPHPVar(PHPassembly,"ResultVar");
I don't have a web server. I don't have an internet connection. Only a local application for windows.
I have tryed to load the php5ts.dll, but the compiler says that I need an assembly manifest.
Someone knows how to interact with php?
You need 2 files (from php-5.3.5-Win32-VC9-x86) php-win.exe and php5ts.dll
Than just place those 2 files in you executable directory and run:
string code = "echo 'test';";
System.Diagnostics.Process ProcessObj = new System.Diagnostics.Process();
ProcessObj.StartInfo.FileName = "php-win.exe";
ProcessObj.StartInfo.Arguments = String.Format("-r \"{0}\"", code);
ProcessObj.StartInfo.UseShellExecute = false;
ProcessObj.StartInfo.CreateNoWindow = true;
ProcessObj.StartInfo.RedirectStandardOutput = true;
ProcessObj.Start();
ProcessObj.WaitForExit();
string Result = ProcessObj.StandardOutput.ReadToEnd();
MessageBox.Show(Result);
You can try hosting the php runtime locally. There are preconfigured packages for that, with PHP + Apache, like xampp and WampServer. This way, you can call it via HTTP requests to localhost (such an approach is discussed here).
With a bit of configuration,you could execute your code against php running on the command line.
See php.net for more info
in search of something similar myself I found this question.
maybe port and include code from http://ph7.symisc.net/index.html if you don't need too fancy php stuff?
Related
I have A requirement to run SAS script through Web application using asp.net and c#.
I have used ProcessStartInfo to execute SAS script. This works fine locally with solution.Once i hosted the application in IIS, it is not working and returning exit code with 111. Please help me to solve this issue.
ProcessStartInfo info = new ProcessStartInfo("path of SAS EXE","file path");
int exitCode = 0;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
p.WaitForExit();
exitCode = p.ExitCode;
Have you considered not doing it using process? SAS has a technology called Integration Technologies which you most likely have. It interfaces using a COM interface. You can then execute SAS that way and pass what is needed vs using a command line call.
Here is sample code:
SAS.Workspace ws = new Workspace();
LanguageService ls = ws.LanguageService;
StoredProcessService sp = ls.StoredProcessService;
sp.Repository = #"file:" + #"x:\temp";
sp.Execute("test.sas", string.Empty);
string log = ls.FlushLog(1000);
If you need to do it via the process start, here is code:
Also, if you are passing commands to SAS, I don't see any. You have to pass commands to SAS from command line (program name at a minimum). info.Arguments is a start. Also, redirect the std output to a file. Look at info.RedirectStandardOutput and info.RedirectStandardError. However, i don't believe that is the issue. I think you are encountering a security issue. Look at Event Viewer and see if it pops up. IISS requires security to execute in a directory.
Finally, why are you using IIS? Unless you have a legacy requirement, IIS should not be used. Switch to Kestrel and ASP.NET Core. I will be presenting a paper at SGF on the use of SAS in this way. Download the paper and code as soon as they are available (next week?)
A requirement has arisen that I need to start a Node.js server from a C# application, this is as simple as running a server.js script within the Node.js console. However, I'm not entirely certain how exactly to achieve that.
Here's what I've looked into so far:
In the Node.js installation, there's a file called C:\Program Files (x86)\nodejs\nodevars.bat, this is the command prompt window for Node.js. To start the server, I could possibly be using the following steps:
Execute the nodevars.bat file.
SendKeys to the new process console window to start the server.
This approach feels a bit fragile. There's no guarantee that the target user will have their Node.js installation in the same place, also sending keys to a process may not be an ideal solution.
Another method could be:
Write a batch file that executes nodevars.bat.
Execute the batch file from the C# application.
This seems like a better approach. However, the only problem here is that the nodevars.bat opens in a new console window.
So to the question(s), is there a way I can start a node.js server script using functionality built into the node.js installation? Perhaps sending arguments to the node.exe?
If it is to serve multiple users, i.e. as a server, then you can use the os-service package, and install a Windows service. You can then start and stop the service using the standard API.
If you are to start the server as a "single purpose" server, i.e. to serve only the current user, then os-service is the wrong approach. (Typically when using this approach you will specify a unique port for the service to use, which will only be used by your application).
To start a batch file or other Console application, from C#, without showing a console window, use the standard method, but be sure to specify:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false; // This is important
psi.CreateNoWindow = true; // This is what hides the command window.
psi.FileName = #"c:\Path\to\your\batchfile.cmd";
psi.Arguments = #"-any -arguments -go Here"; // Probably you will pass the port number here
using(var process = Process.Start(psi)){
// Do something with process if you want.
}
There are a few different ones but I recommend the os-service package.
I would like to integrate FileZilla with my application written in C#.
please someone show me sample code or web site that shows sample code.
although i found article on web, and that article was saying
"application is integrated with FileZilla is so slow".
but i don't know if i can stand that late or not.
so i would like to challenge.
To support FTP/SFTP or any other protocol in C# you can do it in 3 ways:
1. NEW APP PROCESS - Start an app that does the FTP communication in separate process, and be able to control what file to download, where to save it and to tell the app to terminate when download is finished. This way, you can use FileZilla only if it lets you pass certain parameters in command line, like the URI of the resource you want to transfer through FTP/SFTP, and the path where the file should be saved to. And as I can see HERE this could work.
To start the process and pass it command line arguments in C# you would do something like this:
static void StartNewProcess(string app, string args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = app; //full app path
startInfo.Arguments = args; //command line arguments
startInfo.CreateNoWindow = true; //dont create app window
startInfo.WindowStyle = ProcessWindowStyle.Hidden; //hide app from taskbar
Process.Start(startInfo);
}
Now you can execute FileZila app, pass it args containing file URL and let it do its job... But you cant know how long will it take to download the file, when the download is ended, do you need to log in to get it...
2. EXISTING CLASS LIBRARY - Include a Class Library that is written by someone else, that does the job. This way you are in TOTAL control of the process. And as many other suggested, this would be a perfect way for you. Many answers here contain good class libraries that you can use and be happy with the results.
3. HOME-MADE CLASS LIBRARY - Open RFC 959, read it all and write your code... (Now 2. sounds better, doesn't it? :D)
Filezilla is a GUI FTP client, you can't use it to "script" SFTP operations (it only accepts a very limited set of command line arguments).
You must seek a third party C# component or write one yourself (not recommended) to do the job.
To support FTP or SFTP from your C# application, you could use an external library like the one from Chilkat http://www.chilkatsoft.com/ftp-2-dotnet.asp. I use it and it works great!
In theory, you could also implement the FTP protocoll using socket connections by yourself, but you should save yourself that trouble -> don't reinvent the wheel...
I recommend using SharpSSH, if you need to send files via SSH/SFTP in your application.
I'm a C# game programmer with little web development experience.
I need to upload a small file (25-100 or so bytes, depending on it's content) to a server. This is on the Windows Phone 7 using XNA. The target server is fairly limited and only supports PHP and classic ASP.
Since the CF on the WP7 only has access to a limited subset of networking commands, it's looking like an HttpWebRequest GET aimed at a script that saves the file will be the best option. The data I'm sending is small in size, and should be able to be passed as a parameter in the url.
I've been searching but have yet to find a complete example of this, which handles both the client and server side script (mainly the latter). This is close to what I'm looking for, except it has no mention of the server side script: Upload files with HTTPWebrequest (multipart/form-data)
The closest that I got was this: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-server/
But when attempting to use it I get an unhandled exception: "The remote server returned an error: (405) Method Not Allowed". This method seems the most promising so far, but I've yet to be able to debug this.
Unfortunately, I have a short amount of time to implement this, and as I said only a passing familiarity with web development. I'm not worried about maximum security or scalability as this is a temporary measure to collect feedback internally. Basically, I just need the quickest thing that works. ;)
Any help would be fantastic!
I've solved it. First off, PHP wasn't supported on my server (just now learning that PHP and ASP are can't be used on the same server, depending on whether it's on Linux or Windows - like I said, web development noob here!). I switched to ASP and, after digging through the docs, wrote this script:
<%
dim theData, theFileName
set theData=Request("data")
set theFileName=Request("filename")
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.MapPath(theFileName+".txt"))
tfile.WriteLine(theData)
tfile.Close
set fname=nothing
set fs=nothing
set theData=nothing
set theFileName=nothing
%>
This C# code uploads the file:
const string cAddress = "http://site.com/folder/upload.asp";
string fileName = foo;
string data = bar;
string address = cAddress + "?filename=" + fileName + "&data=" + data;
uploadRequest = (HttpWebRequest) HttpWebRequest.Create(address);
uploadRequest.Method = "GET";
uploadRequest.GetResponse();
Hope this helps someone else looking for an example of how to do this!
But you have the METHOD as GET instead of POST. You can't upload a file to a website by passing the file path to the Query String.
I have a developer tool that I want to run from an internal site. It scans source code of a project and stores the information in a DB. I want user to be able to go to the site, chose their project, and hit run.
I don't want the code to be uploaded to the site because the projects can be large. I want to be able to run my assembly locally on their machine. Is there an easy way to do this?
EDIT: I should note, for the time being, this needs to be accomplished in VS2005.
EDIT 2: I am looking for similar functionality to TrendMicro's Housecall. I want the scan to run locally, but the result to be displayed in the web page
You could use a ClickOnce project (winform/wpf) - essentially a regular client app, deployed via a web-server. At the client, it can do whatever it needs. VS2005/VS2008 have this (for winform/wpf) as "Publish" - and results in a ".application" file that is recognised by the browser (or at least, some browsers ;-p).
You might be able to do the same with Silverlight, but that has a stricter sandbox, etc. It would also need to ask the web-server to do all the db work on its behalf.
I want to be able to run my assembly
locally on their machine
Sounds like you want them to download the tool and run it from their local machine, does that work for you?
Any code can scan files given the location and permissions. For a website to open an exe on a different machine and permit that to run and get access to the files contained on the web server would require a horrifically low level of security that would mean the entire system is practically completely open to attack. If your system is completely behind a firewall and hence protected from outside intererance then you want to look more at the permissions and less at the code.
To run an exe on a machine try following notepad example, though you may have to use a specified directory as well
ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(ExitHandlerToKillProcess);
p.StartInfo = psi;
p.Start();
and when done dont forget to kill the Process. Alternately use javascript. Either way watch the security permissions and remember the risks of doing this.
I would probably write some sort of command line tool or service that does the processing and extraction of project data. Then I would use a page to update/register projects that the web server and the command line tool both have common access to. then at specified times either manually or via cron or similar mechanisms extract the data to your database. once you have this, you just use the website to display last extraction times and the extracted data.
if the projects/end users are on a different subnet etc, then you will need the end users to run the tool and then have it post the data into the database.