Could not open input file: filename.php - c#

In .bat file contains code PHP filename.php I am getting error message after opening the bat file Could not find input file:filename.php . Can anyone give the suggestions please
private void button5_Click_1(object sender, EventArgs e)
{
try
{
string bat = "D:\\folder1\\file1.bat";
if (File.Exists(bat))
{
if (radioButton1.Enabled == true)
{
MessageBox.Show(bat.ToString());
System.Diagnostics.Process.Start(bat);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}

I'm guessing the process is not starting in the same folder as filename.php. Either specify the full path in the bat file or set the startup folder:
Process process = new Process();
process.StartInfo.FileName = bat;
process.StartInfo.WorkingDirectory = "D:\\folder1"; // or whatever is appropriate
process.Start();

Related

calling a bat file in C# IIS does nothing

I have a C# program that is trying to call a bat file in the project folder. Here is the code which is calling the bat file:
protected void btnHotFolder_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.Verb = "runas";
string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Abbyy_Script/restart-hotfolder.bat");
startInfo.FileName = path;
Process.Start(startInfo);
}
catch (Exception ex)
{
log.Error("Batch file error");
log.Error(ex.InnerException.Message);
log.Error(ex.InnerException.StackTrace);
}
}
I have read on the forum that some have suggested giving the IIS user folder access rights to the folder containing the bat file. Tried but no avail..
This is part of the project structure showing where the bat file resides:
project - Abbyy_Script - restart-hotfolder.bat
In the bat file, I am currently testing it with notepad++ exe:
taskkill /im notepad++.exe
TIMEOUT 2
START "" "C:\Program Files\Notepad++\notepad++.exe"
You should improve your batch file:
taskkill /f /im notepad++.exe /t
timeout /t 2 /nobreak
IF EXIST "%ProgramFiles(x86)%\Notepad++\notepad++.exe" (
start "" "%ProgramFiles(x86)%\Notepad++\notepad++.exe"
)
IF EXIST "%ProgramFiles%\Notepad++\notepad++.exe" (
start "" "%ProgramFiles%\Notepad++\notepad++.exe"
)
The C# code needs to be improved too:
protected void btnHotFolder_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Abbyy_Script/restart-hotfolder.bat");
startInfo.FileName = path;
Process.Start(startInfo);
}
catch (Exception ex)
{
log.Error("Batch file error");
log.Error(ex.InnerException.Message);
log.Error(ex.InnerException.StackTrace);
}
}

C# WinForms Application keeps looping

I am making an installer for one of my applications, whenever I hit install and I get to the 'folder already exists' message box I made I click OK but then a new process of my application appears and I don't want it to! How do I fix this? Here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace GladeInstaller
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo;
startInfo = new ProcessStartInfo();
startInfo.FileName = Path.Combine
(Path.GetDirectoryName(Application.ExecutablePath), "GladeInstaller.exe");
startInfo.Arguments =
string.Empty;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
Process.Start(startInfo);
}
catch (Exception)
{
MessageBox.Show("The installer needs to be ran as administraitor in order for it to work, if you dont have theese priverlages download Glade Skinned", "Glade Installation Error");
Environment.Exit(0);
}
string path = #"c:\Glade";
try
{
// Determine whether the directory exists.
if (Directory.Exists(path))
{
MessageBox.Show("The directory C:\\Glade allready exists! Delecte the folder C:\\Glade and re-run the installer");
Application.Exit();
}
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
}
catch (Exception ec)
{
Console.WriteLine("The process failed: {0}", ec.ToString());
try
{
foreach (Process proc in Process.GetProcessesByName("Glade Installer"))
{
proc.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Environment.Exit(0);
}
try
{
foreach (Process proc in Process.GetProcessesByName("Glade Installer"))
{
proc.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Installation finished!", "Glade Installer");
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
All the code is correct, sorry if it isn't formatted correctly.
Edit:
After Process.Start(startInfo) I realised that I need to put Application.Exit()
Application.Exit() kills Application.
Here i refactored your code to stop doing that please try it.
try
{
ProcessStartInfo startInfo;
startInfo = new ProcessStartInfo();
startInfo.FileName = Path.Combine
(Path.GetDirectoryName(Application.ExecutablePath), "GladeInstaller.exe");
startInfo.Arguments =
string.Empty;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
Process.Start(startInfo);
}
catch (Exception)
{
MessageBox.Show("The installer needs to be ran as administraitor in order for it to work, if you dont have theese priverlages download Glade Skinned", "Glade Installation Error");
Environment.Exit(0);
}
string path = #"c:\Glade";
try
{
// Determine whether the directory exists.
if (Directory.Exists(path))
{
MessageBox.Show("The directory C:\\Glade allready exists! Delecte the folder C:\\Glade and re-run the installer");
// This part was killing you application
// now it just ends click event without killing application.
//Application.Exit();
}
else
{
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
try
{
foreach (Process proc in Process.GetProcessesByName("Glade Installer"))
{
proc.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Installation finished!", "Glade Installer");
Application.Exit();
}
}
catch (Exception ec)
{
Console.WriteLine("The process failed: {0}", ec.ToString());
try
{
foreach (Process proc in Process.GetProcessesByName("Glade Installer"))
{
proc.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Environment.Exit(0);
}
You are starting a new process in Button1_Click. Take that out and it won't loop. If you need to run the installer as administrator, do it by adding the following to the application manifest file:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Your user will be prompted via the UAC to continue.
You should also make sure the user has UAC enabled. If they don't and they are not running as an admin you should provide an error message indicating that.

C# Iperf Server

I'm trying to write a C# wrapper for Iperf server. After Iperf client is done with packet sending, the C# server application should dump the output data to the text file.
The problem is that this process (server) never exits, so it doesn't dump any data to the txt file. However, when I manually close the cmd window that runs iperf server, the text file is written with data (process exits). But this is clearly not the solution I'm looking for.
Any suggestions how can I write the data directly into the file, w/o need of manually closing the iperf server cmd window?
This is my code:
private void button1_Click(object sender, EventArgs e)
{
string commandline_server = " -s -u -i 1";
try
{
process = new Process();
process.StartInfo.FileName = "iperf.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
//process.StartInfo.RedirectStandardError = true;
process.StartInfo.Arguments = commandline_server;
process.StartInfo.CreateNoWindow = false;
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(process_Exited);
process.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
void process_Exited(object sender, EventArgs e)
{
//throw new NotImplementedException();
string outfile = process.StandardOutput.ReadToEnd();
System.IO.File.WriteAllText("test.txt", outfile);
}
Instead of shelling out to a terminal, have you considered using their API? iperf3 introduced "libiperf".
There are example C-programs in their source code tree.

SFX Install Button

I am working on a program that needs to extract a WinRar SFX automatically. Is there anyway I can programmatically click a install button once the exe is started with a Process? Here is the code that I have so far.
public bool Extract()
{
try
{
Process process = new Process();
process.StartInfo.WorkingDirectory = FilePath;
process.StartInfo.FileName = FilePath + fileName;
process.Start();
process.WaitForExit();
}
catch (Exception)
{
return false;
}
return true;
}
You could make the sfx silent with winrar.
Here is a link

Why file access is denied

I have the following code which...
Checks if a folder exists
If it exists, check if a file exists
if file exists, read all the lines from the file
once all the line has been read, show the length of the line in a messagebox
Code:
private void button2_Click(object sender, EventArgs e)
{
strPath = #"C:\QRXS";
string strFile = #"C:\QRXS\download.lst";
if (Directory.Exists(strPath))
{
try
{
if (File.Exists(strFile))
{
try
{
ln = File.ReadAllLines(strPath);
}
catch (Exception ex)
{
// inform user or log depending on your usage scenario
MessageBox.Show(ex.Message, "FILE ACCESS");
}
if (ln != null)
{
MessageBox.Show(ln.Length + "");
// do something with lines
}
}
}
catch (Exception ce)
{
MessageBox.Show(ce.Message, "FOLDER ACCESS");
}
}
}
Everytime I run the application (used Run as Administrator as well), the following line keeps being invoked:
MessageBox.Show(ex.Message, "FILE ACCESS");
How can I fix it?
Replace:
File.ReadAllLines(strPath);
with:
File.ReadAllLines(strFile);
Reason: strPath denotes a directory. You're trying to read its contents as if it were a file, and that obviously won't work.
You need to use :
File.ReadAllLines(strFile);

Categories