I want to determine the absolute path of files used by a known process by reading the command line. Currently, the process is started with relative paths in the command line that point to various files such as config files. The problem is that if the paths are not relative to the folder containing the executable, I have no way of converting the relative paths provided at the command line, well I can't be 100% sure.
For example two batch files:
BATCH 1
CD c:\test\bin
test.exe ..\config\config.ini
BATCH 2
CD c:\test
bin\test.exe config\config.ini
For batch file one, the command line I get is "c:\test\bin\test.exe ..\config\config.ini" and for batch file two I get "c:\test\bin\test.exe config\config.ini". So, see this I can't resolve the paths.
Anyway for starters, I got the command line from a WMI query using ManagementObjectSearcher. Now I need to get the working directory the process was started from to resolve the paths passed at the command line but how?
EDIT: I forgot one key detail. I want to get the working directory of another process. Basically, my main program gathers info from another program. I'm able to determine the process ID because I know the name of the executable. I can also determine the command line. I must now find the working directory or current directory the executable was started in so I can resolve the relative paths of command line. I hope I made the question clearer.
I think Environment.CurrentDirectory should give you the directory the executable was started in. It is only reliable at the start of the process, because it can change later.
Or maybe try Process.GetCurrentProcess().StartInfo.WorkingDirectory. I didn't try it myself, just looked it up on MSDN
To get working directory (current directory) of another process in c# take a look at https://stackoverflow.com/a/23842609/3029359
Have you tried Application.ExecutablePath?
Alternatively, there are numerous Paths that can be retrieve from Application
Related
I do not know if this is even possible without breaking/crashing the process but is there a way to change the working directory of a System.Diagnostics.Process like you would when executing the cd (change directory) command from the cmd.exe command line interface?
You may set the working directory of the process with
myProcess.StartInfo.WorkingDirectory = "dir".
Documentation here.
As per MSDN, there is only one function which can change current folder, SetCurrentDirectory and it has single string parameter, so the change is for current process only.
I have a console application that is in the environment path that can be run from anywhere. If a user types
Program filename
From the directory where the file exits
File.Exists(args[0])
Will tell me the file exists.
But when passing args[0] it is only the file name, so I am going to assume that C# prepends:
Environment.CurrentDirectory
Because the application can be run from anywhere, and the command line can have a great deal of possible input from relative paths to absolute paths I am wondering what is the safest way to get the proper directory where the file exists.
The following seems to work just fine, but am wonderig if I am missing something, is thier a better way.
string dir = Path.GetDirectoryName(args[0]);
if (String.IsNullOrEmpty(dir))
dir = Environment.CurrentDirectory;
Many thanks in advance.
Use Path.GetFullPath(args[0]) - that will resolve the file name into it's full name for you.
I have a c# form application. It opens a text file upon loading. From this text file it reads the default settings. It then fills the numericupdown fields and textbox fields with the default data.
I want to run this every night at a specific time. The windows task seems to be trying to open the program. However, upon loading it gets a FileNotFoundException Could not find file 'C:\Windows\system32\Settings.txt Which is nowhere close to my application run folder. It opens properly when running from command line and using run in MVS2013.
in my program I am searching for this file using relative pathing
could anyone shed some light on this ridiculousness? I would greatly appreciate it.
Your path is relative to the working directory, which seems to be "C:\Windows\system32" if you didn't specify any. So possible solutions are, set working directory to the directory of your executable file (the "Start in (optional)" field) :
.. or modify your program to use absolute path of the executable (you can get the absolute path programmatically).
I need to get the temp file to see what happened because the actual file is never output. However, I can't seem to find where the temp file is created.
I need to find this out without writing code or building the application because there are too many dependencies scattered all over the place. I would not be able to deploy a debug version.
That method returns the path of a temporary file. The path will tell you where its pointing.
For example:
Console.WriteLine(Path.GetTempFileName());
produces:
C:\Users\will\AppData\Local\Temp\tmp9BD5.tmp
for me on this machine, because the TEMP environment variable is pointing to C:\Users\will\AppData\Local\Temp\
But the whole point of a method like GetTempFileName is that you shouldn't have to care where the file ends up. On the off-chance that you do, you can always get there at command prompts or file-open dialogs by using %TEMP%
Imagine I have a picture viewer application made with C# and .NET. I have already set the preferred application to view pictures to use the C# application.
I want to somehow let my program know where it has been invoked. How can I achieve this?
If you're using it to view pictures via shell associations, you can just check the picture filenames passed in on the command line. You can use Environment.GetCommandLineArgs to get the first filename:
// Should check to make sure there is at least one filename passed first...
string imageFilename = Environment.GetCommandLineArgs[1];
string directory = System.IO.Path.GetDirectoryName(imageFilename);
If you want the working directory, just check Environment.CurrentDirectory at startup...
I think you can use Environment.CurrentDirectory
The current directory (Environment.CurrentDirectory) of an application can change during execution. Additionally, the current directory may not be the directory in which the application resides, such as if a user runs it from a command line in an arbitrary directory by specifying an absolute path to the executable.
If you really want the "current directory" of the application, then use Environment.CurrentDirectory, but if you want to know the location of the application, you can use the following approaches:
System.Windows.Forms.Application.ExecutablePath
(if running a WinForm application)
System.Windows.Forms.Application.StartupPath
(if running a WinForm application)
System.Reflection.Assembly.GetEntryAssembly().Location