File Path
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: File Path

  1. #1
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    File Path

    Hi All,

    In my test situation I use absolute paths like
    C:\TestDir\MyProgram.exe
    and
    C:\TestDir\SubDir\Install.exe

    In the final version the exe files will be on a DVD.
    So the statement(s) above gives an error, logically ....

    Question; how should I define this, as the DVD can be inserted in any drive!!
    the dvd layout will be;
    in the root;
    autorun
    myprogram.exe (started from autorun)
    Subdir\install.exe

    the install.exe will be started by myprogram.exe

    if (System.IO.File.Exists(@"C:\TestDir\MyProgram.exe"))
    and
    TargetProcess.StartInfo.FileName = @"C:\TestDir\SubDir\Install.exe";

    how should I define this in relative terms?

    regards,

    Ger

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,365

    Re: File Path

    Pretty much you want to avoid hardcoded paths (even for testing) because they generally lead to problems.
    A good practice in this case is to retrieve the app folder path and save it off. When you need to use a relative
    path (such as the path to a file located in a sub folder of your app), retrieve the app folder path, and add the
    relative path to the file to form the full path. In short, always pass the full path to a file operation. If you don't,
    .Net will try to deduce it for you. Most times it will get it right, but it won't if you (or a 3rd party assembly) has
    changed the working directory. This type of error is hard to track down, so it's best to always pass full paths.

    That being said, there are at least two options for retrieving the location of the main assembly:

    1) Using reflection and the startup assembly
    Code:
    public static string GetAppFolder()
    {
    	string appFolder = Assembly.GetEntryAssembly( ).Location;  
    	return appFolder.Substring( 0, appFolder.LastIndexOf( '\\' ) + 1 ); // strip off the xxx.exe part
    }
    2) Using the configuration file info
    Code:
    public static string GetAppFolder()
    {
        string appFolder = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
    
        return appFolder.Substring(0, appFolder.LastIndexOf('\\'));  // strip off the xxx.config.exe part
    }
    The first approach gets the full path of the startup assembly and strips off the executable name.

    The second approach uses the configuration file name and strips it off to retrieve the app folder.

    I initially used to use the first approach, but have switched to the second approach because:
    1) It doesn't require a System.Diagnostics reference.
    2) Even though it retrieves the full path of the config file, you don't need to have an app.config or web.config file in your project.
    3) It works on all types of C# projects including web projects.
    4) It works in test projects (the 1st approach fails here).
    Last edited by Arjay; November 30th, 2013 at 12:55 PM.

  3. #3
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: File Path

    Thanks Arjay, will have a go !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center