Tag: C#的

如何获取某个元素在string向量中的位置,将其用作int向量中的索引?

我想获得一个strings的向量中的元素的索引,使用它作为另一个inttypes的向量中的索引,这可能吗? 例: vector <string> Names; vector <int> Numbers; … // condition to check whether the name exists or not if((find(Names.begin(), Names.end(), old_name_)) != Names.end()) { // if yes cout <<"Enter the new name."<< endl; cin >> name; replace(Names.begin(), Names.end(), old_name_, name); } 现在我想要获取Names向量中old_name的位置,以便在访问Numbers向量中的某个元素时使用它。 所以我可以说: Numbers[position] = 3 ; // or whatever value assigned here. 我试过使用: […]

aspx页面redirect到一个新的页面

使用ASPX页面将浏览器redirect到新页面所需的代码是什么? 我已经在我的页面上试过这个default.aspx: <% Response.Redirect("new.aspx", true); %> 要么 <%@ Response.Redirect("new.aspx", true); %> 而这些导致服务器错误是未定的。 我看不到错误代码; 因为服务器不在我的控制之下,错误不公开。 请提供页面第1行所有必要的代码到最后,我真的很感激。

尝试/最终在C#中的开销?

我们已经看到很多关于何时以及为什么要使用try / catch和try / catch / finally 。 而且我知道肯定有一个try / finally的用例(特别是因为这是using语句的实现方式)。 我们也看到了关于try / catch和exception的开销问题。 然而,我所链接的问题并没有提到只是试试最后的开销。 假设try块中没有发生任何exception,那么确保finally语句在离开try块时执行的开销是多less(有时是从函数返回的)? 再一次,我只是想要try / finally ,没有catch ,没有抛出exception。 谢谢! 编辑:好吧,我会试图让我的使用案例更好一点。 我应该使用哪个DoWithTryFinally或DoWithoutTryFinally ? public bool DoWithTryFinally() { this.IsBusy = true; try { if (DoLongCheckThatWillNotThrowException()) { this.DebugLogSuccess(); return true; } else { this.ErrorLogFailure(); return false; } } finally { this.IsBusy = false; } } […]

如何检查数据表中是否存在列

我有一个csv文件的内容生成一个datable。 我使用其他信息将csv的某些列(现在在数据表中)映射到用户需要填写的信息。 在最好的世界里映射将是可能的。 但是这不是现实…所以在我尝试映射数据列的值之前,我需要检查该列是否存在。 如果我不做这个检查,我有一个ArgumentException。 当然,我可以用这样的代码来检查: try { //try to map here. } catch (ArgumentException) { } 但我现在有3列地图和一些或全部可能存在/缺失 有没有好的方法来检查一个数据表中是否存在一列?

命名空间或大会?

我在名称空间和程序集之间感到非常困惑。 是System.Data和System.Web命名空间或组件? 我注意到这些被称为命名空间,同时它们存在于GAC_32文件夹中。 那么他们究竟是什么?

将数组转换为向量最简单的方法是什么?

将数组转换为向量最简单的方法是什么? void test(vector<int> _array) { … } int x[3]={1, 2, 3}; test(x); // Syntax error. 我想以最简单的方式将int数组中的x转换为向量。

如何获取网页的内容并将其保存到stringvariables中

如何使用ASP.NET获取网页的内容? 我需要编写一个程序来获取网页的HTML并将其存储到一个stringvariables中。

Environment.GetFolderPath(… CommonApplicationData)仍然返回Vista上的“C:\ Documents and Settings \”

据我了解,你应该使用Environment.GetFolderPath方法,所以你有独立于操作系统的代码… Windows XP使用C:\Documents and Settings\ 。 Windows Vista使用C:\ProgramData和C:\Users 。 我在Windows Vista计算机上使用下面的代码,它返回一个C:\Documents and Settings\目录,而不是C:\ProgramData就像它应该…任何想法? string commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); try { File.CreateText( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\mycompany\\uid"); log.Debug("Created file successfully"); } catch (Exception ex) { log.Error("Unable to create the uid file: ", ex); }

从.NET / C#下载图片

我正试图从网站上下载图片。 我使用的代码工作正常,而图像是可用的。 如果图片不可用,则会造成问题。 如何validation图像的可用性? 码: 方法1: WebRequest requestPic = WebRequest.Create(imageUrl); WebResponse responsePic = requestPic.GetResponse(); Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error webImage.Save("D:\\Images\\Book\\" + fileName + ".jpg"); 方法2: WebClient client = new WebClient(); Stream stream = client.OpenRead(imageUrl); bitmap = new Bitmap(stream); // Error : Parameter is not valid. stream.Flush(); stream.Close(); client.dispose(); if (bitmap != null) { bitmap.Save("D:\\Images\\" […]

将文件转换为字节的可靠方法

我在网上find了以下代码: private byte [] StreamFile(string filename) { FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read); // Create a byte array of file stream length byte[] ImageData = new byte[fs.Length]; //Read block of bytes from stream into the byte array fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); return ImageData; //return the byte data } 它是可靠的,足以使用c#中的文件转换为byte [],还是有更好的方法来做到这一点?