小编Mil*_*lne的帖子

替代if,否则if

我有很多if,else if语句,我知道必须有更好的方法来做到这一点,但即使在搜索stackoverflow之后,我也不确定如何在我的特定情况下这样做.

我正在解析文本文件(账单)并根据账单上是否出现某些字符串将服务提供商的名称分配给变量(txtvar.Provider).

这是我正在做的一小部分样本(不要笑,我知道它很乱).总而言之,如果是,那么大约有300个.

if (txtvar.BillText.IndexOf("SWGAS.COM") > -1)
{
    txtvar.Provider = "Southwest Gas";
}
else if (txtvar.BillText.IndexOf("georgiapower.com") > -1)
{
    txtvar.Provider = "Georgia Power";
}
else if (txtvar.BillText.IndexOf("City of Austin") > -1)
{
    txtvar.Provider = "City of Austin";
}
// And so forth for many different strings
Run Code Online (Sandbox Code Playgroud)

我想使用类似switch语句的东西更高效和可读,但我不确定如何比较BillText.我正在寻找这样的东西,但无法弄清楚如何使它工作.

switch (txtvar.BillText)
{
    case txtvar.BillText.IndexOf("Southwest Gas") > -1:
        txtvar.Provider = "Southwest Gas";
        break;
    case txtvar.BillText.IndexOf("TexasGas.com") > -1:
        txtvar.Provider = "Texas Gas";
        break;
    case txtvar.BillText.IndexOf("Southern") > -1:
        txtvar.Provider = "Southern Power & Gas"; …
Run Code Online (Sandbox Code Playgroud)

c# performance dictionary if-statement loop-unrolling

15
推荐指数
4
解决办法
1万
查看次数

File.Copy()如果​​文件可能已存在则性能

我有一个项目,每5分钟作为一个计划任务运行.除此之外,该项目还运行数百个图像,并以这种方式将它们复制到网络驱动器中.

foreach (string file in Files)
{
    string Control = Path.GetFileNameWithoutExtension(file);
        File.SetAttributes(file, FileAttributes.Normal);
        try
        {
            File.Copy(file, destinationFolder + "\\" + Control + @".pdf", false);
        }
        catch (Exception err)
        {
            Console.Writeline(err.ToString());
        }
}
Run Code Online (Sandbox Code Playgroud)

"false"参数当然告诉它不要覆盖文件,如果它已经存在的话.

这比首先检查文件是否已经存在更快/更好的做法,然后只在文件不存在时才复制?(见下文)

foreach (string file in Files)
{
    if (File.Exists(destinationFolder + "\\" + ControlNumber + ".pdf") == false)
    {
        File.SetAttributes(file, FileAttributes.Normal);
        File.Copy(file, destinationFolder + "\\" + ControlNumber + @".pdf");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的直觉告诉我,第一种是更好的方式.但是,我对编程比较陌生,并且想知道哪个更好,更快,更广泛接受等等.

谢谢!

编辑:知道我复制的远程驱动器/文件夹包含4TB的图像数据(数百万图像)可能有用也可能没有帮助

c# performance file-io copy file

5
推荐指数
1
解决办法
3455
查看次数

一个解决方案中的多个exe文件,都引用单个公共类

我有一个越来越大的#console项目(vs2012).目前我一个接一个地调用多个'方法'.当我添加更多方法时,我希望能够将它们分解为多个可执行文件,以便按预定应用程序运行,但仍然能够引用(并使用tortoise SVN更改)一个公共类规则,它们都将使用参考.我是否必须创建一个dll才能实现此目的,还是有更简单的方法?对不起,我是所有这一切的新手.我发现了类似的问题,但我要么不理解它们,要么它们不是我想要的.谢谢!

.net c# dll visual-studio

4
推荐指数
1
解决办法
2094
查看次数

如何在没有pha​​r支持的情况下在PHP 5.2.8上安装symfony(Composer依赖)?

我一直试图在我的网站上安装symfony但是我没有取得什么成功,因为它取决于作曲家.

安装:

php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
Run Code Online (Sandbox Code Playgroud)

当我尝试安装时(所有安装选项似乎都需要phar支持)我得到了这个输出:

-----------------------------------------------------------
Warning: Unexpected character in input:  '\' (ASCII=92) state=1 in Command line code(1) : eval()'d code on line 378
#!/usr/bin/env php
Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:

The phar extension is missing.
Install it or recompile php without --disable-phar

Your PHP (5.2.8) is too old, you must upgrade to PHP 5.3.2 or higher.
-----------------------------------------------------------

我没有权限安装不同版本的PHP,因为我使用的付费虚拟主机有点限制.我有什么选择?

php install phar symfony

-1
推荐指数
1
解决办法
1444
查看次数