Tag: exception

使用Process.Start启动Firefox:当您设置Usename和Password时,Firefox无法启动

当我尝试使用Process.Start和ProcessStartInfo(.NET)启动Firefox时,一切似乎都能正常工作。 但是当我指定另一个帐户(用户的成员)的用户名和密码时,似乎什么也没发生。 相同的代码适用于Calc.exe或IE。 这很奇怪。 有任何想法吗? 这是代码: System.Diagnostics.ProcessStartInfo pInfo = new System.Diagnostics.ProcessStartInfo(); pInfo.CreateNoWindow = false; pInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; pInfo.WorkingDirectory = “{WorkingDirectory}”; pInfo.Arguments = “{CommandLineArgs}”; pInfo.FileName = “{ExecutableAddress}”; pInfo.ErrorDialog = true; pInfo.UseShellExecute = false; pInfo.UserName = “{LimitedAccountUserName}”; pInfo.Password = “{SecureLimitedAccountPassword}”; System.Diagnostics.Process.Start(pInfo); 感谢大家。

如何从C#windows应用程序项目调用C ++ DLL

我使用Visual Studio中的类库项目在C ++中创建了一个dll。 我需要从C#应用程序中调用dll中的方法。 我知道有2个approches。 一种是将dll项目引用添加到C#项目或使用DllExport导出方法。 但是,当我尝试两种方式时,它总是在运行时调用dll方法时出现以下错误。 TestClient.exe中发生未处理的“System.BadImageFormatException”类型exception附加信息:尝试加载格式不正确的程序。 (HRESULTexception:0x8007000B) 我可以知道如何避免这个问题吗? 提前致谢!

Try / Catch没有抓住

在下面的代码中,我故意将“@fooData”错误地输入到“@ foo111Data”以检查try语句是否正在捕获我的exception。 见下面的代码。 但是try / catch语句没有在MessageBox中捕获并显示和exception,而VS2010只是分解并突出显示错误代码行。 try { conn.Open(); cmd.Parameters.AddWithValue(“@foo111Data”, dataStrTb1.Text); cmd.ExecuteNonQuery(); } catch (SqlCeException ex) { MessageBox.Show(ex.ToString()); } finally { conn.Close(); }

.NET远程处理exception:权限被拒绝:无法远程调用非公共或静态方法

我正在编写一个程序,它允许加载特定的托管.DLL文件并使用它。 由于我希望能够卸载.DLL文件,我正在创建两个AppDomain – 一个用于应用程序本身,另一个用于当前加载的.DLL。 由于加载的.DLL中的大多数对象都没有很好地序列化,我正在创建一个MarshalByRefObject包装类,它将对象本身保存在自己的AppDomain中,并将一些reflection函数暴露给主应用程序AppDomain。 但是,当我尝试在远程对象上调用方法时,我遇到了exception: 权限被拒绝:无法远程调用非公共或静态方法。 这很奇怪,因为我根本没有使用任何非公共或静态方法。 从本质上讲,我所拥有的是: class RemoteObjectWrapper: MarshalByRefObject { private Type SourceType; private object Source; public RemoteObjectWrapper(object source) { if (source == null) throw new ArgumentNullException(“source”); this.Source = source; this.SourceType = source.GetType(); } public T WrapValue(object value) { if ( value == null ) return default(T); var TType = typeof(T); if […]

实现通用自定义exception的优点和缺点

实现自定义exception的优缺点如下: 创建一个枚举,在其描述中表示错误消息: public class Enums { public enum Errors { [Description(“This is a test exception”)] TestError, … } } 创建自定义exception类: public class CustomException : ApplicationException { protected Enums.Errors _customError; public CustomException(Enums.Errors customError) { this._customError = customError; } public override string Message { get { return this._customError!= Enums.Errors.Base ? this.customError.GetDescription() : base.Message; } } } GetDescription方法是一个枚举扩展方法,它使用reflection获取枚举描述。 […]

MongoDB C#驱动程序检查身份validation状态和角色

这是我使用MongoDB身份validation机制登录MongoDB的代码。 try { var credential = MongoCredential.CreateMongoCRCredential(“test”, “admin”, “123456”); var settings = new MongoClientSettings { Credentials = new[] { credential } }; var mongoClient = new MongoClient(settings); var _database = mongoClient.GetDatabase(“test”); var collection = _database.GetCollection(“book”); var filter = new BsonDocument(); var document = collection.Find(new BsonDocument()).ToList(); } catch (Exception ex) { } 当我们在Credential中输入错误的用户名/密码时,如何检查登录结果? 目前我无法检查它,我必须等待collection.Find().ToList()抛出一个TimeoutException ,并在此上下文中它的身份validation失败。 我们必须创建一个CRUD来检查身份validation结果(通过捕获TimeoutException […]

如何在C#中打开程序集绑定日志记录?

我遇到了这个错误,无法想象缺少什么: {“Could not load file or assembly ‘xxxxx, Version=1.0.0.42, Culture=neutral, PublicKeyToken=c5067b3d1110b13c’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)”:”xxxxx, Version=1.0.0.42, Culture=neutral, PublicKeyToken=c5067b3d1110b13c”} 在详细的exception窗口中,它还告诉我: WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to […]

捕捉一般例外情况

根据这篇MSDN文章 ,你不应该抓住一般的例外情况。 我确定有一个stackoverflow问题处理这个问题,我理解为什么它不是一个好的做法,但我今天在另一篇MSDN文章中看到了这个例子: using System; using System.IO; class Test { public static void Main() { try { using (StreamReader sr = new StreamReader(“TestFile.txt”)) { String line = sr.ReadToEnd(); Console.WriteLine(line); } } catch (Exception e) { Console.WriteLine(“The file could not be read:”); Console.WriteLine(e.Message); } } } 是否有任何理由在该示例中捕获一般exception,或者只是他们懒得编写一个示例来捕获所有特定exception?

我没有关闭以前的DataReader,但在哪里?

我已经更改了以前的代码,所以我没有使用’使用’。 它工作得更早,不同类中的代码基本上代表相同的东西,但它的工作。 我现在已经盯着它看了2个小时,我无法弄清问题可能出在哪里。 我只有一个阅读器,但每次我使用DisplayFileContent方法时都会收到错误:错误: There is already an open DataReader associated with this command which must be closed first. // May be public so we can display // content of file from different forms. public void DisplayFileContent(string filePath) { // Counting all entries. int countEntries = 0; // Encrypting/Decrypting data. EncryptDecrypt security = new EncryptDecrypt(); […]

是否有一个提升lexical_cast相当于C#TryParse?

简介(来自Eric Lippert博客): 不幸的例外是不幸的设计决定的结果。 在完全非特殊情况下抛出exception情况,因此必须始终抓住并处理。 一个令人烦恼的exception的典型例子是Int32.Parse,如果你给它一个无法解析为整数的字符串,它会抛出。 但是这个方法的99%用例是转换用户输入的字符串,这可能是任何旧的东西,因此解析失败绝不是例外。 更糟糕的是,如果没有自己实现整个方法,调用者就无法提前确定他们的参数是否是坏的,在这种情况下,他们不需要首先调用它。 现在重要的部分: 这个不幸的设计决定非常令人烦恼,当然框架团队很快就会实施TryParse,这样做是正确的。 来自MSDN Int32.TryParse: 返回值类型:System.Boolean如果s已成功转换,则为true;否则为false。 否则,是的。 所以同事们正在研究一些需要检查一个字符串是否为数字的代码,所以在考虑它并意识到没有好的C ++解决方案之后(基本上它是for__each / find_if或boost:lexical_cast try catch)我想拥有is_convertible或者来自boost的东西会有多好? 我可以包装boost lexical_cast并在try块结束时返回true并在catch块结束时返回false但我更喜欢现有的做法:)解决方案。