I want to get full path from process name WITHOUT running the process.In otherwords- where C# gets absolute path when it is executing following :
Process.Start(startInfo);
startInfo does not contain absolute path.
The full path of the executable is resolved through the %PATH% environment variable. You can replicate the behaviour as follows:
var result = Environment.GetEnvironmentVariable("PATH")
.Split(';')
.Select(path => Path.Combine(path, "notepad.exe"))
.FirstOrDefault(path => File.Exists(path));
// result == "C:\\Windows\\system32\\notepad.exe"
Uses standard windows search policy: current folder and folders in the PATH environment variable.
Perhaps I misunderstood, but what about :
var fInfo = new FileInfo(startInfo.FileName);
var fullPath = fInfo.FullName;
?
Related
It's current path is C:\Users\USERNAME\OneDrive\Desktop\Pro\Professional
How do i set it to C:\Users\USERNAME\OneDrive\Desktop\Pro
But I don't set by writing string to set it back I want the system to understand where it is and go backwards.
Simply put I want it to be like the cd.. command in Windows CMD.
To get the current directory, use
var currentDirectory = Directory.GetCurrentDirectory();
To get the parent directory, use
var parentDirectory = Path.GetDirectoryName(currentDirectory);
And finally to set the current directory:
Directory.SetCurrentDirectory(parentDirectory);
There is a method in System.IO called Path.GetDirectoryName. this would take your path as input and return the parent directory of the path which you have given.
example:
string path = "C:\Users\USERNAME\OneDrive\Desktop\Pro\Professional";
string parentDirectory = Path.GetDirectoryName(path);
here, the parentDirectory now containes "C:\Users\USERNAME\OneDrive\Desktop\Pro"
if you want to go up multiple leveles you can call multiple times,
example:
string path = #"C:\Users\USERNAME\OneDrive\Desktop\Pro\Professional";
string parentDirectory = Path.GetDirectoryName(Path.GetDirectoryName(path));
the parentDirectory now containes "C:\Users\USERNAME\OneDrive\Desktop"
I publish a console project in C# and I put it in the path environment FE c:\some\path\project.exe. the excecutable has the following code.
Console.WriteLine(Environment.GetCommandLineArgs()[0]);
Console.WriteLine(System.AppContext.BaseDirectory);
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
Console.WriteLine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
When I execute it. in the cmd I Get the following:
c:\here\here\here > project.exe
c:\some\path\project.dll
c:\some\path\
c:\some\path\
c:\some\path
c:\some\path
I need to get the path c:\here\here\here
Try this :
Console.WriteLine(Directory.GetCurrentDirectory());
You could do something like this besides what was suggested in Dharati's answer:
string fullPath = System.Reflection.Assembly.GetEntryAssembly().Location;
string noFileNamePath = fullPath.Substring(0, (fullPath.LastIndexOf('\\') + 1));
Is there a way to set a 'root' or a base path to then use relative paths from in C#?
So for example, say I had the path:
C:\Users\Steve\Documents\Document.txt
Could I then use this path, instead of the programs assembly, to be the base path. So this would then allow me to use something like:
..\..\Pictures\Photo.png
Thanks
why not do:
string RootPath = string.empty
#if DEBUG
RootPath = "C:\Users\Steve\Documents\Document.txt"
#else
RootPath = System.Reflection.Assembly.GetAssembly(typeof(MyClass)).Location;
#endif
var newPath = Path.Combine(RootPath, "..\..\Pictures\Photo.png");
How can I check if the directory exists with a dynamic path (~) not a fixed path (C:)?
My code:
Soin_Id = Request.QueryString["SoinId"];
string path = #"~\Ordo\Soin_"+Soin_Id+#"\";
if (Directory.Exists(path))
{
ASPxFileManager_Ordo.Settings.RootFolder = path;
}
else
{
ASPxFileManager_Ordo.Settings.RootFolder = #"~\Ordo\";
}
With this condition, it's never true, even though the directory exists.
You need to use Server.MapPath to resolve dynamic path to physical path on server.
if (Directory.Exists(Server.MapPath(path)))
also consider using Path.Combine for concatenation of path.
Passing in a value with a comma in the UNC path (e.g. "\servername\Smith,John\Documents\") causes the following to start windows explorer but it opens the My Documents instead of the folder path. If I paste in the path into windows explorer's address bar, the folder opens appropriately.
public void OpenWindowsExplorer(string path) {
var runExplorer = new ProcessStartInfo { FileName = "explorer.exe", Arguments = path };
Process.Start(runExplorer);
}
Any idea as to why this is happening/how to resolve the issue is greatly appreciated.
Put quotes around the path:
public void OpenWindowsExplorer(string path) {
path = string.Format("\"{0}\"", path);
var runExplorer = new ProcessStartInfo { FileName = "explorer.exe",
Arguments = path };
Process.Start(runExplorer);
}