using子句会关闭此流吗?

我显然已经习惯了一个糟糕的编码习惯。 以下是我编写的代码示例: using(StreamReader sr = new StreamReader(File.Open(“somefile.txt”, FileMode.Open))) { //read file } File.Move(“somefile.txt”, “somefile.bak”); //can’t move, get exception that I the file is open 我认为因为using子句在StreamReader上显式调用了Close()和Dispose() , FileStream也会关闭。 我能解决问题的唯一方法是将上面的块更改为: using(FileStream fs = File.Open(“somefile.txt”, FileMode.Open)) { using(StreamReader sr = new StreamReader(fs)) { //read file } } File.Move(“somefile.txt”, “somefile.bak”); // can move file with no errors 是否应该通过在第一个块中处理关闭StreamReader来关闭底层的FileStream ? […]

Null-coalescing运算符为动态对象的属性返回null

我最近在使用Json.NET将JSON解析为动态对象时发现了null-coalescing运算符的问题。 假设这是我的动态对象: string json = “{ \”phones\”: { \”personal\”: null }, \”birthday\”: null }”; dynamic d = JsonConvert.DeserializeObject(json); 如果我尝试使用?? 运算符在d的一个字段上,它返回null: string s = “”; s += (d.phones.personal ?? “default”); Console.WriteLine(s + ” ” + s.Length); //outputs 0 但是,如果我将动态属性分配给字符串,那么它可以正常工作: string ss = d.phones.personal; string s = “”; s += (ss ?? “default”); Console.WriteLine(s + ” ” […]

在引号之间获取值

如何使用RegEx获取引号之间的值 例如,我想从function测试中找到所有参数 test(“bla”); print(“foo”); test(“moo”); 结果必须是{“bla”,“moo”}

使用Linq在集合中的指定元素之后查找元素

我有一份有序的人员名单。 我有一个我认识的人存在于该系列中。 如何确定列表中的下一个人?

INSERT与UPDATE

我有以下查询: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“chestionar”].ConnectionString); SqlCommand cmd = new SqlCommand(“INSERT INTO Raspunsuri Values(@raspuns,@cnp,@data,’1′,@ip,@idsesiune)”, con); cmd.Parameters.AddWithValue(“@cnp”, Session[“sesiune_cnp”]); cmd.Parameters.AddWithValue(“@raspuns”, textbox1.Text); cmd.Parameters.AddWithValue(“@data”, DateTime.Now.ToLocalTime()); cmd.Parameters.AddWithValue(“@ip”,ip); cmd.Parameters.AddWithValue(“@idsesiune”, id_sesiune); try { con.Open(); cmd.ExecuteNonQuery(); Response.Redirect(“User2.aspx”); } catch (Exception ex) { Console.WriteLine(“Error:” + ex); } finally { con.Close(); } 我需要的是看表中是否有任何记录,如果有更新,否则插入它。我怎么能实现呢?

如何用Reflection + C#获取所有引用

使用System.Reflection,我可以从特定的类中获取所有方法 我需要知道这些方法的参考是什么。 例如:在Visual Studio中,如果需要特定对象的引用 右键单击对象并选择“查找所有引用” Visual Studio显示此选定对象的引用 我想做同样的事情,但是从reflection或其他方式的代码。 我可以这样做吗?

替换字符串中的多个单词

我有多个单词我想用值替换,最好的方法是什么? 示例:这就是我所做的,但感觉和看起来都错了 string s =”Dear , your booking is confirmed for the “; string s1 = s.Replace(“”, client.FullName); string s2 =s1.Replace(“”, event.EventDate.ToString()); txtMessage.Text = s2; 一定有更好的方法? 谢谢

等待TcpClient数据可用的最佳方法?

while (TcpClient.Client.Available == 0) { Thread.Sleep(5); } 有一个更好的方法吗?

linq to nhibernate compareto不支持

我有linq到nhibernate查询: var listka = from i in FakturyZakupu.Queryable where String.Compare(i.REJESTRY.REJ_KOD,sbWartoscBetween1.ToString()) >= 0 && String.Compare(i.REJESTRY.REJ_KOD,sbWartoscBetween2.ToString()) <= 0 select i; lista = listka.ToList(); 它编译得非常好,但是如果我使用它,则抛出exception: NotSupportedException int32 CompareTo(System.String,System.String) 我怎样才能在两个值之间使用linq查询字符串值。 就像在SQL中一样:select * from table from a a和b之间的id?

无打印机选择对话框打印

我想直接打印我的水晶报告,没有打印机选择弹出窗口。 我怎样才能做到这一点 ? myReportDocument.SetDataSource(saveDataSet); //Print crystalReportViewer1.ShowRefreshButton = false; crystalReportViewer1.ShowCloseButton = false; crystalReportViewer1.ShowGroupTreeButton = false; crystalReportViewer1.ReportSource = myReportDocument; crystalReportViewer1.PrintReport(); 我正在使用默认(且仅限)打印机。