相关疑难解决方法(0)

如何使用公共函数从字节返回KB,MB和GB

我正在编写一个返回文件大小的"函数"(以B,KB,MB,GB为单位).

VB.Net代码总是首先以字节为单位获取大小,因此当文件大小(以字节为单位)小于100时,它返回B,如果> 1000,则将其除以1000,然后返回KB.但是当它应该是MB时,我尝试除以1000000,它返回的大小总是比它应该的大2 MB!

有人可以告诉我我做错了什么!

我的文件大小是(15,570,550字节).. ..是...(14.8 MB)

所以当我通过这个函数运行它时它返回16MB!

代码

Public Function GetFileSize(ByVal TheFile As String, _
                            Optional ByVal ShowSizeType As Boolean = False) As String
    If TheFile.Length = 0 Then Return ""
    If Not System.IO.File.Exists(TheFile) Then Return ""
    '---
    Dim TheSize As Integer = My.Computer.FileSystem.GetFileInfo(TheFile).Length
    Dim SizeType As String = ""
    '---
    If TheSize < 1000 Then
        SizeType = "B"
    Else
        If TheSize < 1000000000 Then
            If TheSize < 1000000 Then
                SizeType = "KB"
                TheSize = TheSize / …
Run Code Online (Sandbox Code Playgroud)

vb.net .net-4.0 visual-studio-2010

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

我可以在switch语句中使用变量吗?

我正在编写基于文本的冒险,并且遇到了问题.我正在尝试创建一个switch语句案例来处理您想要的每个考试操作,并且到目前为止我的代码正在获取此代码:

case "examine" + string x:
    //this is a method that I made that makes sure that it is an object in the area
    bool iseobj = tut.Check(x);
    if (iseobj)
        x.examine();
    else
        Console.WriteLine("That isn't an object to examine");
    break;
Run Code Online (Sandbox Code Playgroud)

如何在case语句中使用变量?我想要任何以"examine"+(x)开头的字符串来触发这个案例.

.net c# console-application switch-statement

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