Tag: C#的

文件大小的格式提供者

有没有简单的方法来创build一个使用IFormatProvider的类,写出一个用户友好的文件大小? public static string GetFileSizeString(string filePath) { FileInfo info = new FileInfo(@"c:\windows\notepad.exe"); long size = info.Length; string sizeString = size.ToString(FileSizeFormatProvider); // This is where the class does its magic… } 它应该导致string格式化如“ 2,5 MB ”,“ 3,9 GB ”,“ 670字节 ”等等。

如何改变整个项目的命名空间?

我正在从这篇文章修改演示应用程序: http : //msdn.microsoft.com/en-us/magazine/dd419663.aspx 我需要更新所有的文件来使用我的命名空间,例如现在的文件位于这里: MySolution\MyApp\DemoApp\ViewModel\MainWindowViewModel.cs 正在使用这样的命名空间: namespace DemoApp.ViewModel { /// <summary> /// The ViewModel for the application's main window. /// </summary> public class MainWindowViewModel : WorkspaceViewModel 我需要在这里移动文件(删除DemoApp文件夹): MySolution\MyApp\ViewModel\MainWindowViewModel.cs 还要使用正确的命名空间: namespace MyApp.ViewModel { …. 如何在Visual Studio 2010中做到这一点? 更新确定这里是可能的重复在Visual Studio中更改项目名称空间现在我知道如何更改项目的命名空间,但如何移动文件系统上的文件? (摆脱“DemoApp”文件夹)

如果我想将一个无符号整数舍入到最接近的小整数或相等的偶数整数,我可以除以2然后乘以2?

例如 : f(8)=8 f(9)=8 我可以做x = x/2*2; ? 编译器是否有优化这种expression的风险?

如何将“0”和“1”转换为false和true

我有一个通过Odbc连接到数据库的方法。 我调用的存储过程有一个返回值,从数据库端是一个“字符”。 现在我把这个返回值作为一个string来抓取,并在简单的if语句中使用它。 我真的不喜欢比较像这样的string的想法,只有两个值可以从数据库中返回0和1。 OdbcCommand fetchCommand = new OdbcCommand(storedProc, conn); fetchCommand.CommandType = CommandType.StoredProcedure; fetchCommand.Parameters.AddWithValue("@column ", myCustomParameter); fetchCommand.Parameters.Add("@myReturnValue", OdbcType.Char, 1) .Direction = ParameterDirection.Output; fetchCommand.ExecuteNonQuery(); string returnValue = fetchCommand.Parameters["@myReturnValue"].Value.ToString(); if (returnValue == "1") { return true; } 处理这种情况的正确方法是什么? 我试过'Convert.ToBoolean()'这似乎是明显的答案,但我跑到'string不被识别为有效的布尔值。 被抛出的exception 我在这里错过了什么,还是有另外一种方法让“1”和“0”的行为如同真假? 谢谢!

绑定在代码隐藏中定义的对象

我有一些在代码后面实例化的对象,例如,XAML被称为window.xaml,在window.xaml.cs中 protected Dictionary<string, myClass> myDictionary; 我怎样才能将这个对象绑定到,例如,列表视图,只使用XAML标记? 更新: (这正是我在我的testing代码中): <Window x:Class="QuizBee.Host.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="{Binding windowname}" Height="300" Width="300" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> </Grid> </Window> 并在代码隐藏 public partial class Window1 : Window { public const string windowname = "ABCDEFG"; public Window1() { InitializeComponent(); } } 假设标题应该成为“ABCDEFG”对吗? 但最终什么也没有显示。

在IIS 7.5中托pipe的Web Api中找不到HTTP 404页面

我有一个Web Api应用程序。 当我使用VS 2010debugging开发服务器进行testing时,它工作得非常好。 但是我现在将它部署到IIS 7.5,并且在尝试访问应用程序时遇到HTTP 404错误。 这是我的web.config <?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-FlowGearProxy-20123141219;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add key="webpages:Enabled" value="true" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" /> […]

复杂的C声明

我只是通过互联网上的一些代码,发现这一点: float * (*(*foo())[SIZE][SIZE])() 我如何阅读这份声明? 阅读这种复杂的声明是否有一套特定的规则?

为什么不能在编译时解决运行时多态性?

考虑: #include<iostream> using namespace std; class Base { public: virtual void show() { cout<<" In Base \n"; } }; class Derived: public Base { public: void show() { cout<<"In Derived \n"; } }; int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; } 为什么这段代码会导致运行时多态性,为什么不能在编译时解决呢?

如何将数组的内容复制到C ++中的std :: vector而不循环?

我有一个值的数组,从我需要存储以供日后处理的程序的不同部分传递给我的函数。 由于我不知道在处理数据之前我的函数会被调用多less次,所以我需要一个dynamic的存储结构,所以我select了一个std::vector 。 我不想做标准循环push_back单独的所有值,这将是很好,如果我可以使用类似于memcpy全部复制它。

使用.NET在目录中删除3个月以前的文件

我想知道(使用C#)如何删除超过3个月的特定目录中的文件,但我想date可以灵活。 只是要清楚:我正在查找超过90天的文件,换句话说,应该保留90天以前创build的文件,删除所有其他文件。