Ejemplo n.º 1
0
        /// <summary>
        /// Return the index into the system image list of the image that represents the given file.
        /// </summary>
        /// <param name="path">The full path to the file or directory whose icon is required</param>
        /// <returns>The index of the icon, or -1 if something goes wrong</returns>
        /// <remarks>This is only useful if you are using the system image lists directly. Since there is
        /// no way to do that in .NET, it isn't a very useful.</remarks>
        public static int GetSysImageIndex(string path)
        {
            SHFILEINFO shfi   = new SHFILEINFO();
            int        flags  = SHGFI_ICON | SHGFI_SYSICONINDEX;
            IntPtr     result = ShellUtilities.SHGetFileInfo(path, 0, out shfi, Marshal.SizeOf(shfi), flags);

            if (result.ToInt32() == 0)
            {
                return(-1);
            }
            else
            {
                return(shfi.iIcon);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the string that describes the file's type.
        /// </summary>
        /// <param name="path">The file or directory whose type is to be fetched</param>
        /// <returns>A string describing the type of the file, or an empty string if something goes wrong.</returns>
        public static String GetFileType(string path)
        {
            SHFILEINFO shfi   = new SHFILEINFO();
            int        flags  = SHGFI_TYPENAME;
            IntPtr     result = ShellUtilities.SHGetFileInfo(path, 0, out shfi, Marshal.SizeOf(shfi), flags);

            if (result.ToInt32() == 0)
            {
                return(String.Empty);
            }
            else
            {
                return(shfi.szTypeName);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Return the icon for the given file/directory.
        /// </summary>
        /// <param name="path">The full path to the file whose icon is to be returned</param>
        /// <param name="isSmallImage">True if the small (16x16) icon is required, otherwise the 32x32 icon will be returned</param>
        /// <param name="useFileType">If this is true, only the file extension will be considered</param>
        /// <returns>The icon of the given file, or null if something goes wrong</returns>
        public static Icon GetFileIcon(string path, bool isSmallImage, bool useFileType)
        {
            int flags = SHGFI_ICON;

            if (isSmallImage)
            {
                flags |= SHGFI_SMALLICON;
            }

            int fileAttributes = 0;

            if (useFileType)
            {
                flags |= SHGFI_USEFILEATTRIBUTES;
                if (System.IO.Directory.Exists(path))
                {
                    fileAttributes = FILE_ATTRIBUTE_DIRECTORY;
                }
                else
                {
                    fileAttributes = FILE_ATTRIBUTE_NORMAL;
                }
            }

            SHFILEINFO shfi   = new SHFILEINFO();
            IntPtr     result = IntPtr.Zero;

            try {
                //Updated windows and now get a F***ed up exception "ListView virtualization requires a valid ListViewItem to be provided by the RetrieveVirtualItem event or in the OnRetrieveVirtualItem method."
                result = ShellUtilities.SHGetFileInfo(path, fileAttributes, out shfi, Marshal.SizeOf(shfi), flags);
            }
            catch (Exception) { }

            if (result.ToInt32() == 0)
            {
                return(null);
            }
            else
            {
                return(Icon.FromHandle(shfi.hIcon));
            }
        }
Ejemplo n.º 4
0
        internal static string GetEpisodeName(string name, Series series, Episode[] ep, string format, int filenum, char invalidCharReplacement = '_', string episodedateformat = "YYYY-MM-DD", string seriesdateformat = "YYYY")
        {
            if (format == null || format.Trim().Length == 0 || series == null || ep == null || ep.Length == 0)
            {
                return(name);
            }

            if (name == null)
            {
                name = "";
            }

            format = format.Replace('/', Path.DirectorySeparatorChar);
            format = format.Replace('\\', Path.DirectorySeparatorChar);

            char[] invalidFileChars = Path.GetInvalidFileNameChars();

            if (invalidCharReplacement == '\0' || invalidFileChars.Contains(invalidCharReplacement))
            {
                invalidCharReplacement = '_';
            }

            if (episodedateformat == null || episodedateformat.Trim().Length == 0)
            {
                episodedateformat = "YYYY-MM-DD";
            }
            if (seriesdateformat == null || seriesdateformat.Trim().Length == 0)
            {
                seriesdateformat = "YYYY";
            }


            string newName = format;

            string epName       = ep[0].EpisodeName == null ? "" : ep[0].EpisodeName.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
            string airedseason  = ep[0].AiredSeason.ToString("##00");
            string airedEpisode = ep[0].AiredEpisodeNumber.ToString("##00");
            string directors    = FromArray(ep[0].Directors).Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
            string writers      = FromArray(ep[0].Writers).Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
            string overview     = ep[0].Overview == null ? "" : ep[0].Overview.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
            string absnumber    = "";

            if (ep[0].AbsoluteNumber.HasValue)
            {
                absnumber = ep[0].AbsoluteNumber.Value.ToString("##00");
            }
            string firstAired = ep[0].FirstAired.ToString(episodedateformat).Replace(':', '-');

            if (ep.Length > 1)
            {
                for (int i = 1; i < ep.Length; i++)
                {
                    epName += ep[i].EpisodeName == null ? "" : " & " + ep[i].EpisodeName.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
                    if (ep[0].AiredSeason != ep[i].AiredSeason)
                    {
                        airedseason += "-" + ep[i].AiredSeason.ToString("##00"); //Assume same season?
                    }
                    airedEpisode += "-" + ep[i].AiredEpisodeNumber.ToString("##00");
                    directors    += FromArray(ep[i].Directors).Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
                    writers      += FromArray(ep[i].Writers).Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
                    overview     += ep[i].Overview == null ? "" : ep[i].Overview.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement);
                    if (ep[0].AbsoluteNumber.HasValue)
                    {
                        absnumber += ep[i].AbsoluteNumber.Value.ToString("##00");
                    }
                    firstAired += ep[i].FirstAired.ToString(episodedateformat).Replace(':', '-');
                }
            }

            newName = newName.Replace("%1", series.SeriesName == null ? "" : series.SeriesName.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement));       //Series Name
            newName = newName.Replace("%series%", series.SeriesName == null ? "" : series.SeriesName.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement)); //Series Name
            newName = newName.Replace("%2", epName);                                                                                                                              //episodeName
            newName = newName.Replace("%title%", epName);                                                                                                                         //episodeName
            newName = newName.Replace("%3", airedseason);                                                                                                                         //airedSeason
            newName = newName.Replace("%season%", airedseason);                                                                                                                   //airedSeason
            newName = newName.Replace("%4", series.FirstAired.ToString(seriesdateformat).Replace(':', '-'));                                                                      //Series firstAired
            newName = newName.Replace("%seriesyear%", series.FirstAired.ToString(seriesdateformat).Replace(':', '-'));                                                            //Series firstAired
            newName = newName.Replace("%5", FromArray(series.Genre).Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement));                                  //Genre
            newName = newName.Replace("%genre%", FromArray(series.Genre).Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement));                             //Genre
            newName = newName.Replace("%6", airedEpisode);                                                                                                                        //airedEpisodeNumber
            newName = newName.Replace("%episodenumber%", airedEpisode);                                                                                                           //airedEpisodeNumber
            newName = newName.Replace("%7", name);                                                                                                                                //File name
            newName = newName.Replace("%filename%", name);                                                                                                                        //File name
            newName = newName.Replace("%8", filenum.ToString("##00"));                                                                                                            //File number
            newName = newName.Replace("%filenumber%", filenum.ToString("##00"));                                                                                                  //File number
            newName = newName.Replace("%9", "");                                                                                                                                  //Comment
            newName = newName.Replace("%comment%", "");                                                                                                                           //Comment

            if (newName.Contains("%0") || newName.Contains("%bitrate%") || newName.Contains("%t") || newName.Contains("%playtime%") || newName.Contains("%res%") || newName.Contains("%resolution%"))
            {
                Dictionary <string, List <string> > details = ShellUtilities.GetFileDetails(name);
                if (details.ContainsKey("Bit rate"))
                {
                    newName = newName.Replace("%0", details["Bit rate"][0]);        //Bitrate
                    newName = newName.Replace("%bitrate%", details["Bit rate"][0]); //Bitrate
                }
                else
                {
                    newName = newName.Replace("%0", "");        //Bitrate
                    newName = newName.Replace("%bitrate%", ""); //Bitrate
                }

                if (details.ContainsKey("Length"))
                {
                    TimeSpan tslen;
                    if (TimeSpan.TryParse(details["Length"][0], out tslen))
                    {
                        string length = "";

                        if (tslen.TotalMinutes > 120)
                        {
                            length = tslen.Hours + "hrs " + tslen.Minutes + "min " + tslen.Seconds;
                        }
                        else
                        {
                            length = tslen.TotalMinutes + "min " + tslen.Seconds;
                        }

                        newName = newName.Replace("%t", length);         //Play time
                        newName = newName.Replace("%playtime%", length); //Play time
                    }
                    else
                    {
                        newName = newName.Replace("%t", "");
                        newName = newName.Replace("%playtime", "");
                    }
                }
                else
                {
                    newName = newName.Replace("%t", "");
                    newName = newName.Replace("%playtime", "");
                }

                if (details.ContainsKey("Dimensions"))
                {
                    newName = newName.Replace("%res%", details["Dimensions"][0]);
                    newName = newName.Replace("%resolution%", details["Dimensions"][0]);
                }
                else
                {
                    newName = newName.Replace("%res%", "");
                    newName = newName.Replace("%resolution%", "");
                }
            }

            newName = newName.Replace("%directors%", directors);                                                                                           //directors
            newName = newName.Replace("%writers%", writers);                                                                                               //writers
            newName = newName.Replace("%overview%", overview);                                                                                             //overview
            newName = newName.Replace("%absolutenumber%", absnumber);                                                                                      //absoluteNumber
            newName = newName.Replace("%status%", series.Status);                                                                                          //status
            newName = newName.Replace("%episodeAired%", firstAired);                                                                                       //Episode firstAired
            newName = newName.Replace("%netowrk%", series.Network.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement));             //network
            newName = newName.Replace("%seriesoverview%", series.Overview.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement));     //Series overview
            newName = newName.Replace("%airsdayofweek%", series.AirsDayOfWeek.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement)); //airsDayOfWeek
            newName = newName.Replace("%airstime%", series.AirsTime.ToString("t").Replace(':', '-'));                                                      //airsTime
            if (series.Rating != null)
            {
                newName = newName.Replace("%rating%", series.Rating.Replace('/', invalidCharReplacement).Replace('\\', invalidCharReplacement)); //rating
            }
            else
            {
                newName = newName.Replace("%rating%", "no rating");
            }

            foreach (KeyValuePair <string, string> replace in Configuration.ReplaceChars)
            {
                newName = newName.Replace(replace.Key, replace.Value); //rating
            }


            foreach (char c in invalidFileChars)
            {
                if (c == '\\')
                {
                    continue;
                }
                newName = newName.Replace(c, invalidCharReplacement);
            }

            if (name != null && name.Trim().Length > 0)
            {
                System.IO.FileInfo file = new System.IO.FileInfo(name);
                newName += file.Extension;
            }
            return(newName);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Execute the given operation on the file or directory identified by the given path.
        /// Example operations are "edit", "print", "explore".
        /// </summary>
        /// <param name="path">The file or directory to be operated on</param>
        /// <param name="operation">What operation should be performed</param>
        /// <returns>Values &lt; 31 indicate some sort of error. See ShellExecute() documentation for specifics.</returns>
        public static int Execute(string path, string operation)
        {
            IntPtr result = ShellUtilities.ShellExecute(0, operation, path, "", "", SW_SHOWNORMAL);

            return(result.ToInt32());
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Execute the default verb on the file or directory identified by the given path.
 /// For documents, this will open them with their normal application. For executables,
 /// this will cause them to run.
 /// </summary>
 /// <param name="path">The file or directory to be executed</param>
 /// <returns>Values &lt; 31 indicate some sort of error. See ShellExecute() documentation for specifics.</returns>
 /// <remarks>The same effect can be achieved by <code>System.Diagnostics.Process.Start(path)</code>.</remarks>
 public static int Execute(string path)
 {
     return(ShellUtilities.Execute(path, ""));
 }
Ejemplo n.º 7
0
        private void SetupColumns()
        {
            // The column setup here is identical to the File Explorer example tab --
            // nothing specific to the TreeListView.

            // The only difference is that we don't setup anything to do with grouping,
            // since TreeListViews can't show groups.

            SysImageListHelper helper = new SysImageListHelper(this.treeListViewFilesAndTags);

            this.colFilename.ImageGetter = delegate(object x) {
                if (x == null)
                {
                    return(null);
                }
                return(helper.GetImageIndex(((MyFileSystemInfo)x).FullName));
            };
            this.colFilename.AspectGetter = delegate(object x) {
                if (((MyFileSystemInfo)x) != null)
                {
                    return(((MyFileSystemInfo)x).Name);
                }
                return("");
            };

            // Get the size of the file system entity.
            // Folders and errors are represented as negative numbers
            this.colSize.AspectGetter = delegate(object x) {
                if (x == null)
                {
                    return("");
                }

                MyFileSystemInfo myFileSystemInfo = (MyFileSystemInfo)x;

                if (myFileSystemInfo.IsDirectory)
                {
                    return((long)-1);
                }

                try
                {
                    return(myFileSystemInfo.Length);
                }
                catch (System.IO.FileNotFoundException)
                {
                    // Mono 1.2.6 throws this for hidden files
                    return((long)-2);
                }
            };

            // Show the size of files as GB, MB and KBs. By returning the actual
            // size in the AspectGetter, and doing the conversion in the
            // AspectToStringConverter, sorting on this column will work off the
            // actual sizes, rather than the formatted string.
            this.colSize.AspectToStringConverter = delegate(object x) {
                if (x == null)
                {
                    return("");
                }

                long sizeInBytes = (long)x;
                if (sizeInBytes < 0) // folder or error
                {
                    return("");
                }
                return(HelperFunctions.FormatFileSize(sizeInBytes));
            };

            // Show the system description for this object
            this.colFormat.AspectGetter = delegate(object x) {
                if (x == null)
                {
                    return("");
                }

                return(ShellUtilities.GetFileType(((MyFileSystemInfo)x).FullName));
            };

            //this.colSeries.AspectGetter = delegate (object x) {
            //    return VideoInformation.Series((MyFileSystemInfo)x);
            //};
            //this.colSeason.AspectGetter = delegate (object x) {
            //    return VideoInformation.Season((MyFileSystemInfo)x);
            //};
            //this.colEpisodeNumber.AspectGetter = delegate (object x) {
            //    return VideoInformation.EpisodeNumber((MyFileSystemInfo)x);
            //};
            //this.colEpisodeTitle.AspectGetter = delegate (object x) {
            //    return VideoInformation.EpisodeTitle((MyFileSystemInfo)x);
            //};
            //this.colResolution.AspectGetter = delegate (object x) {
            //    return VideoInformation.Resolution((MyFileSystemInfo)x);
            //};
            //this.colYear.AspectGetter = delegate (object x) {
            //    return VideoInformation.Year((MyFileSystemInfo)x);
            //};
            //this.colThumb.AspectGetter = delegate (object x) {
            //    return VideoInformation.Thumb((MyFileSystemInfo)x);
            //};


            //// Show the file attributes for this object
            //// A FlagRenderer masks off various values and draws zero or images based
            //// on the presence of individual bits.
            //this.olvColumnAttributes.AspectGetter = delegate (object x) {
            //    return ((MyFileSystemInfo)x).Attributes;
            //};
            //FlagRenderer attributesRenderer = new FlagRenderer();
            //attributesRenderer.ImageList = imageListSmall;
            //attributesRenderer.Add(FileAttributes.Archive, "archive");
            //attributesRenderer.Add(FileAttributes.ReadOnly, "readonly");
            //attributesRenderer.Add(FileAttributes.System, "system");
            //attributesRenderer.Add(FileAttributes.Hidden, "hidden");
            //attributesRenderer.Add(FileAttributes.Temporary, "temporary");
            //this.olvColumnAttributes.Renderer = attributesRenderer;

            //// Tell the filtering subsystem that the attributes column is a collection of flags
            //this.olvColumnAttributes.ClusteringStrategy = new FlagClusteringStrategy(typeof(FileAttributes));

            this.colPath.AspectGetter = delegate(object x) {
                if (x != null)
                {
                    return(((MyFileSystemInfo)x).FullName);
                }
                else
                {
                    return("");
                }
            };
        }