将“ 1.5TB”,“ 500MB”转换为分成单个文件大小 [英] Convert "1.5TB", "500MB" into a single unit of file size

查看:74
本文介绍了将“ 1.5TB”,“ 500MB”转换为分成单个文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户使用任何标准后缀(例如TB,MB,GB)输入文件大小

I want to allow a user to enter a file size, using any of the standard suffixes (such as TB, MB, GB)

我想获得值的方式可以将它们与文件夹大小进行比较。

I'd like to get the value in a way that i can compare them to a folder size.

想法是要有一个程序,如果文件夹超过某个大小会发出警告。

The idea is to have a program that'll warn if a folder gets above a certain size, with the size dictated by a user-inputted string.

.net框架中是否内置任何内容,可让我解析诸如之类的字符串? 1.5TB 400GB 1.9GB 0.5KB

Is there anything built into the .net framework that allows me to parse strings such as 1.5TB, 400GB, 1.9GB and 0.5KB ?

推荐答案

对于简单的解释器

像这样的代码是一个简单的开始,您可能需要处理更多情况,并考虑大小写差异( Gb GB )。

Code like this is a simple start, you will need to handle perhaps more cases, and account for differences in casing (Gb vs GB for example).

从上下文定义和表达式开始:

You start with a definition for a context and an Expression:

public class FileSizeContext
{
    private string input;
    private long output;

    public FileSizeContext(string input)
    {
        this.Input = input;
    }

    public string Input { get; set; }

    public long Output { get; set; }
}

public abstract class FileSizeExpression
{
    public abstract void Interpret(FileSizeContext value);
}

然后,您定义终端表达式以及所有变体:

Then you define your terminal expression,a nd all of the variants:

public abstract class TerminalFileSizeExpression : FileSizeExpression
{
    public override void Interpret(FileSizeContext value)
    {
        if(value.Input.EndsWith(this.ThisPattern()))
        {
            double amount = double.Parse(value.Input.Replace(this.ThisPattern(),String.Empty));
            var fileSize = (long)(amount*1024);
            value.Input = String.Format("{0}{1}",fileSize,this.NextPattern());
            value.Output = fileSize;
        }
    }
    protected abstract string ThisPattern();
    protected abstract string NextPattern();
}

public class KbFileSizeExpression : TerminalFileSizeExpression
{
    protected override string ThisPattern(){return "KB";}
    protected override string NextPattern() { return "bytes"; }
}
public class MbFileSizeExpression : TerminalFileSizeExpression
{
    protected override string ThisPattern() { return "MB"; }
    protected override string NextPattern() { return "KB"; }
}
public class GbFileSizeExpression : TerminalFileSizeExpression
{
    protected override string ThisPattern() { return "GB"; }
    protected override string NextPattern() { return "MB"; }
}
public class TbFileSizeExpression : TerminalFileSizeExpression
{
    protected override string ThisPattern() { return "TB"; }
    protected override string NextPattern() { return "GB"; }
}

然后,您添加一个非终结符表达式(工作):

Then you add a non-terminal expression (this does the bulk of the work):

public class FileSizeParser : FileSizeExpression
{
    private List<FileSizeExpression> expressionTree = new List<FileSizeExpression>()
                                                  {
                                                      new TbFileSizeExpression(),
                                                      new GbFileSizeExpression(),
                                                      new MbFileSizeExpression(),
                                                      new KbFileSizeExpression()
                                                  };

    public override void Interpret(FileSizeContext value)
    {
        foreach (FileSizeExpression exp in expressionTree)
        {
            exp.Interpret(value);
        }
    }
}

最后,这是一种客户端代码:

Finally, here is the sort of client code:

var ctx = new FileSizeContext("10Mb");
var parser = new FileSizeParser();
parser.Interpret(ctx);
Console.WriteLine("{0} bytes", ctx.Output); // 10485760 bytes

实时示例: http://rextester.com/rundotnet?code=WMGOQ13650

编辑。从Mb更改为MB(一个正式为MegaByte,另一个为MegaBit)。将int更改为long以解决大尺寸问题。

Edits. Changed to MB from Mb (one is officially MegaByte other is MegaBit). Changed int to long to account for large sizes.

这篇关于将“ 1.5TB”,“ 500MB”转换为分成单个文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆