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);
Related
This question already has answers here:
Redirecting standard input of console application
(3 answers)
Closed 5 years ago.
I need to enter account and password in command line. How Can I achieve this? I'm using the following code.
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"/k" + Commmand;
process.StartInfo = startInfo;
process.Start();
Below are the expected prompts resulting from the cmd process:
You must have a DB of "users", thant contains usernames and passwords.
In command line ask from a user needed input and check it with you DB of passwords. DB can be anithing that is allowable in your case. For example simple List or smth like that.
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 have a C# program that is running and I want to launch another executable in different directory.
I have this code on event:
string path = "Y:\Program\test.exe";
Process.Start(path);
Problem is that in order for program to work right it need to take information from settings.ini where the exe file is located but it takes settings.ini from program folder with which I am trying to launch second program. test.exe is working fine when I am opening it from its folder by double click. What could be the problem?
You need to tell the process what the working directory is via ProcessStartInfo.WorkingDirectory:
var processStartInfo = new ProcessStartInfo
{
WorkingDirectory = #"Y:\Program",
FileName = #"Y:\Program\test.exe",
};
Process.Start(processStartInfo);
Edit:
In order to get the directory from the user, you can use DirectoryInfo.FullName:
var userFileInfo = new FileInfo(userInsertedVariableHere);
var parentDirectory = userFileInfo.Directory.FullName;
Process myProcess = new Process();
ProcessStartInfo remoteAdmin =
new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + #"\iisreset.exe /restart");
remoteAdmin.UserName = username;
remoteAdmin.Password = pwd;
remoteAdmin.Domain = domain;
myProcess.StartInfo = remoteAdmin;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start(); --- ERROR HERE
Can not find the file specified.
But when I try to run iisreset on the local machine by cmd it's working.
Unless I'm missing something, (Environment.GetFolderPath(Environment.SpecialFolder.System) will get back the local machine (Where the code is running) special folder. So it's expecting the file C:\Windows\System\iisreset.exe to be located on your machine. The only method I could see to get around this, is to drop the C:\ and instead add in the device's name \\DeviceName\C$\ and then the filepath. This is assuming the special folder system is located in the same place on your machine and the remote machine.
The only other method, to get the remote machines system directory is to get it via WMI or via a reg entry reading.
So if using WMI:
"SELECT * FROM Win32_OperatingSystem"
Once done, you would then need to build the folder string yourself from that.
There is no file called C:\Windows\System\iisreset.exe /restart (assuming that Environment.GetFolderPath(Environment.SpecialFolder.System) returns C:\Windows\System\
So you would want
ProcessStartInfo remoteAdmin =
new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + "iisreset.exe");
remoteAdmin.Arguments = "/restart";
But Environment.GetFolderPath(Environment.SpecialFolder.System) probably returns something like C:\Windows\System (note no trailing slash), and there is definitely no file called c:\windows\systemiisreset.exe
So you would actually want
ProcessStartInfo remoteAdmin =
new ProcessStartInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "iisreset.exe"));
remoteAdmin.Arguments = "/restart";
iisreset.exe supports remote calls, so instead of using WMI to get remote directory you can actually just do:
iisreset {servername}
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.