【问题标题】:My C# code is getting errors我的 C# 代码出现错误
【发布时间】:2015-07-25 21:28:28
【问题描述】:

我是 C# 新手,我在网上找到了这个脚本并尝试将它应用到我的项目中,但我遇到了错误,不知道如何修复它... ** ** 部分是红色卷曲下划线(错误)让我头疼的地方。
我怀疑using 部分缺少什么不是吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;

namespace cpuinfo
{
    public class Class1
    {
        public static int getMaxCPUFreqMHz()
        {

            int maxFreq = -1;
            try
            {

                **RandomAccessFile** reader = new **RandomAccessFile**("/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state", "r");

                bool done = false;
                while (!done)
                {
                    String line = reader.readLine();
                    if (null == line)
                    {
                        done = true;
                        break;
                    }
                    String[] splits = **line.Split("\\s+")**;
                    **Assert**(splits.Length == 2);
                    int timeInState = **Integer**.parseInt(splits[1]);
                    if (timeInState > 0)
                    {
                        int freq = **Integer**.parseInt(splits[0]) / 1000;
                        if (freq > maxFreq)
                        {
                            maxFreq = freq;
                        }
                    }
                }

            }
            catch (IOException ex)
            {
                ex.**printStackTrace**();
            }

            return maxFreq;
        }
    }
}

【问题讨论】:

  • 您确定您找到的代码是针对 C# 的吗?同时发布错误。我猜它找不到RandomAccessFileAssertIntegerprintStackTrace,因为它来自Java。
  • 这段代码来自哪里?
  • 只是补充一点:即使您修复了编译错误,这也行不通。您正在尝试从 .NET 环境访问 linux 文件 (/sys/device/...)。您不能将 Java 和 C#、Windows 和 Linux 混为一谈——您需要多考虑一下您想要实现的目标。也许你正在寻找这个:stackoverflow.com/questions/6923763/…
  • @darkfang 一开始你为什么认为它是 C#?它是完整的 android Java。

标签: c# android visual-studio


【解决方案1】:

您似乎想在这里混合使用 Java 和 C# - Integer.parseInt() 看起来很像 Java,但命名空间和使用非常类似于 C#。

我认为这可能是您问题的根本原因。

【讨论】:

  • 与命名约定等相同。这就像一个混合物,因为.Split(...).Length 更像是 C#,在 Java 中它们可能像 .split(...).getLength() 或 @ 987654327@
  • @Bauss 由于我使用的是 C#,是否可以用 C# 替换 Java 函数而不会对我的代码产生任何影响?
  • @Bauss 我将如何重写它?你能帮我修好吗?我已经挣扎了好几天...
  • 你能具体告诉我你想做什么吗,也许我能想出一些办法
  • @Bauss 基本上它从 android 设备上的文件中获取 cpu 信息。这是我复制代码的地方-> android.stackexchange.com/questions/19810/…
【解决方案2】:

您似乎使用 Java JDK 中的类。在 C# 中,我们不使用 Assert、Integer 或 RandomAccessFile

你应该重写你的代码。将 Assert 更改为 Debug.Assert 或 Trace.Assert。将 Integer.parseInt 更改为 Convert.ToInt32。而且我真的不知道 RandomAccessFile 是什么。我猜你正在从文件中读取内容。你应该

using (StreamReader reader = new StreamReader ("/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state") {

                bool done = false;
                while (!done)
                {
                    String line = reader.ReadLine();
                    if (null == line)
                    {
                        done = true;
                        break;
                    }
                    String[] splits = line.Split('\\', 's', '+');
                    Debug.Assert (splits.Length == 2);
                    int timeInState = Convert.ToInt32 (splits[1]);
                    if (timeInState > 0)
                    {
                        int freq = Convert.ToInt32(splits[0]) / 1000;
                        if (freq > maxFreq)
                        {
                            maxFreq = freq;
                        }
                    }
                }
}

只需将try{} 中的所有内容更改为上面的代码就可以了。

另外,printStackTrace 不在 .NET Framework 中。你应该使用Console.WriteLine (ex.StackTrace)

如果您还有任何错误,请告诉我。

【讨论】:

  • 仍然有一些红色的卷曲下划线,它显示The name 'done/reader' does not exist in the current context截图 -> prntscr.com/7wu04w
  • 您是否忘记为using 语句添加花括号?我无法重现错误。 @darkfang
  • 是的,我确实放了花括号!在bool done = false; 行,我收到 2 个错误,1) Invalid expression term 'bool' 2) Syntax error, ',' expected; 。 @Sweeper
  • 尝试按照错误的说法,将;更改为,我猜是因为这太奇怪了。我想我现在帮不了你。尝试询问其他人。
  • 哦!我懂了!您忘记为using 语句添加右括号^v^ 现在一切都已修复!顺便说一句@Sweeper
猜你喜欢
  • 2023-02-21
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 2018-08-01
相关资源
最近更新 更多