本文整理汇总了C#中SpecialFolderOption类的典型用法代码示例。如果您正苦于以下问题:C# SpecialFolderOption类的具体用法?C# SpecialFolderOption怎么用?C# SpecialFolderOption使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SpecialFolderOption类属于命名空间,在下文中一共展示了SpecialFolderOption类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnixGetFolderPath
// needed by our BCL, e.g. IsolatedStorageFile.cs
internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
{
var dir = iOSGetFolderPath (folder);
if ((option == SpecialFolderOption.Create) && !Directory.Exists (dir))
Directory.CreateDirectory (dir);
return dir;
}
示例2: GetFolderPathCore
private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
{
// Get the path for the SpecialFolder
string path = GetFolderPathCoreWithoutValidation(folder);
Debug.Assert(path != null);
// If we didn't get one, or if we got one but we're not supposed to verify it,
// or if we're supposed to verify it and it passes verification, return the path.
if (path.Length == 0 ||
option == SpecialFolderOption.DoNotVerify ||
Interop.Sys.Access(path, Interop.Sys.AccessMode.R_OK) == 0)
{
return path;
}
// Failed verification. If None, then we're supposed to return an empty string.
// If Create, we're supposed to create it and then return the path.
if (option == SpecialFolderOption.None)
{
return string.Empty;
}
else
{
Debug.Assert(option == SpecialFolderOption.Create);
// TODO #11151: Replace with Directory.CreateDirectory once we have access to System.IO.FileSystem here.
Action<string> createDirectory = LazyInitializer.EnsureInitialized(ref s_directoryCreateDirectory, () =>
{
Type dirType = Type.GetType("System.IO.Directory, System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: true);
MethodInfo mi = dirType.GetTypeInfo().GetDeclaredMethod("CreateDirectory");
return (Action<string>)mi.CreateDelegate(typeof(Action<string>));
});
createDirectory(path);
return path;
}
}
示例3: UnixGetFolderPath
// the security runtime (and maybe other parts of corlib) needs the
// information to initialize themselves before permissions can be checked
internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
{
string home = internalGetHome ();
// http://freedesktop.org/Standards/basedir-spec/basedir-spec-0.6.html
// note: skip security check for environment variables
string data = internalGetEnvironmentVariable ("XDG_DATA_HOME");
if ((data == null) || (data == String.Empty)) {
data = Path.Combine (home, ".local");
data = Path.Combine (data, "share");
}
// note: skip security check for environment variables
string config = internalGetEnvironmentVariable ("XDG_CONFIG_HOME");
if ((config == null) || (config == String.Empty)) {
config = Path.Combine (home, ".config");
}
switch (folder) {
// MyComputer is a virtual directory
case SpecialFolder.MyComputer:
return String.Empty;
// personal == ~
case SpecialFolder.Personal:
#if MONOTOUCH
return Path.Combine (home, "Documents");
#else
return home;
#endif
// use FDO's CONFIG_HOME. This data will be synced across a network like the windows counterpart.
case SpecialFolder.ApplicationData:
#if MONOTOUCH
{
string dir = Path.Combine (Path.Combine (home, "Documents"), ".config");
if (option == SpecialFolderOption.Create){
if (!Directory.Exists (dir))
Directory.CreateDirectory (dir);
}
return dir;
}
#else
return config;
#endif
//use FDO's DATA_HOME. This is *NOT* synced
case SpecialFolder.LocalApplicationData:
#if MONOTOUCH
{
string dir = Path.Combine (home, "Documents");
if (!Directory.Exists (dir))
Directory.CreateDirectory (dir);
return dir;
}
#else
return data;
#endif
case SpecialFolder.Desktop:
case SpecialFolder.DesktopDirectory:
return ReadXdgUserDir (config, home, "XDG_DESKTOP_DIR", "Desktop");
case SpecialFolder.MyMusic:
if (Platform == PlatformID.MacOSX)
return Path.Combine (home, "Music");
else
return ReadXdgUserDir (config, home, "XDG_MUSIC_DIR", "Music");
case SpecialFolder.MyPictures:
if (Platform == PlatformID.MacOSX)
return Path.Combine (home, "Pictures");
else
return ReadXdgUserDir (config, home, "XDG_PICTURES_DIR", "Pictures");
case SpecialFolder.Templates:
return ReadXdgUserDir (config, home, "XDG_TEMPLATES_DIR", "Templates");
#if NET_4_0 || MOONLIGHT || MOBILE
case SpecialFolder.MyVideos:
return ReadXdgUserDir (config, home, "XDG_VIDEOS_DIR", "Videos");
#endif
#if NET_4_0
case SpecialFolder.CommonTemplates:
return "/usr/share/templates";
case SpecialFolder.Fonts:
if (Platform == PlatformID.MacOSX)
return Path.Combine (home, "Library", "Fonts");
return Path.Combine (home, ".fonts");
#endif
// these simply dont exist on Linux
// The spec says if a folder doesnt exist, we
// should return ""
case SpecialFolder.Favorites:
if (Platform == PlatformID.MacOSX)
return Path.Combine (home, "Library", "Favorites");
else
return String.Empty;
//.........这里部分代码省略.........
示例4: GetFolderPath
static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
{
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
string dir = null;
if (Environment.IsRunningOnWindows)
dir = GetWindowsFolderPath ((int) folder);
else
dir = UnixGetFolderPath (folder, option);
#if !NET_2_1
if ((dir != null) && (dir.Length > 0) && SecurityManager.SecurityEnabled) {
new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dir).Demand ();
}
#endif
return dir;
}
示例5: GetKnownFolderPath
private static string GetKnownFolderPath(string folderGuid, SpecialFolderOption option)
{
Guid folderId = new Guid(folderGuid);
string path;
int hr = Interop.mincore.SHGetKnownFolderPath(folderId, (uint)option, IntPtr.Zero, out path);
if (hr != 0) // Not S_OK
{
if (hr == Interop.mincore.COR_E_PLATFORMNOTSUPPORTED)
{
throw new PlatformNotSupportedException();
}
else
{
return string.Empty;
}
}
return path;
}
示例6: GetFolderPathCore
private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
{
// We're using SHGetKnownFolderPath instead of SHGetFolderPath as SHGetFolderPath is
// capped at MAX_PATH.
//
// Because we validate both of the input enums we shouldn't have to care about CSIDL and flag
// definitions we haven't mapped. If we remove or loosen the checks we'd have to account
// for mapping here (this includes tweaking as SHGetFolderPath would do).
//
// The only SpecialFolderOption defines we have are equivalent to KnownFolderFlags.
string folderGuid;
switch (folder)
{
case SpecialFolder.ApplicationData:
folderGuid = Interop.mincore.KnownFolders.RoamingAppData;
break;
case SpecialFolder.CommonApplicationData:
folderGuid = Interop.mincore.KnownFolders.ProgramData;
break;
case SpecialFolder.LocalApplicationData:
folderGuid = Interop.mincore.KnownFolders.LocalAppData;
break;
case SpecialFolder.Cookies:
folderGuid = Interop.mincore.KnownFolders.Cookies;
break;
case SpecialFolder.Desktop:
folderGuid = Interop.mincore.KnownFolders.Desktop;
break;
case SpecialFolder.Favorites:
folderGuid = Interop.mincore.KnownFolders.Favorites;
break;
case SpecialFolder.History:
folderGuid = Interop.mincore.KnownFolders.History;
break;
case SpecialFolder.InternetCache:
folderGuid = Interop.mincore.KnownFolders.InternetCache;
break;
case SpecialFolder.Programs:
folderGuid = Interop.mincore.KnownFolders.Programs;
break;
case SpecialFolder.MyComputer:
folderGuid = Interop.mincore.KnownFolders.ComputerFolder;
break;
case SpecialFolder.MyMusic:
folderGuid = Interop.mincore.KnownFolders.Music;
break;
case SpecialFolder.MyPictures:
folderGuid = Interop.mincore.KnownFolders.Pictures;
break;
case SpecialFolder.MyVideos:
folderGuid = Interop.mincore.KnownFolders.Videos;
break;
case SpecialFolder.Recent:
folderGuid = Interop.mincore.KnownFolders.Recent;
break;
case SpecialFolder.SendTo:
folderGuid = Interop.mincore.KnownFolders.SendTo;
break;
case SpecialFolder.StartMenu:
folderGuid = Interop.mincore.KnownFolders.StartMenu;
break;
case SpecialFolder.Startup:
folderGuid = Interop.mincore.KnownFolders.Startup;
break;
case SpecialFolder.System:
folderGuid = Interop.mincore.KnownFolders.System;
break;
case SpecialFolder.Templates:
folderGuid = Interop.mincore.KnownFolders.Templates;
break;
case SpecialFolder.DesktopDirectory:
folderGuid = Interop.mincore.KnownFolders.Desktop;
break;
case SpecialFolder.Personal:
// Same as Personal
// case SpecialFolder.MyDocuments:
folderGuid = Interop.mincore.KnownFolders.Documents;
break;
case SpecialFolder.ProgramFiles:
folderGuid = Interop.mincore.KnownFolders.ProgramFiles;
break;
case SpecialFolder.CommonProgramFiles:
folderGuid = Interop.mincore.KnownFolders.ProgramFilesCommon;
break;
case SpecialFolder.AdminTools:
folderGuid = Interop.mincore.KnownFolders.AdminTools;
break;
case SpecialFolder.CDBurning:
folderGuid = Interop.mincore.KnownFolders.CDBurning;
break;
case SpecialFolder.CommonAdminTools:
folderGuid = Interop.mincore.KnownFolders.CommonAdminTools;
break;
case SpecialFolder.CommonDocuments:
folderGuid = Interop.mincore.KnownFolders.PublicDocuments;
break;
case SpecialFolder.CommonMusic:
folderGuid = Interop.mincore.KnownFolders.PublicMusic;
//.........这里部分代码省略.........
示例7: InternalGetFolderPath
private static string InternalGetFolderPath(SpecialFolder folder, SpecialFolderOption option, bool suppressSecurityChecks = false)
{
#if FEATURE_CORESYSTEM
// This is currently customized for Windows Phone since CoreSystem doesn't support
// SHGetFolderPath. The allowed folder values are based on the version of .NET CF WP7 was using.
switch (folder)
{
case SpecialFolder.System:
return SystemDirectory;
case SpecialFolder.ApplicationData:
case SpecialFolder.Favorites:
case SpecialFolder.Programs:
case SpecialFolder.StartMenu:
case SpecialFolder.Startup:
case SpecialFolder.Personal:
throw new PlatformNotSupportedException();
default:
throw new PlatformNotSupportedException();
}
#else // FEATURE_CORESYSTEM
#if !FEATURE_CORECLR
if (option == SpecialFolderOption.Create && !suppressSecurityChecks) {
FileIOPermission createPermission = new FileIOPermission(PermissionState.None);
createPermission.AllFiles = FileIOPermissionAccess.Write;
createPermission.Demand();
}
#endif
StringBuilder sb = new StringBuilder(Path.MAX_PATH);
int hresult = Win32Native.SHGetFolderPath(IntPtr.Zero, /* hwndOwner: [in] Reserved */
((int)folder | (int)option), /* nFolder: [in] CSIDL */
IntPtr.Zero, /* hToken: [in] access token */
Win32Native.SHGFP_TYPE_CURRENT, /* dwFlags: [in] retrieve current path */
sb); /* pszPath: [out]resultant path */
String s;
if (hresult < 0)
{
switch (hresult)
{
default:
// The previous incarnation threw away all errors. In order to limit
// breaking changes, we will be permissive about these errors
// instead of calling ThowExceptionForHR.
//Runtime.InteropServices.Marshal.ThrowExceptionForHR(hresult);
break;
case __HResults.COR_E_PLATFORMNOTSUPPORTED:
// This one error is the one we do want to throw.
// <STRIP>
throw new PlatformNotSupportedException();
}
// SHGetFolderPath does not initialize the output buffer on error
s = String.Empty;
}
else
{
s = sb.ToString();
}
if (!suppressSecurityChecks)
{
// On CoreCLR we can check with the host if we're not trying to use any special options.
// Otherwise, we need to do a full demand since hosts aren't expecting to handle requests to
// create special folders.
#if FEATURE_CORECLR
if (option == SpecialFolderOption.None)
{
FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, s);
state.EnsureState();
}
else
#endif // FEATURE_CORECLR
{
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, s).Demand();
}
}
return s;
#endif // FEATURE_CORESYSTEM
}
示例8: GetFolderPath
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option) {
if (!Enum.IsDefined(typeof(SpecialFolder),folder))
throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)folder));
if (!Enum.IsDefined(typeof(SpecialFolderOption),option))
throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)option));
Contract.EndContractBlock();
return InternalGetFolderPath(folder, option);
}
示例9: GetFolderPath
static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
{
string dir = null;
if (Environment.IsRunningOnWindows) {
dir = GetWindowsFolderPath ((int) folder);
} else {
dir = InternalGetFolderPath (folder);
}
#if !NET_2_1
if ((dir != null) && (dir.Length > 0) && SecurityManager.SecurityEnabled) {
new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dir).Demand ();
}
#endif
return dir;
}
示例10: GetFolderPathCore
private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
{
return string.Empty;
}
示例11: GetFolderPath
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
{
if (!Enum.IsDefined(typeof(SpecialFolder), folder))
{
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
}
if (option != SpecialFolderOption.None && !Enum.IsDefined(typeof(SpecialFolderOption), option))
{
throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
}
return GetFolderPathCore(folder, option);
}
示例12: GetFolderPath
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
{
if (!Enum.IsDefined(typeof(SpecialFolder), folder))
{
throw new ArgumentException(GetResourceString("Arg_EnumIllegalVal", new object[] { (int) folder }));
}
if (!Enum.IsDefined(typeof(SpecialFolderOption), option))
{
throw new ArgumentException(GetResourceString("Arg_EnumIllegalVal", new object[] { (int) option }));
}
if (option == SpecialFolderOption.Create)
{
new FileIOPermission(PermissionState.None) { AllFiles = FileIOPermissionAccess.Write }.Demand();
}
StringBuilder lpszPath = new StringBuilder(260);
int num = Win32Native.SHGetFolderPath(IntPtr.Zero, (int) (folder | ((SpecialFolder) ((int) option))), IntPtr.Zero, 0, lpszPath);
if (num < 0)
{
switch (num)
{
case -2146233031:
throw new PlatformNotSupportedException();
}
}
string path = lpszPath.ToString();
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
return path;
}
示例13: GetFolderPath
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option) {
if (!Enum.IsDefined(typeof(SpecialFolder),folder))
throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)folder));
if (!Enum.IsDefined(typeof(SpecialFolderOption),option))
throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)option));
Contract.EndContractBlock();
if (option == SpecialFolderOption.Create) {
FileIOPermission createPermission = new FileIOPermission(PermissionState.None);
createPermission.AllFiles = FileIOPermissionAccess.Write;
createPermission.Demand();
}
StringBuilder sb = new StringBuilder(Path.MAX_PATH);
int hresult = Win32Native.SHGetFolderPath(IntPtr.Zero, /* hwndOwner: [in] Reserved */
((int)folder | (int)option), /* nFolder: [in] CSIDL */
IntPtr.Zero, /* hToken: [in] access token */
Win32Native.SHGFP_TYPE_CURRENT, /* dwFlags: [in] retrieve current path */
sb); /* pszPath: [out]resultant path */
if (hresult < 0)
{
switch (hresult)
{
default:
// The previous incarnation threw away all errors. In order to limit
// breaking changes, we will be permissive about these errors
// instead of calling ThowExceptionForHR.
//Runtime.InteropServices.Marshal.ThrowExceptionForHR(hresult);
break;
case __HResults.COR_E_PLATFORMNOTSUPPORTED:
// This one error is the one we do want to throw.
// <STRIP>
throw new PlatformNotSupportedException();
}
}
String s = sb.ToString();
new FileIOPermission( FileIOPermissionAccess.PathDiscovery, s ).Demand();
return s;
}
示例14: GetFolderPath
public static string GetFolderPath (SpecialFolder folder, SpecialFolderOption option)
{
return UnixGetFolderPath (folder, option);
}
示例15: GetFolderPath
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption options) {
Contract.Ensures(Contract.Result<string>() != null);
return default(string);
}