C# code not starting .exe getting error cannot find file name - c#

I really have no idea why this code isnt working. Everytime i get the error Cannot start process because a file name has not been provided. Even though i provided the path in which the EXE is located and verified it.
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
namespace ProcessExitSample
{
class testsandboxprogram
{
static void Main(string[] args)
{
Contract.Requires(args != null);
try
{
var firstProc = new Process();
Process.Start(#"PATH TO EXE I WANT TO LAUNCH");
firstProc.EnableRaisingEvents = true;
firstProc.Start();
firstProc.WaitForExit();
//so upon exit should run the second program here
Console.WriteLine("First process exited: " + firstProc.ExitCode);
var secondProc = new Process();
Process.Start(#"PATH TO PROGRAM I WANT TO LAUNCH");
secondProc.Start();
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong sorry :(: " + ex.Message);
return;
}
}
}
}

String myexepath = #"C:\Program Files (x86)\Steam\steamapps\common\BattleBlock Theater\BattleBlockTheater.exe"
As this path contains, a space enclose it between double quotes:
Process.Start("\""+myexepath+"\"");

var firstProc = new Process();
// Process.Start(#"PATH TO EXE I WANT TO LAUNCH");
firstProc.EnableRaisingEvents = true;
firstProc.Start();
You do not provide a path for your process to start. I commented out the irrelevant code, since is not related to the firstProc variable.
You probably want:
firstProc.StartInfo.FileName = #"\Path\To\Exe";

The most obvious is try to run your PATH TO EXE I WANT TO LAUNCH in a command line environment and see if you get a self explanatory error.
If your path contains spaces you will see that your are trying to execute something problematic with spaces and you can then use the answer Graffito gave.

Related

C# Console app need assistance with main args error

I have the following c# Console app I would run this in ssis but i am using a couple of PDF manipulating librarys. so i am going to call an exe from my ssis package while passing in a file path.
But i am getting the following error when trying to run via the exe.
Unhandled Exception: System.IndexOutOfRangeException: Index was
outside the bounds of the array. at ConsoleApp.program.Main(String[]
args) line 87
BUT if i run in debug it works fine. Once i get it working on its own via the exe, i want to pass the filepath as a parameter in ssis.
see c# below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;
using System.IO;
namespace PDF_Read_ConsoleApp
{
class Program
{
public static void FilePath(string path)
{
//Console.WriteLine("Please enter full pdf path \n\n ");
//path = Console.ReadLine();
string fp;
fp = #path;
string[] files = Directory.GetFiles(path, "*.pdf");
foreach (string s in files)
{
string txtOutput = s.Replace(".pdf", ".txt");
if (File.Exists(txtOutput))
{
File.Delete(txtOutput);
}
string output;
PDDocument doc = null;
try
{
doc = PDDocument.load(s);
PDFTextStripper stripper = new PDFTextStripper();
stripper.getText(doc);
output = stripper.getText(doc);
StreamWriter NewFile;
NewFile = new StreamWriter(txtOutput);
//NewFile.Write(output.ToString());
NewFile.Write(output.ToString());
NewFile.Close();
}
finally
{
//if (doc != null)
//{
doc.close();
// Console.WriteLine("\n\n File saveed - ({0} ", txtOutput);
//}
}
}
}
static void Main(string[] args)
{
args[0] = #"C:\SSIS_Packages\PDF_Import\PDF_Import\PO_pdfs"; //// TESTING FILE PATH1
FilePath(args[0]);
}
}
}
Kind Regards
Rob
I have managed to get it working, I need to enter an argument within the debug screen, see information in URL below
Console app arguments, how arguments are passed to Main method
THank you for everyone's comments

Function is not running and creating the text file

I created the function pro:
it contains the process array
it calls another write function to make the file and write into it.
the write function writeproc:
it checks if the file at specified path is present or not.
if not it generates the file else it appends the text into the file.
when i run the code it is not doing anything.... :(
This is the main method for the console app that i have made in c#.
[STAThread]
static void Main(String[] args)
{
pro();
}
pro function:
static void pro()
{
Process[] localAll = Process.GetProcesses();
String path_pro = "C://KEYLOGS//processes.txt";
foreach(Process proc in localAll)
{
writeproc(path_pro, proc);
}
}
writeproc function:
static void writeproc(String p, Process the_process)
{
if (!File.Exists(p))
{
using (StreamWriter sw = File.CreateText(p))
{
//empty file generated.
}
}
else
{
using (StreamWriter sw = File.AppendText(p))
{
sw.WriteLine("Process: "+the_process);
}
}
}
This may be the cause of two different things.
1: The folder does not exist on your C drive so the file can't be created. (It will throw a System.IO.DirectoryNotFoundException)
Add Directory.CreateDirectory(p); to the start of your writeproc method.
2: You don't have enough rights to write to your C drive. (It will throw a System.UnauthorizedAccessException)
I suggest adding a breakpoint in your writeproc method to see what exception is being thrown.

Script Error when open .chm disappear when opening through the debugger

I get a number of script errors and none of the images will be shown when I open a .chm file on my computer. If I esc all error messages and refresh (twice) then the .chm is shown correctly. Although I need to do this for each new page.
I have made all recommended fixes for .chm files! Reregistrered, unblocked, fixed paths,... The errors is for all .chm on the machine
But, here is my real question, if I run this program, with a .chm file as argument, through the Visual Studio 2013 debugger then the .chm is shown correctly!
The problem is probably in my Windows configuration, but somehow the debugger "fixes" this error and get it to work. Does the debugger have it's own configuration that isn't dependent on the actual Windows configuration?
using System.Diagnostics;
namespace xcute
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
string f = args[0];
Process.Start(f);
}
}
}
}
EDIT: Here are the error dialogs
I have found the problem (well sort of)!
If I open the .chm as administrator then everything works! So obviously I have some permission error on my computer. The reason it worked when I ran my program in the debugger is that Visual Studio is started as Administrator...
But since I'm a programmer I solved the issue by creating a small program that start hh.exe as admin. I get the UAC consent form but I can live with that.
// Anders
The program:
internal class Program
{
private static void Main(string[] args)
{
if (args.Length > 0)
{
Execute(args[0]);
}
}
private static void Execute(string chmFile)
{
const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\hh.exe");
info.Arguments = chmFile;
info.UseShellExecute = true;
info.Verb = "runas";
try
{
Process.Start(info);
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode == ERROR_CANCELLED)
Console.WriteLine("Why you no select Yes?");
else
throw;
}
}
}

After updating EnvironmentVariable - PATH - I still get a FileNotFoundException on Windows 2003 Server

I'm just starting with a new product and I guess I don't understand the PATH variable. My documentation says to update the PATH like this which I do successfully in a little console application:
using HP.HPTRIM.SDK;
namespace TestSDKforTRIM71
{
class Program
{
static void Main(string[] args)
{
string trimInstallDir = #"C:\Program Files\Hewlett-Packard\HP TRIM";
string temp = Environment.GetEnvironmentVariable("PATH") + ";" + trimInstallDir;
Environment.SetEnvironmentVariable("PATH", temp);
DoTrimStuff();
}
public static void DoTrimStuff()
{
using (Database db = new Database())
{
db.Connect();
Console.WriteLine(db.Id);
}
Console.ReadKey();
}
}
}
In the above project, I have a reference to HP.HPTRIM.SDK which exists at:
C:\Program Files\Hewlett-Packard\HP TRIM\HP.HPTRIM.SDK.dll
After the above ran successfully, I tried to permanently change the PATH by using Control Panel:System:Advanced:Environment Variables. I verified the above PATH by examining the registry at HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. I see the following as the last entry in the PATH value:
;C:\Program Files\Hewlett-Packard\HP TRIM\
I thought this would permanently SET this at the end of the PATH but when I run the above console program with a few lines commented out I get the FileNotFoundException (see below). I am confused about how to get this in the PATH and not have to worry about it anymore.
using HP.HPTRIM.SDK;
namespace TestSDKforTRIM71
{
class Program
{
static void Main(string[] args)
{
//string trimInstallDir = #"C:\Program Files\Hewlett-Packard\HP TRIM";
//string temp = Environment.GetEnvironmentVariable("PATH") + ";" + trimInstallDir;
//Environment.SetEnvironmentVariable("PATH", temp);
DoTrimStuff(); // without setting the PATH this fails despite being in REGISTRY...
}
public static void DoTrimStuff()
{
using (Database db = new Database())
{
db.Connect();
Console.WriteLine(db.Id);
}
Console.ReadKey();
}
}
}
Only newly started processes that don't inherit their environment from their parent will have the updated PATH. You'll have to at least restart the Visual Studio hosting process, close and re-open your solution. To cover all possible corners, log out and log back in so that Windows Explorer (and thus Visual Studio) also start using the updated environment.

System.Diagnostics.Process.Start() opens/runs wrong directory?

I think I found a bug. In my opinion Process.Start runs wrong directory.
To test, create default console application template and paste following:
using System;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool test = false;
DirectoryInfo root = Directory.CreateDirectory(
System.IO.Path.Combine(Directory.GetCurrentDirectory(), "folder"));
DirectoryInfo bug = Directory.CreateDirectory(
System.IO.Path.Combine(root.FullName, "bug"));
DirectoryInfo bugDotCom = Directory.CreateDirectory(
System.IO.Path.Combine(root.FullName, "bug.com"));
ProcessStartInfo bugPSI = new ProcessStartInfo(bug.FullName);
ProcessStartInfo bugDotComPSI = new ProcessStartInfo(bugDotCom.FullName);
if (test)
{
Console.WriteLine(bug.FullName);
Process.Start(bugPSI);
}
else
{
Console.WriteLine(bugDotCom.FullName);
Process.Start(bugDotComPSI);
}
Console.ReadKey();
}
}
}
when variable test is set to false, bug.com directory should be opened, otherwise bug directory. However, this example shows that always bug.com is opened (no matter to test variable) - at least for me.
What's wrong? I'm missing something or that's just a bug?
.com is part of %PATHEXT%, so Windows will use it if it exists.
Changing the extension so that there is no bug.com folder avoids the problem.
To fix the problem, add a \ to the end of the path.

Categories

Resources