Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

3
在C#中重命名目录
在这里很难说出要问什么。这个问题是模棱两可,含糊,不完整,过于宽泛或夸张的,不能以目前的形式合理地回答。如需帮助澄清此问题以便可以重新打开, 请访问帮助中心。 11年前关闭。 我在任何地方都找不到DirectoryInfo.Rename(To)或FileInfo.Rename(To)方法。因此,我写了我自己的文章,并把它发布在这里,以供任何需要的人使用,因为我们要面对现实:MoveTo方法过于强大,如果您只想重命名目录或文件,则总是需要额外的逻辑: public static class DirectoryExtensions { public static void RenameTo(this DirectoryInfo di, string name) { if (di == null) { throw new ArgumentNullException("di", "Directory info to rename cannot be null"); } if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("New name cannot be null or blank", "name"); } di.MoveTo(Path.Combine(di.Parent.FullName, name)); …
69 c#  file-io  directory  rename 

2
在Moq Callback()调用中设置变量值
我想我可能对Moq回调方法的语法有些困惑。当我尝试做这样的事情: IFilter filter = new Filter(); List<IFoo> objects = new List<IFoo> { new Foo(), new Foo() }; IQueryable myFilteredFoos = null; mockObject.Setup(m => m.GetByFilter(It.IsAny<IFilter>())) .Callback( (IFilter filter) => myFilteredFoos = filter.FilterCollection(objects)) .Returns(myFilteredFoos.Cast<IFooBar>()); 抛出异常,因为myFilteredFoos在Cast<IFooBar>()调用期间为null 。这不是我期望的那样吗?我认为FilterCollection会被调用,然后myFilteredFoos将为非null并允许强制转换。 FilterCollection不能返回空值,这使我得出不被调用的结论。另外,当我这样声明时myFilteredFoos: Queryable myFilteredFoos; Return调用抱怨myFilteredFoos可能在初始化之前使用。
69 c#  .net  unit-testing  mocking  moq 

2
在元素中将属性序列化为Xml属性
我有以下课程: [Serializable] public class SomeModel { [XmlElement("SomeStringElementName")] public string SomeString { get; set; } [XmlElement("SomeInfoElementName")] public int SomeInfo { get; set; } } (当填充一些测试数据时)并使用XmlSerializer.Serialize()进行序列化会导致以下XML: <SomeModel> <SomeStringElementName>testData</SomeStringElementName> <SomeInfoElementName>5</SomeInfoElementName> </SomeModel> 我需要拥有的是: <SomeModel> <SomeStringElementName Value="testData" /> <SomeInfoElementName Value="5" /> </SomeModel> 有没有办法在不编写自己的自定义序列化代码的情况下将其指定为属性?

17
GDI +的Bitmap.Save方法中发生一般错误
我正在上载该图像的缩略图副本并将其保存在缩略图文件夹中。 我正在使用以下链接: http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx 但 newBMP.Save(directory + "tn_" + filename); 导致异常“ GDI +中发生一般错误”。 我尝试授予文件夹权限,还尝试在保存时使用新的单独bmp对象。 编辑: protected void ResizeAndSave(PropBannerImage objPropBannerImage) { // Create a bitmap of the content of the fileUpload control in memory Bitmap originalBMP = new Bitmap(fuImage.FileContent); // Calculate the new image dimensions int origWidth = originalBMP.Width; int origHeight = …

4
将XML转换为动态C#对象
我已使用以下C#代码使用JSON.Net框架将JSON数据字符串转换为动态对象: // Creates a dynamic .Net object representing the JSON data var ProductDB = JsonConvert.DeserializeObject<dynamic>(JsonData); 转换后,我可以使用如下代码直接访问元素: // Variables to be used string ProductID; string ProductType; int ProductQty; // Loop through each of the products foreach (dynamic product in ProductDB.products) { ProductID = product.id; ProductType = product.type; ProductQty = product.qty; } …
69 c#  xml  json 

8
为什么要使用Enumerable.ElementAt()而不是[]运算符?
这似乎是一个愚蠢的问题,但我还没有找到答案,所以就在这里。:) 在这两种情况下,如果您无法检查集合的边界,都会收到“超出范围”的异常。这仅仅是编码风格的偏好吗? 如果有人需要一个例子: List<byte> myList = new List<byte>(){0x01, 0x02, 0x03}; byte testByte = myList.ElementAt(2); 与 byte testByte = myList[2];
69 c# 

6
内置.Net算法,可将值四舍五入到最接近的10个间隔
如何在C#中将任何值四舍五入到10个间隔?例如,如果我有11,我希望它返回10,如果我有136,那么我希望它返回140。 我可以轻松地手工完成 return ((int)(number / 10)) * 10; 但是我正在寻找一种内置算法来完成这项工作,例如Math.Round()。我不愿意手工做的原因是,我不想为我的整个项目编写相同或相似的代码,即使是像上面这样简单的事情也是如此。
69 c#  math  rounding 

8
仅从字符串返回数字0-9
我需要一个可以在VBScript和.NET中使用的正则表达式,该正则表达式将仅返回在字符串中找到的数字。 例如,以下任何“字符串”应仅返回1231231234 1231231234 (123)123-1234 123-123-1234 (123)123-1234 123.123.1234 1231231234 1 2 3 1 2 3 1 2 3 4 这将在电子邮件解析器中用于查找客户可能在电子邮件中提供的电话号码并进行数据库搜索。 我可能错过了类似的正则表达式,但是我确实在regexlib.com上进行了搜索。 [编辑]-在设置musicfreak的答案后添加了RegexBuddy生成的代码 VBScript代码 Dim myRegExp, ResultString Set myRegExp = New RegExp myRegExp.Global = True myRegExp.Pattern = "[^\d]" ResultString = myRegExp.Replace(SubjectString, "") VB.NET Dim ResultString As String Try Dim RegexObj As New …

5
2个数字之间的差异
我需要完美的算法或C#函数来计算2个十进制数字之间的差异(距离)。 例如: 100和25之间的差是75 100,而-25是125 -100,-115是15 -500,而100是600 是否有C#函数或非常优雅的算法来计算此值,否则我必须分别使用if s处理每种情况。 如果有这样的功能或算法,那是哪一个呢?
69 c#  algorithm  math  numbers 

2
如何使用BackgroundWorker?
我知道它有3种方法。在我的程序中,我有一种发送消息的方法。这通常很晚,并且程序有时根本不发送响应按钮按下的消息。有时可能比我期望的要晚5秒钟,程序会冻结。我想使用aBackgroundWorker按预期方式发送消息,并允许程序始终正常运行。我有用于在按钮处理程序中发送消息的代码。现在,我将这些等效代码放在哪里?我希望所有这些仍然可以通过按按钮来处理。 这是合适的处理程序吗? backgroundWorker1.RunWorkerAsync(); 并在: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {} 我要把我的代码放在按钮处理程序中吗?而这之前: carga.progressBar1.Minimum = 0; carga.progressBar1.Maximum = 100; Carga是ProgressBar所在的另一种形式。在这种情况下如何使用BackgroundWorker?

10
C#是否具有IsNullOrEmpty用于List / IEnumerable?
我知道通常空列表比NULL更可取。但是我将返回NULL,主要有两个原因 我必须显式检查并处理null值,以避免bug和攻击。 ??之后很容易执行操作以获得返回值。 对于字符串,我们有IsNullOrEmpty。有什么从C#本身做了列表或IEnumerable的同样的事情?

11
如何设置环境名称(IHostingEnvironment.EnvironmentName)?
默认的ASP.NET Core Web项目包含以下行Startup.cs: if (string.Equals(env.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase)) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(ErrorPageOptions.ShowAll); } else { app.UseExceptionHandler("/Home/Error"); } As I understand, the EnvironmentName is a new way to handle Dev/Production environment. But it doesn't changes on Release build configuration. So what is the way to set a different EnvironmentName? I can imagine that …

13
如何使用C#读取Excel文件的数据?
如何使用C#读取Excel文件?我打开一个Excel文件进​​行阅读,然后将其复制到剪贴板以搜索电子邮件格式,但是我不知道该怎么做。 FileInfo finfo; Excel.ApplicationClass ExcelObj = new Excel.ApplicationClass(); ExcelObj.Visible = false; Excel.Workbook theWorkbook; Excel.Worksheet worksheet; if (listView1.Items.Count > 0) { foreach (ListViewItem s in listView1.Items) { finfo = new FileInfo(s.Text); if (finfo.Extension == ".xls" || finfo.Extension == ".xlsx" || finfo.Extension == ".xlt" || finfo.Extension == ".xlsm" || finfo.Extension == ".csv") …
68 c#  excel 


3
图像与位图类
我很难理解Image班级与Bitmap班级之间的差异。现在,我知道Bitmap继承自,Image但据我了解两者都非常相似。谁能对此有所启发?
68 c#  .net  image  bitmap 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.