internal static string GetModuleFileName(IntPtr module) { const int BufferSize = 32768; StringBuilder sb = new(BufferSize); uint result = GetModuleFileName(module, sb, sb.Capacity); if (result == 0) { throw Exceptions.Log(new Win32Exception(Marshal.GetLastWin32Error(), "Error calling GetModuleFileName for the specified HMODULE.")); } else { string fileName = sb.ToString(); // See the docs for GetModuleFileName and the "Naming a File" MSDN topic. const string longUncPrefix = @"\\?\UNC\"; const string longPrefix = @"\\?\"; if (fileName.StartsWith(longUncPrefix)) { fileName = fileName.Substring(longUncPrefix.Length); } else if (fileName.StartsWith(longPrefix)) { fileName = fileName.Substring(longPrefix.Length); } return(fileName); } }
/// <summary> /// Validates the XML using the given schemas and throws an XmlSchemaValidationException /// if there are errors or warnings. /// </summary> /// <param name="xml">The XML to validate.</param> /// <param name="schemas">The schemas to use for validation.</param> public static void RequireValidation(this XElement xml, XmlSchemaSet schemas) { IList <ValidationEventArgs> errors = xml.Validate(schemas); if (errors.Count > 0) { StringBuilder sb = new(); foreach (var error in errors) { if (sb.Length > 0) { sb.AppendLine(); } sb.Append(error.Severity).Append(": ").Append(error.Message); // Try to include position info and other details if they're available. XmlSchemaException ex = error.Exception; if (ex != null) { // According to the docs for IXmlLineInfo, the line number and position are 1-based, // but they'll only be set for XElements if LoadOptions.SetLineInfo was used. if (ex.LineNumber > 0 && ex.LinePosition > 0) { sb.AppendFormat(" (Line: {0}, Position: {1})", ex.LineNumber, ex.LinePosition); } else if (ex.LineNumber > 0) { sb.AppendFormat(" (Line: {0})", ex.LineNumber); } if (!string.IsNullOrEmpty(ex.SourceUri)) { sb.AppendFormat(" (Schema URI: {0})", ex.SourceUri); } var sourceObject = ex.SourceSchemaObject; if (sourceObject != null) { if (sourceObject.LineNumber > 0 && sourceObject.LinePosition > 0) { sb.AppendFormat(" (Schema Line: {0}, Schema Position: {1})", sourceObject.LineNumber, sourceObject.LinePosition); } else if (sourceObject.LineNumber > 0) { sb.AppendFormat(" (Schema Line: {0})", sourceObject.LineNumber); } } } } XmlSchemaValidationException result = new(sb.ToString()); result.Data.Add("ValidationErrors", errors); throw Exceptions.Log(result); } }
internal static string[] SplitCommandLine(string commandLine) { // This logic came from http://www.pinvoke.net/default.aspx/shell32/CommandLineToArgvW.html // and also from http://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp. // Use CommandLineToArgvW because it handles a variety of special quote and backslash cases such as: // /c:"1 2" is parsed as a single arg: /c:1 2 // Double double quotes ("") become a single escaped double quote usually. // Backslash double quote may be parsed as an escaped double quote. // See: "What's up with the strange treatment of quotation marks and backslashes by CommandLineToArgvW" // http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx // Also: "How is the CommandLineToArgvW function intended to be used?" // http://blogs.msdn.com/b/oldnewthing/archive/2010/09/16/10062818.aspx // And: "The first word on the command line is the program name only by convention" // http://blogs.msdn.com/b/oldnewthing/archive/2006/05/15/597984.aspx IntPtr ptrToSplitArgs = CommandLineToArgvW(commandLine, out int numberOfArgs); // CommandLineToArgvW returns NULL upon failure. if (ptrToSplitArgs == IntPtr.Zero) { // The inner Win32Exception will use the last error code set by CommandLineToArgvW. throw Exceptions.Log(new ArgumentException("Unable to split command line: " + commandLine, new Win32Exception())); } // Make sure the memory ptrToSplitArgs to is freed, even upon failure. try { string[] splitArgs = new string[numberOfArgs]; // ptrToSplitArgs is an array of pointers to null terminated Unicode strings. // Copy each of these strings into our split argument array. for (int i = 0; i < numberOfArgs; i++) { IntPtr lpwstr = Marshal.ReadIntPtr(ptrToSplitArgs, i * IntPtr.Size); splitArgs[i] = Marshal.PtrToStringUni(lpwstr) !; } return(splitArgs); } finally { // Free memory obtained by CommandLineToArgW. // In .NET the LocalFree API is exposed by Marshal.FreeHGlobal. Marshal.FreeHGlobal(ptrToSplitArgs); } }
private static NotSupportedException NewNotSupportedException() => Exceptions.Log(new NotSupportedException("The set is read-only."));