public static string Path(this Environment.SpecialFolder folder)
    {
        var path = Environment.GetFolderPath(folder);

        if (!string.IsNullOrEmpty(path))
        {
            return(path);
        }
        switch (folder)
        {
        case Environment.SpecialFolder.Cookies:
        {
            //detect current OS and give correct folder, here is for example only
            var defaultRoot = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            return(System.IO.Path.Combine(defaultRoot, "Cookies"));
        }

        //case OTHERS:
        //    {
        //        // TO DO
        //    }
        default:
        {
            //do something or throw or return null
            throw new PlatformNotSupportedException(folder.ToString());
            //return null;
        }
        }
    }
        string TestFolder(Environment.SpecialFolder folder, bool supported = true, bool exists = true, bool readOnly = false)
        {
            var path = Environment.GetFolderPath(folder);

            Assert.That(path.Length > 0, Is.EqualTo(supported), folder.ToString());
            if (!supported)
            {
                return(path);
            }

            Assert.That(Directory.Exists(path), Is.EqualTo(exists), path);
            if (!exists)
            {
                return(path);
            }

            string file = Path.Combine(path, "temp.txt");

            try {
                File.WriteAllText(file, "mine");
                Assert.False(readOnly, "!readOnly " + folder);
            }
            catch {
                Assert.True(readOnly, "readOnly " + folder);
            }
            finally {
                File.Delete(file);
            }
            return(path);
        }
        public static Boolean CheckShortcut(Environment.SpecialFolder directory, String subdir = "")
        {
            log.Debug("CheckShortcut: directory=" + directory.ToString() + "; subdir=" + subdir);
            Boolean foundShortcut = false;

            if (subdir != "")
            {
                subdir = "\\" + subdir;
            }
            String shortcutDir = Environment.GetFolderPath(directory) + subdir;

            if (!System.IO.Directory.Exists(shortcutDir))
            {
                return(false);
            }

            foreach (String file in System.IO.Directory.GetFiles(shortcutDir))
            {
                if (file.EndsWith("\\OutlookGoogleCalendarSync.lnk") || //legacy name <=v2.1.0.0
                    file.EndsWith("\\" + Application.ProductName + ".lnk"))
                {
                    foundShortcut = true;
                    break;
                }
            }
            return(foundShortcut);
        }
        public static void RemoveShortcut(Environment.SpecialFolder directory, String subdir = "")
        {
            try {
                log.Debug("RemoveShortcut: directory=" + directory.ToString() + "; subdir=" + subdir);
                if (subdir != "")
                {
                    subdir = "\\" + subdir;
                }
                String shortcutDir = Environment.GetFolderPath(directory) + subdir;

                if (!System.IO.Directory.Exists(shortcutDir))
                {
                    log.Info("Failed to delete shortcut in \"" + shortcutDir + "\" - directory does not exist.");
                    return;
                }
                foreach (String file in System.IO.Directory.GetFiles(shortcutDir))
                {
                    if (file.EndsWith("\\OutlookGoogleCalendarSync.lnk") || //legacy name <=v2.1.0.0
                        file.EndsWith("\\" + Application.ProductName + ".lnk"))
                    {
                        System.IO.File.Delete(file);
                        log.Info("Deleted shortcut in \"" + shortcutDir + "\"");
                        break;
                    }
                }
            } catch (System.Exception ex) {
                log.Warn("Problem trying to remove legacy Start Menu shortcut.");
                log.Error(ex.Message);
            }
        }
示例#5
0
 public override void LoadDirectories()
 {
     try
     {
         List <FolderInfo> list        = new List <FolderInfo>();
         string[]          folderNames = System.Enum.GetNames(typeof(Environment.SpecialFolder));
         foreach (string folderName in folderNames)
         {
             Environment.SpecialFolder member = (Environment.SpecialFolder)System.Enum.Parse(typeof(Environment.SpecialFolder), folderName, true);
             string     folderPath            = Environment.GetFolderPath(member);
             FolderInfo fi = new FolderInfo(Parent, member.ToString(), folderPath);
             list.Add(fi);
         }
         Directories = list.ToArray();
     }
     catch (UnauthorizedAccessException)
     {
         HasErrors = true;
     }
     catch (Exception)
     {
         throw;
     }
     finally
     {
         IsDirectoriesLoaded = true;
     }
 }
示例#6
0
        public static void AddShortcut(Environment.SpecialFolder directory, String subdir = "")
        {
            log.Debug("AddShortcut: directory=" + directory.ToString() + "; subdir=" + subdir);
            String appPath = Application.ExecutablePath;

            if (subdir != "")
            {
                subdir = "\\" + subdir;
            }
            String shortcutDir = Environment.GetFolderPath(directory) + subdir;

            if (!System.IO.Directory.Exists(shortcutDir))
            {
                log.Debug("Creating directory " + shortcutDir);
                System.IO.Directory.CreateDirectory(shortcutDir);
            }

            string shortcutLocation = System.IO.Path.Combine(shortcutDir, "OutlookGoogleCalendarSync.lnk");

            IWshRuntimeLibrary.WshShell     shell    = new IWshRuntimeLibrary.WshShell();
            IWshRuntimeLibrary.IWshShortcut shortcut = shell.CreateShortcut(shortcutLocation) as IWshRuntimeLibrary.WshShortcut;

            shortcut.Description      = "Synchronise Outlook and Google calendars";
            shortcut.IconLocation     = appPath.ToLower().Replace("OutlookGoogleCalendarSync.exe", "icon.ico");
            shortcut.TargetPath       = appPath;
            shortcut.WorkingDirectory = Application.StartupPath;
            shortcut.Save();
            log.Info("Created shortcut in \"" + shortcutDir + "\"");
        }
示例#7
0
        public static string TryGetFolderPath(Environment.SpecialFolder folder)
        {
            // the .NET function fails, if the path is for example set to e: instead of e:\
            string result = string.Empty;

            try
            {
                result = Environment.GetFolderPath(folder);
            }
            catch (ArgumentException)
            {
                // Not so nice. Let's check the registry ourselves.
                string folderName = folder.ToString();
                try
                {
                    RegistryKey key = Registry.CurrentUser;
                    key = key.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders");
                    if ((key != null) & (key.GetValue(folderName) != null))
                    {
                        result = key.GetValue(folderName).ToString().TrimEnd(Path.DirectorySeparatorChar);
                    }
                }
                catch (Exception)
                {
                    // if we can't access registry, we have no choice but to return an empty string
                }
            }

            return(result);
        }
 public IEnumerable <Folder> Get()
 {
     foreach (int folderValue in Enum.GetValues(typeof(Environment.SpecialFolder)))
     {
         Environment.SpecialFolder folder = (Environment.SpecialFolder)folderValue;
         yield return(new Folder(folder.ToString(), Environment.GetFolderPath(folder)));
     }
 }
示例#9
0
 private static void AddSpecialFolder(Environment.SpecialFolder sf)
 {
     try
     {
         AddSpecialFolder(sf.ToString(), Environment.GetFolderPath(sf));
     }
     catch
     {
     }
 }
示例#10
0
        private static string GetDisplayName(Environment.SpecialFolder sf)
        {
            string path = Environment.GetFolderPath(sf, Environment.SpecialFolderOption.Create);

            if (Directory.Exists(path))
            {
                return(System.IO.Path.GetFileName(path));
            }

            return(sf.ToString());
        }
示例#11
0
        public string GetSpecialFolder(Environment.SpecialFolder folder)
        {
            var ensureDirectoryExists = UserCachePath.Parent.EnsureDirectoryExists(folder.ToString());
            var specialFolderPath     = ensureDirectoryExists.ToString();

            if (enableTrace)
            {
                logger.Trace("GetSpecialFolder: {0}", specialFolderPath);
            }

            return(specialFolderPath);
        }
示例#12
0
            public DirectoryComboEntry(string p, Environment.SpecialFolder spDir)
                : this(p)
            {
                switch (spDir)
                {
                case Environment.SpecialFolder.MyDocuments:
                    label = "*My Documents*";
                    break;

                case Environment.SpecialFolder.DesktopDirectory:
                    label = "*Desktop*";
                    break;

                default:
                    label = spDir.ToString();
                    break;
                }
            }
    public static string Path(this Environment.SpecialFolder folder)
    {
        try
        {
            return(Environment.GetFolderPath(folder));
        }
        catch (PlatformNotSupportedException ex)
        {
            switch (folder)
            {
            case Environment.SpecialFolder.Cookies:
            {
                //detect current OS and give correct folder, here is for example only
                var defaultRoot = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                return(System.IO.Path.Combine(defaultRoot, "Cookies"));
            }

            case OTHERS:
            {
                // TO DO
            }

            default:
            {
                //do something or throw or return null
                throw new PlatformNotSupportedException(folder.ToString());
                //return null;
            }
            }
        }
        catch (Exception ex)
        {
            //do something
            return(null);
        }
    }
示例#14
0
        public static void RemoveShortcut(Environment.SpecialFolder directory, String subdir = "")
        {
            log.Debug("RemoveShortcut: directory=" + directory.ToString() + "; subdir=" + subdir);
            if (subdir != "")
            {
                subdir = "\\" + subdir;
            }
            String shortcutDir = Environment.GetFolderPath(directory) + subdir;

            if (!System.IO.Directory.Exists(shortcutDir))
            {
                log.Info("Failed to delete shortcut in \"" + shortcutDir + "\" - directory does not exist.");
                return;
            }
            foreach (String file in System.IO.Directory.GetFiles(shortcutDir))
            {
                if (file.EndsWith("\\OutlookGoogleCalendarSync.lnk"))
                {
                    System.IO.File.Delete(file);
                    log.Info("Deleted shortcut in \"" + shortcutDir + "\"");
                    break;
                }
            }
        }
示例#15
0
 public override string ToString()
 {
     return(_sf.ToString());
 }