This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
batch file execution in c#
I am using c# to run Java batch file..
But the problem is, that it's not taking the path
I am using in the code as:
var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.WorkingDirectory = batch_process_path;
si.FileName = batch_process_path + "\\" + "run.bat";
si.UseShellExecute = true;
Process.Start(si.FileName);
According to my logic the process should start from the si.working directory. But it is starting from "C:". But if I give the static path it will execute successfully..
I can't understand what the problem is.
Please help me out.
Do not use batch_process_path + "\\" + instead use Path.Combine() to make sure the path is correctly fitted with slashes.
Also read this "When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable"
So set it to false.
Related
This question already has answers here:
C# getting the path of %AppData%
(11 answers)
Closed 3 years ago.
Essentially I am trying to create a C# program that goes into a local directory and performs some tasks by executing a batch file. The batch file itself is located in the AppData Roaming table and requires the C# program to know the username of the computer, whatever it may be.
This is what I currently have:
static void Main()
{
ProcessStartInfo processInfo = new ProcessStartInfo("C:\\Users\\%username%\\AppData\\Roaming");
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = true;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.Start();
}
Notice how I've added %username% but it doesn't seem to understand environmental variables.
What can I add to my code?
My code is not exactly a duplicate as the use case is entirely different, I am referencing the app data roaming directory and opening a batch located in it.
You can get the user folder for the current user like this:
string userfolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
And so the path you want would be like:
string path = Path.Combine(userfolder, "AppData\\Roaming");
ProcessStartInfo processInfo = new ProcessStartInfo(path);
This question already has an answer here:
How do I delete a folder and it's contents, without putting them in the recycle bin?
(1 answer)
Closed 4 years ago.
So I'm writing code to download some files from a sharepoint, store them locally in a tempFolder, then create a .zip file with these files and then I want to remove the tempFolder & it's files under it. Right now "File.Delete" does that, but all the files are put in my recyclingBin. If this code runs constantly on a server & instead of my now 5 test files, uses hundreds or thousands of files to zip every half hour maybe, it's going to go badly...
So is there a way in C# to hard delete (or shift delete) a folder & it's subfiles?
You use a CMD workaround for this:
Process p = new Process();
p.StartInfo = new ProcessStartInfo( "cmd", "/c sdelete -p 1 -s -z -q -a 'path/to/director' )
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
This question already has answers here:
When running a program using Process.Start, it can't find it's resource files
(2 answers)
Closed 6 years ago.
I want my program to search a users computer for a file called "xonotic.exe" and open it. Xonotic is a video game if that helps. Inside it's parent folder it contains many other contents that .exe uses on launch.
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter the executable to run, including the complete path
start.FileName = #"C:\Users\Landon\Desktop\Xonotic\xonotic.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = false;
Process.Start(start);
When I run my program in vs with this code it launches "xonotic.exe" but (here's what it looks like). That is a print of the screen on launch and if it looks like the text is going off the edge of my screen its because it is; what you see is what I see.
When I exit vs and just open "xonotic.exe" as I would normally it launches perfectly. My question is why would the programmatic way and the manual way open the same .exe in two different ways? Also when I get out of the buggy xonotic and hover over the icon to see the full view it says that you have reached this menu due to missing or unlocatable content/data. (You can see what I'm talking about here). Also if this part can be resolved easily is there a way to search for the .exe without having to know the filepath? I figured something like this would be super easy, all I want to do is launch program but it has been giving me a lot of grief.
Set the WorkingDirectory property first, then you may also try to set UseShellExecute property as well if needed:
string pathToExecutable = #"C:\Users\Landon\Desktop\Xonotic\xonotic.exe";
var startInfo = new ProcessStartInfo()
{
FileName = pathToExecutable,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = false,
WorkingDirectory = System.IO.Path.GetDirectoryName(pathToExecutable),
UseShellExecute = true
};
UPDATE:
You can search on all drive in all folders, but this would be very time consuming. Here is the code for that:
string[] drives = Directory.GetLogicalDrives();
string pathToExecutable = String.Empty;
foreach (string drive in drives)
{
pathToExecutable =
Directory
.EnumerateFiles(drive, "xonotic.exe", SearchOption.AllDirectories)
.FirstOrDefault();
if (!String.IsNullOrEmpty(pathToExecutable))
{
break;
}
}
if (String.IsNullOrEmpty(pathToExecutable))
{
// We did not find the executable.
}
else
{
// We found the executable. Now we can start it.
}
You have here 3 different question, maybe give them numbers for each question.
And about your third question-
you must have a path of the root and then you can search for all the exe files in the subfolders
string PathOfEXE = #"C:\"; //or d:\ or whatever you want to be the root
List<string> fileEntries = Directory.GetFiles(PathOfEXE , "*.exe",
System.IO.SearchOption.AllDirectories).ToList();
Directory.GetFiles(PathPractice, "*.exe", System.IO.SearchOption.AllDirectories).ToList();
System.IO.SearchOption.AllDirectories).ToList();
foreach (string fullPathfileName in fileEntries)
{
string fileName =System.IO.Path.GetFileName(fullPathfileName )
if (fileName=="xonotic.exe"){ //your code goes here
string pathToExecutable = fullPathfileName ;
}
}
I've been looking into a problem for some time without finding a solution to it. Tried google as well, without getting on track...
Any help would be appreciated!
Problem is:
I'm sending a DDE-command, to open a PDF document to print it silently. Problem is with the path, if it has non-ASCII chars. It would not get recognized and it throws an error. What should I do with the path string to avoid this and to get Acrobat reader to understand the path? I've tried with an "o" instead of "ø", and that works flawlessly... Thanks in advance!
string file = #"C:\Users\Bø\1_tmp_printpages.pdf";
client.Execute("[DocOpen(\"" + file + "\")]", 60000);
client.Execute("[FilePrintSilent(\"" + file + "\")]", 60000);
client.Execute("[DocClose(\"" + file + "\")]", 60000);
client.Execute("[AppExit]", 60000);
I'm using NDde to pass DDE messages.
Since I couldnt find a DDE library other than NDde for C#, I was not able to process non US-ASCII symbols in my path. Therefore I decided to use a different approach, verbs.
Here is my new code that accepts non US-ASCII symbols. It's inside a try-block, and I'm doing some logic to kill AcroRd32 afterwards. But the code itself to print PDF silently is underneath... :)
System.Diagnostics.Process P = new Process();
P.StartInfo.FileName = mc.PrintPages;
P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
P.StartInfo.Verb = "print";
P.StartInfo.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
P.StartInfo.CreateNoWindow = true;
P.Start();
I am running a Java batch file from C#. If I run it by double clicking it executes successfully, but if I run it from C# code it gives exception in thread
"exception in "main" thread
java.lang.noclassdeffoundError"..
what can be the reason and how can it be solved? I am using the code:
var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = "batch-file path";
si.UseShellExecute = true;
Process.Start(si);
You are most likely missing some of the parameters that would be included in your systems environment variables.
Try setting working directory like this
process.StartInfo.WorkingDirectory = "C:\";
Also, try few other options as mentioned here,
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/20992653-dabf-4b31-85f7-e7bfbfb4557c
Try adding the following code as the first line to your batch file.
#cd /d %~dp0
Do not use batch_process_path + "\" + instead use Path.Combine() to make sure the path is correctly fitted with slashes.
Also read this "When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable"
So set it to false.