How to get the current path to the application directory for the application

I want to get the current directory path, but not the location of the application, but its location in the shortcut.

I tried this, but they returned the location of the application.

Directory.GetCurrentDirectory(); Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase); Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); 
+6
source share
7 answers

If adding a COM object link is not a problem, add a COM object link - Windows Script Host object model

I ran this code in my desktop folder and it really worked. to use the current folder - Environment.CurrentDirectory

 using System; using System.IO; using IWshRuntimeLibrary; //COM object -Windows Script Host Object Model namespace csCon { class Program { static void Main(string[] args) { // Folder is set to Desktop string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var di = new DirectoryInfo(dir); FileInfo[] fis = di.GetFiles(); if (fis.Length > 0) { foreach (FileInfo fi in fis) { if (fi.FullName.EndsWith("lnk")) { IWshShell shell = new WshShell(); var lnk = shell.CreateShortcut(fi.FullName) as IWshShortcut; if (lnk != null) { Console.WriteLine("Link name: {0}", lnk.FullName); Console.WriteLine("link target: {0}", lnk.TargetPath); Console.WriteLine("link working: {0}", lnk.WorkingDirectory); Console.WriteLine("description: {0}", lnk.Description); } } } } } } } 

enter image description here

Link to the code from the forum: http://www.neowin.net/forum/topic/658928-c%23-resolve-lnk-files/

+5
source

According to the process API link in MSDN, the STARTUPINFO struct process for this process contains information about the label of the .lnk file in the title element. There is a flag present in the dwFlags struct element, which is set when this is the case - therefore it seems that this is not always set (im guessing if you run exe directly)

From MSDN:

STARTF_TITLEISLINKNAME: 0x00000800

The lpTitle element contains the path to the shortcut file (.lnk) that the user called to begin this process. This is usually set by the shell when the .lnk file indicates the application is starting. Most applications do not need to set this value. This flag cannot be used with STARTF_TITLEISAPPID.

Link here .

+4
source

Try the following:

 Environment.CurrentDirectory 

From MSDN :

Gets or sets the full path to the current working directory.

+1
source

I think you will need to use COM and add a link to "Microsoft Shell Control And Automation" as described in this blog post:

Here is a code usage example:

 namespace Shortcut { using System; using System.Diagnostics; using System.IO; using Shell32; class Program { public static string GetShortcutTargetFile(string shortcutFilename) { string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); Shell shell = new Shell(); Folder folder = shell.NameSpace(pathOnly); FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; return link.Path; } return string.Empty; } static void Main(string[] args) { const string path = @"C:\link to foobar.lnk"; Console.WriteLine(GetShortcutTargetFile(path)); } } } 
+1
source

using System.Reflection;

  string currentAssemblyDirectoryName = Path.GetDirectoryName( Assembly.GetExecutingAssembly() .Location ); 

Also for web applications you can use:

Web Applications:

 Request.PhysicalApplicationPath 

http://msdn.microsoft.com/en-us/library/system.web.httprequest.physicalapplicationpath.aspx

to capture the application path :)

0
source

Since creating a shortcut is part of the workflow, just set the working directory to "% cd%" for the shortcut, then in the application use:

 Environment.CurrentDirectory 

Obviously, you will want to commit this value before any code that your application calls may change.

When creating a shortcut using Windows Explorer, you cannot set the working directory. So, having created it, open its properties page by right-clicking on it and selecting "Properties", then set the Start field to %cd% . After creating such a shortcut, you can move or copy it to the folders in which you want the application to start.

0
source

If you do not want to use a COM object as suggested in other answers

You can open the file as a regular text file, split it into the \x00 0 character and check the resulting array of strings. One of them, obviously, will be the purpose of the link: something like "C:\path\to\file" or in the case of UNC "\\computers\path\to\file" .

 string lnkFilePath "..."; var file = System.IO.File.ReadAllText(lnkFilePath); var contents = Regex.Split(file, "[\x00]+"); var paths = contents.Where(line => Regex.IsMatch(line, @"^([AZ]:\\|^\\\\)\S+.*?")); 
0
source

All Articles