/// <summary> /// Convert a path into a fully qualified local file path. /// </summary> /// <param name="path">The path to convert.</param> /// <returns>The fully qualified path.</returns> /// <remarks> /// <para> /// Converts the path specified to a fully /// qualified path. If the path is relative it is /// taken as relative from the application base /// directory. /// </para> /// <para> /// The path specified must be a local file path, a URI is not supported. /// </para> /// </remarks> public static string ConvertToFullPath(string path) { if (path == null) { throw new ArgumentNullException("path"); } string baseDirectory = ""; try { string applicationBaseDirectory = SystemInfo.ApplicationBaseDirectory; if (applicationBaseDirectory != null) { // applicationBaseDirectory may be a URI not a local file path Uri applicationBaseDirectoryUri = new Uri(applicationBaseDirectory); if (applicationBaseDirectoryUri.IsFile) { baseDirectory = applicationBaseDirectoryUri.LocalPath; } } } catch (Exception ex) { // Ignore URI exceptions & SecurityExceptions from SystemInfo.ApplicationBaseDirectory LogLog.Warn("Insufficient permissions to get the path: " + ex.Message); } if (baseDirectory != null && baseDirectory.Length > 0) { // Note that Path.Combine will return the second path if it is rooted return(Path.GetFullPath(Path.Combine(baseDirectory, path))); } return(Path.GetFullPath(path)); }
/// <summary> /// Writes output to the standard error stream. /// </summary> /// <param name="message">The message to log.</param> /// <remarks> /// <para> /// Writes to both Console.Error and System.Diagnostics.Trace. /// Note that the System.Diagnostics.Trace is not supported /// on the Compact Framework. /// </para> /// <para> /// If the AppDomain is not configured with a config file then /// the call to System.Diagnostics.Trace may fail. This is only /// an issue if you are programmatically creating your own AppDomains. /// </para> /// </remarks> private static void EmitErrorLine(string message) { try { #if NETCF Console.WriteLine(message); //System.Diagnostics.Trace.WriteLine(message); #else Console.Error.WriteLine(message); Debug.WriteLine(message); #endif } catch { // Ignore exception, what else can we do? Not really a good idea to propagate back to the caller LogLog.Warn("Exception while trying to write in console"); } }
/// <summary> /// Parse a string into an <see cref="Int64"/> value /// </summary> /// <param name="s">the string to parse</param> /// <param name="val">out param where the parsed value is placed</param> /// <returns><c>true</c> if the string was able to be parsed into an integer</returns> /// <remarks> /// <para> /// Attempts to parse the string into an integer. If the string cannot /// be parsed then this method returns <c>false</c>. The method does not throw an exception. /// </para> /// </remarks> public static bool TryParse(string s, out long val) { #if NETCF val = 0; try { val = long.Parse(s, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture); return(true); } catch { } return(false); #else // Initialise out param val = 0; try { double doubleVal; if (Double.TryParse(s, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out doubleVal)) { val = Convert.ToInt64(doubleVal); return(true); } } catch (Exception ex) { // Ignore exception, just return false LogLog.Warn("Insufficient permissions to parse: " + ex.Message); } return(false); #endif }
/// <summary> /// Loads the type specified in the type string. /// </summary> /// <param name="relativeAssembly">An assembly to load the type from.</param> /// <param name="typeName">The name of the type to load.</param> /// <param name="throwOnError">Flag set to <c>true</c> to throw an exception if the type cannot be loaded.</param> /// <param name="ignoreCase"><c>true</c> to ignore the case of the type name; otherwise, <c>false</c></param> /// <returns>The type loaded or <c>null</c> if it could not be loaded.</returns> /// <remarks> /// <para> /// If the type name is fully qualified, i.e. if contains an assembly name in /// the type name, the type will be loaded from the system using /// <see cref="Type.GetType(string,bool)"/>. /// </para> /// <para> /// If the type name is not fully qualified it will be loaded from the specified /// assembly. If the type is not found in the assembly then all the loaded assemblies /// will be searched for the type. /// </para> /// </remarks> public static Type GetTypeFromString(Assembly relativeAssembly, string typeName, bool throwOnError, bool ignoreCase) { // Check if the type name specifies the assembly name if (typeName.IndexOf(',') == -1) { //LogLog.Trace("SystemInfo: Loading type ["+typeName+"] from assembly ["+relativeAssembly.FullName+"]"); #if NETCF return(relativeAssembly.GetType(typeName, throwOnError)); #else // Attempt to lookup the type from the relativeAssembly Type type = relativeAssembly.GetType(typeName, false, ignoreCase); if (type != null) { // Found type in relative assembly //LogLog.Trace("SystemInfo: Loaded type ["+typeName+"] from assembly ["+relativeAssembly.FullName+"]"); return(type); } Assembly[] loadedAssemblies = null; try { loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); } catch (System.Security.SecurityException ex) { // Insufficient permissions to get the list of loaded assemblies LogLog.Warn("Insufficient permissions to get the list of loaded assemblies: " + ex.Message); } if (loadedAssemblies != null) { // Search the loaded assemblies for the type foreach (Assembly assembly in loadedAssemblies) { type = assembly.GetType(typeName, false, ignoreCase); if (type != null) { // Found type in loaded assembly LogLog.Trace("SystemInfo: Loaded type [" + typeName + "] from assembly [" + assembly.FullName + "] by searching loaded assemblies."); return(type); } } } // Didn't find the type if (throwOnError) { throw new TypeLoadException("Could not load type [" + typeName + "]. Tried assembly [" + relativeAssembly.FullName + "] and all loaded assemblies"); } return(null); #endif } else { // Includes explicit assembly name //LogLog.Trace("SystemInfo: Loading type ["+typeName+"] from global Type"); #if NETCF return(Type.GetType(typeName, throwOnError)); #else return(Type.GetType(typeName, throwOnError, ignoreCase)); #endif } }