Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

Sometimes you have a file relative to the root of application, that you want to read from your code. However when your code is build on development machine, the binary files are located in "\bin\debug","\bin\release" or just "\bin"(for VB or Web projects). And you need to make copy of files relative to bin/debug folder.

The function below helps to find the file, even if it was not copied to bin subfolder. (Should I name it ResolvePath smilar to Page.ResolveUrl?)

 

Note, that in most cases Visual Studio file "Copy to Output Directory" Property will be sufficient to achieve the same result.

        /// <summary>

        /// Function to find file in a root, even if bin directory is passed, which actually can be @"\bin\debug",@"\bin\release" or just @"\bin"

        /// </summary>

        /// <param name="sBinDirectory"></param>

        /// <param name="sRelativeFilePath"></param>

        /// <param name="bThrowExceptionIfNotExist"></param>

        /// <returns></returns>

        public static string GetPathForRelativeToRootFile(string sBinDirectory, string sRelativeFilePath, bool bThrowExceptionIfNotExist)

        {

            string sOriginalFullName = Path.Combine(sBinDirectory, sRelativeFilePath);

            string sFullName = sOriginalFullName;

            bool bExists = false;

            //Check if templateFileDirectory exists, if not and is not debug version, going up two levels to find template folder

            if (!File.Exists(sOriginalFullName))

            {

                sBinDirectory = StringHelper.TrimEnd(sBinDirectory, @"\").ToLower();

                //string sNewDir = "";

                string sEndBin = @"\bin\debug";

                bExists = IsFileInParentDirExist(sBinDirectory, sEndBin, sRelativeFilePath, ref sFullName);

                if (bExists == false)

                {

                    sEndBin = @"\bin\release";

                    bExists = IsFileInParentDirExist(sBinDirectory, sEndBin, sRelativeFilePath, ref sFullName);

                }

                if (bExists == false)

                {

                    sEndBin = @"\bin";

                    bExists = IsFileInParentDirExist(sBinDirectory, sEndBin, sRelativeFilePath, ref sFullName);

                }

            }

            else

            {

                bExists = true;

            }

            if (bExists == false)

            {

                if (bThrowExceptionIfNotExist == true)

                {

                    throw new FileNotFoundException("File not found in directory " + sBinDirectory, sRelativeFilePath);

                }

                else

                {

                    return sOriginalFullName;

 

                }

            }

            return sFullName;

        }

        //        ///

        /// <summary>

        /// helper method to be called from GetPathForRelativeToRootFile with different sEndBin parameters

        /// </summary>

        /// <param name="sBinDirectory"></param>

        /// <param name="sEndBin"></param>

        /// <param name="sRelativeFilePath"></param>

        /// <param name="sFullName"></param>

        /// <returns></returns>

        private static bool IsFileInParentDirExist(string sBinDirectory, string sEndBin, string sRelativeFilePath, ref string sFullName)

        {

            bool bExists = false;

            if (sBinDirectory.EndsWith(sEndBin))

            {

                string sNewDir = StringHelper.TrimEnd(sBinDirectory, sEndBin);

                sFullName = Path.Combine(sNewDir, sRelativeFilePath);

                if (File.Exists(sFullName))

                {

                    bExists = true;

                }

            }

            return bExists;

        }

Slightly related functions for ASP.NET discussed in ResolveUrl() without Page Rick Strahl's post.

Posted on Friday, December 12, 2008 10:22 PM .Net Framework | Back to top


Comments on this post: Access to Relative To Application Root File

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net