小编Yuk*_*uya的帖子

C#以%为单位获取已用内存

我已经创建了一个性能计数器,它可以检查%的总内存使用量,但问题是它没有给我与任务管理器显示的相同的值.例如:我的程序说34%,但任务经理说40%.

有任何想法吗?

注意
我尝试获取系统的可用RAM,而不是进程使用的RAM.

现行守则

private PerformanceCounter performanceCounterRAM = new PerformanceCounter();

performanceCounterRAM.CounterName = "% Committed Bytes In Use";
performanceCounterRAM.CategoryName = "Memory";

progressBarRAM.Value = (int)(performanceCounterRAM.NextValue());
            labelRAM.Text = "RAM: " + progressBarRAM.Value.ToString(CultureInfo.InvariantCulture) + "%";
Run Code Online (Sandbox Code Playgroud)

编辑
我用计时器每秒刷新进度条和标签.

c# performancecounter

41
推荐指数
3
解决办法
5万
查看次数

检查端口是否打开

我似乎无法找到任何告诉我路由器中的端口是否打开的东西.这甚至可能吗?

我现在的代码似乎并没有真正起作用......

private void ScanPort()
{
    string hostname = "localhost";
    int portno = 9081;
    IPAddress ipa = (IPAddress) Dns.GetHostAddresses(hostname)[0];
    try
    {
        System.Net.Sockets.Socket sock =
                new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                                              System.Net.Sockets.SocketType.Stream,
                                              System.Net.Sockets.ProtocolType.Tcp);
        sock.Connect(ipa, portno);
        if (sock.Connected == true) // Port is in use and connection is successful
            MessageBox.Show("Port is Closed");
        sock.Close();
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        if (ex.ErrorCode == 10061) // Port is unused and could not establish connection 
            MessageBox.Show("Port is Open!");
        else
            MessageBox.Show(ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# winforms

23
推荐指数
2
解决办法
6万
查看次数

解析Json.NET:"意外的令牌:StartObject"

我正在解析JSON,我收到以下错误:

我正在使用Newtonsoft.Json.NET DLL.

读取字符串出错.意外的令牌:StartObject.路径'[0]',第1行,第2位.

这是我的代码:

public static List<string> GetPluginByCategory(string category)
    {
        var wc = new WebClient();
        var json = wc.DownloadString("http://api.bukget.org/api2/bukkit/category/" + category);
        var list = JsonConvert.DeserializeObject<List<string>>(json);
        return list;
    }
Run Code Online (Sandbox Code Playgroud)

category可以是以下字符串之一:

["管理工具","反悲伤工具","聊天相关","开发者工具","经济","修复","乐趣","一般","信息","力学","杂项" ,"角色扮演","传送","网站管理","世界编辑与管理","世界发电机"

编辑:这是我得到的回应:

 [{"description": "Stop users swearing\n", "name": "a5h73y", "plugname": "NoSwear"}, {"description": "Be sure that your server rules are read and accepted!", "name": "acceptdarules", "plugname": "AcceptDaRules"}]
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么它不起作用?以前用过:/.

c# json json.net

10
推荐指数
1
解决办法
6万
查看次数

重置表单中的所有项目

我想知道,有没有办法可以将所有复选框,文本框,数字和其他控件重置为默认值,而无需单独为每个控件编写代码?这是我尝试过的代码,但似乎不起作用:

for (int i = 0; i < this.Controls.Count; i++)
{
    this.Controls[i].ResetText();
}
Run Code Online (Sandbox Code Playgroud)

编辑:
我已通过手动设置控制值修复它,抱歉所有的麻烦>.<.

c# reset winforms

10
推荐指数
2
解决办法
11万
查看次数

C#定时器计数器,采用xx.xx.xx格式

我有一个计数器,每1秒钟计数一次,并在int中加1.

问题
如何格式化我的字符串,以便计数器看起来像这样:

00:01:23

代替:

123

我尝试过的
事情到目前为止我尝试过的事情:

for (int i = 0; i < 1; i++)
        {
            _Counter += 1;
            labelUpTime.Text = _Counter.ToString();
        }
Run Code Online (Sandbox Code Playgroud)

我的计时器的间隔设置为:1000(因此它每秒加1).
我确实读过有关string.Format("")的内容,但我不知道它是否适用.
谢谢,如果你能引导我完成这个:D!

c# int formatting timer

6
推荐指数
1
解决办法
7279
查看次数

PHP:强制下载标题不会显示总大小和速度

每当我使用这个脚本下载文件时,我都无法看到下载时的总大小和速度......我想让它看起来更像是"直接下载链接".此脚本的目的是隐藏直接下载链接限制直接下载和其他下载行为,如机器人.想想mediafire,rapidshare,megaupload等.

我们现在使用的脚本但是没有显示从正常下载链接下载时的显示方式,我将发布正在发生的事情的屏幕截图:
在此输入图像描述

我希望这个截图有所帮助,因为我已经在互联网上搜索了几个小时,似乎无法找到解决方案:(.

if (isset($_GET['file'])){
   $file = $_GET['file'];
   $path = '/home/user/domains/domain.com/files/upload/';
   $filepath = $path.$file;

   if (file_exists($filepath)){

    set_time_limit(0); // for slow connections

    header('Content-Description: File Transfer');
    header("Content-Disposition: attachment; filename=\"$file\"");
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($filepath));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Expires: 0');

    readfile($filepath); // send file to client 
   } 
   else{
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404); 
   }
  }else{
   header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404); 
  }
Run Code Online (Sandbox Code Playgroud)

php download

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

HTML Agility Pack获取所有输入字段

我在互联网上找到了一些找到所有href标签并将其更改为google.com的代码,但是如何告诉代码找到所有input字段并将自定义文本放在那里?

这是我现在的代码:

HtmlDocument doc = new HtmlDocument();
doc.Load(path);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
    HtmlAttribute att = link.Attributes["href"];
    att.Value = "http://www.google.com";
}
doc.Save("file.htm");
Run Code Online (Sandbox Code Playgroud)

请,有人可以帮助我,我似乎无法在互联网上找到任何有关这方面的信息:(.

c# html-agility-pack

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

过滤Listview中的项目

我试图ListView通过使用a 来过滤a中的项目TextBox.
我已经设法制作了一些东西,但它只能从我的列表视图中删除项目,而不是将它们带回来.这是我的代码的一个小例子:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string value = textBox1.Text.ToLower();
    for (int i = listView1.Items.Count - 1; -1 < i; i--)
    {
        if
        (listView1.Items[i].Text.ToLower().StartsWith(value) == false)
        {
            listView1.Items[i].Remove();
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何检索已删除的项目?我似乎无法弄明白>:...

c# listview winforms

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

C#从try-catch块返回

为什么我不能从try块中返回我从url获得的图像?对不起英语不好:(.

这是我得到的错误:

退货声明丢失

    public static Image GetExternalImg(string name, string size)
    {
        try
        {
            // Get the image from the web.
            WebRequest req = WebRequest.Create(String.Format("http://www.picturesite.com/picture.png", name, size));
            // Read the image that we get in a stream.
            Stream stream = req.GetResponse().GetResponseStream();
            // Save the image from the stream that we are rreading.
            Image img = Image.FromStream(stream);
            // Save the image to local storage.
            img.Save(Environment.CurrentDirectory + "\\images\\" + name + ".png");
            return img;
        }
        catch (Exception)
        {

        }
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,因为我现在卡住了:(.

c# return try-catch

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

如果陈述看起来更好

如何让这个看起来更好看:

lblTotalWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem) * 1024 + " kb";    // Total world size
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem))                  // World itself
{
    lblWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem) * 1024 + " kb";
}
else
{
    lblWorldSize.Text = "Couldn't find world.";
}
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem + "_nether"))      // Nether
{
    lblNetherSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem + "_nether") * 1024 + " kb";
} …
Run Code Online (Sandbox Code Playgroud)

c# if-statement

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