My FileHelper class

I’ve posted a few code snippets and some people noticed that there are references to unresolved methods.I am using a few helper classes. This post describes my FileHelper class:

System.IO;
     
/// <summary>
      /// Summary description for FileHelper.

using

    /// See also StreamHelper class methods, e.g. SaveStringToFile,SaveToFile

      /// </summary>

      public static class FileHelper

      {

            public static string GetFileExtensionWithoutDot(string fileName)

            {

                  string sExt=Path.GetExtension(fileName);

                  if ((null==sExt) | (sExt==string.Empty ))

                        return string.Empty;

                  return sExt.Remove(0,1);

            }

        //sourced from internal HtmlAgilityPack IOLibrary http://smourier.blogspot.com/2005/05/net-html-agility-pack-how-to-use.html

        public static void MakeWritable(string path)

        {

            if (!File.Exists(path))

                return;

            File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);

        }

        //sourced from internal HtmlAgilityPack IOLibrary http://smourier.blogspot.com/2005/05/net-html-agility-pack-how-to-use.html

        // if target directory doesn’t exist, create it

        // if target file is read-only, overwrite it

        //NOte: if source file is read-only, copied file will be also read-only

        public static void CopyAlways(string source, string target)

        {

            if (!File.Exists(source))

                return;

            Directory.CreateDirectory(Path.GetDirectoryName(target));

            MakeWritable(target);

            File.Copy(source, target, true);

        }

        //from http://www.codeproject.com/cs/files/copydirectoriesrecursive.asp

        // alternative use Win API or (http://www.bubble-media.com/cgi-bin/articles/archives/000026.html)

        // Copy directory structure recursively

        public static void CopyDirectory(string Src,string Dst)

        {

            String[] Files;

 

            if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)

                Dst+=Path.DirectorySeparatorChar;

            if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);

            Files=Directory.GetFileSystemEntries(Src);

            foreach(string Element in Files){

                // Sub directories

                if(Directory.Exists(Element))

                    CopyDirectory(Element,Dst+Path.GetFileName(Element));

                // Files in directory

                else

                    File.Copy(Element,Dst+Path.GetFileName(Element),true);

                }

        }

        //from http://www.pgacon.com/visualbasic.htm#Creating_a_temporary_file_name

        public static string MakeTempFileName(string path, string ext)

        {

            int x;

            string s;

            if (String.IsNullOrEmpty(ext))   ext = “.tmp”;

            //if (path == “”)

            //{

            //    path = App.path;

            //}

            //if ((Right(path, 1) != “\”))

            //{

            //    path = path + “\”;

            //}

            x = 0;

            do

            {

                x = x + 1;

                s = path + x + ext;

            } while (File.Exists(s));

            return s;

        }

        /// <summary>

        /// FileInfo and DirectoryInfo derived from ABSTRUCT class FileSystemInfo,

        /// so MS doesn’t provide standard method to open path without trying one or another class

        /// </summary>

        /// <param name=”fi”></param>

        /// <returns>can return null if path is not found</returns>

        public static FileSystemInfo GetFileOrDirectory(string path)

        {

            //alternatively call File.GetAttributes and the open either FileInfo or DirectoryInfo

            FileInfo fi = new FileInfo(path);

            if(fi.Exists)

            {

                return fi; 

            }

            else

            {

                DirectoryInfo di = new DirectoryInfo(path);

                if (di.Exists)

                {

                    return di;

                }

            }

            return null;

        }

 

        public static bool IsDirectory(FileSystemInfo fi)

        {

            bool bRet = (fi.Attributes & FileAttributes.Directory) == FileAttributes.Directory;

             return bRet;

        }

        public static bool IsValidUNCDirectoryPath(string sPath)

        {//http://channel9.msdn.com/ShowPost.aspx?PostID=135681

            Uri uri;

            bool bRet = Uri.TryCreate(sPath, UriKind.Absolute, out uri) && uri.IsUnc;

            return bRet;

        }

        //from SessionToolsSESSIONSessionManagerStartEnd.vb

        public static string GetPathInTempFolder(string AppName,string InstanceName, string RelativeFileName)

        {

            if(RelativeFileName==null){   throw new ArgumentNullException(“RelativeFileName”);}

            string sTempFolderName = Path.GetTempPath();// ‘        Environment.GetEnvironmentVariable(“TEMP”)

            if (!String.IsNullOrEmpty(AppName))

            {

                sTempFolderName = Path.Combine(sTempFolderName, AppName);

            }

            if (!String.IsNullOrEmpty(InstanceName))

            {

                sTempFolderName = Path.Combine(sTempFolderName, InstanceName);

            }

            string pathTarget = Path.Combine(sTempFolderName, RelativeFileName);

            string sTargetDir = Path.GetDirectoryName(pathTarget);

            if ((!(Directory.Exists(sTargetDir))))

            {

                Directory.CreateDirectory(sTargetDir);

            }

            return pathTarget;

        }

        public static string RemoveInvalidFileNameChars(string FileName)

        {

            char[] chars = Path.GetInvalidFileNameChars();

            string[] parts = FileName.Split(chars);

            return String.Join(“”, parts);

        }

 


Advertisements