C#代码片段

Python爬虫实战:5分钟搞定慢慢买历史价格查询(附完整代码) 本文提供了一份Python爬虫实战指南,教你如何快速编写脚本自动查询慢慢买网站的商品历史价格。文章详细拆解了环境准备、接口分析、核心代码实现及常见问题应对,并附有完整代码,帮助读者将爬虫技术应用于日常比价,实现高效购物决策。 阅读详情

1.将打开的某个form激活

foreach (Form form in Application.OpenForms)
{
  if (form.GetType() == typeof(YourDialogClass)&&form.Text == "TheTitle")
  {
    form.Activate();
    return;
  }
}


2.将float数据类型,整数部分至少保留4位,小数部分至少保留8位,不足位用零补全

float c = 3.1415f;
string s = c.ToString("0000.00000000");

3.如何禁止调整窗口的大小

FormBorderStyle 设为FixedSingle 即可。


4.处理事件

Application.DoEvents();

5.DebugOutputException

public static void DebugOutputException(Exception ex)
        {
#if X_DEBUG
            Debug.WriteLine(ex.ToString() + " " + ex.StackTrace.ToString());
#endif
        }


6.判断目录是否存在

Directory.Exists(string path);


7.创建目录

Directory.CreateDirectory(string path);


8.#region

#region 使您可以在使用 Visual Studio 代码编辑器的大纲显示功能时指定可展开或折叠的代码块。 在较长的代码文件中,能够折叠或隐藏一个或多个区域会十分便利,这样,您可将精力集中于当前处理的文件部分。 下面的示例演示如何定义区域:

#region MyClass definition
public class MyClass 
{
    static void Main() 
    {
    }
}
#endregion

9.将f保留digits位小数, 靠近原点

        /// <summary>
        /// 将f保留digits位小数, 靠近原点
        /// </summary>
        public double CutOffValue(double f, int digits)
        {
            double roundDown = 0.0;
            //截断保留小数位1位
            {
                double n = Math.Pow(10, digits);
                double valuTemp = Math.Abs(f) * n;
                double floorValue = Math.Floor(valuTemp);
                roundDown = floorValue / n;
            }

            if (f < 0)
            {
                roundDown = -roundDown;
            }

            return roundDown;
        }


10.在指定矩形区域居中绘制文本

StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;

g.DrawString(message, font, new SolidBrush(Color.FromArgb(128, Color.Black)), new Rectangle(x, y, width, height), strFormat);


11.获取程序集版本

 
string assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); 


12.获取文件版本

 
using System.Diagnostics; 
FileVersionInfo myFileVersion = FileVersionInfo.GetVersionInfo (System.Windows.Forms.Application.ExecutablePath); 
string fileVersion = myFileVersion.FileVersion; 


13.无符号整数, 十六进制形式显示, 并且指定位数

byte a = 1234;

a.ToString("X6");

string.Format("{0:X8}", a);

X十六进制字符大写, x十六进制字符小写

14.C# 获得当前 进程 或 线程 的ID  

如何获得当前进程的Id用:
Process[] processes = Process.GetProcesses(); 
foreach(Process process in processes) 
{ 
if(process.ProcessName == "进程名" 
{ 
MessageBox.Show(process.Id); 
} 
}
或
Process processes   =Process.GetCurrentProcess
processes.id获得当前进程的ID

--------------------------------------------------------------------------------------
如何获得当前线程的ID用:
Thread.CurrentThread.ManagedThreadId.ToString() (推荐)
或
AppDomain.GetCurrentThreadId().ToString()

手把手教你用VL822-QFN88设计一个支持PD快充的Type-C四口HUB(附原理图要点) 本文详细介绍了基于VL822-QFN88芯片设计支持PD快充的Type-C四口HUB的完整流程,包括芯片选型、系统架构设计、原理图要点、PCB布局及固件配置。特别适合硬件工程师和电子设计爱好者,帮助实现高速数据传输与快速充电的多端口扩展方案。 阅读详情

相关推荐

5分钟搞懂语义通信:如何用深度学习让AI理解你的真实意图?

本文深入解析了语义通信如何利用深度学习技术,实现从传统‘比特保真’到‘语义保真’的范式转移。通过介绍基于神经网络的端到端系统架构、编解码技术及实战案例,阐明AI如何理解并高效传递信息核心意图,为6G时代的人机交互奠定基础。

weixin_29191669的博客 263

Winform 中如何获取程序已经打开的窗体?

Application.OpenForms 了解一下~ foreach (Form frm in Application.OpenForms) //遍历所有打开的窗体! { if (frm is PageSetupForm) { frm.FormClosed += (s, e) => { }; //添加关闭事件 } } ...

iceagezh的专栏 3405

Innovus 安装全过程

【代码】Innovus 安装全过程。

lmyynlbcy的博客 5539

winforms小项目技术

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 1.在做多文档窗体MDI中多个界面切换用到的技术 不用复杂的if else 判断,下面的技术可以化繁为简: private void tsbMemberCon

lovejesuslove的博客 576

使用DeepSeek、MCP和AKShare实现智能金融问答系统技术方案

DeepSeek是由深度求索公司开发的大规模语言模型,最新版本DeepSeek-V3采用混合专家(MoE)架构,拥有671B参数,激活37B参数,在多项基准测试中表现优异3。要使用DeepSeek的API服务:访问DeepSeek平台注册账号获取API Key(形如AKShare是基于Python的金融数据接口库,提供股票、基金、期货、宏观经济等领域的海量数据。主要特点包括:免费开源,数据源丰富支持多种数据格式(DataFrame/JSON)持续更新维护安装命令。

Kin 3174

SQLite学习——Dapper初识

dapper下载:https://www.nuget.org/packages/Dapper/1.40.0

jiDxiaohuo的博客 1298

C# 同时保持两个窗体激活

C#中,如何同时保持两个窗体激活?基本的思想是当一个窗体激活时设置另一个窗体激活。这样相互激活。然而我们只是用单纯的C#代码是无法实现的,我们需要用到WinApI。我们现在有Form1和Form3两个窗体,Form1作为起始窗体。public partial class Form

abrahu的专栏 5380

从零开始:STM32C011开发板的硬件生态与快速验证

本文全面解析STM32C011开发板的硬件生态与快速验证方法,帮助开发者从零开始掌握这款基于ARM Cortex-M0+内核的微控制器。内容涵盖硬件架构解析、开发环境搭建、基础功能验证及性能优化技巧,为嵌入式开发提供实用指导。

swift5iosmith的博客 481

c# 类的反射实例 (GetType().Invoke().GetMethod().CreateInstance())

原文:http://www.cnblogs.com/chenwei19/archive/2009/02/04/1384034.html Class1和Form 窗体在同一个命名空间 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 namespace fanshetest1 ...

weixin_30319153的博客 262

【AI深究】合成数据(Synthetic Data)深度解析:原理、算法与工程实践——全网最详细流程|核心原理、主流方法、数学表达、工程实践与细节、未来趋势|GAN、VAE、语言模型LLM合成如GPT

大家好,我是爱酱。本篇延续将会系统梳理合成数据(Synthetic Data)的核心原理、主流方法、数学表达、工程实践与未来趋势,结合数学公式,帮助你全面理解这一AI建模的关键技术。 注:本文章含大量数学算式、详细例子说明及大量代码演示,大量干货,建议先收藏再慢慢观看理解。新频道发展不易,你们的每个赞、收藏跟转发都是我继续分享的动力!

AI人工智能爱酱~你的AI学习好帮手~ 4931

【24届数字IC秋招总结】正式批面试经验汇总6——禾赛

15、ahb写或者读的数据存储起来了嘛,ahb读的话,apb这边是怎样的。19、覆盖率收集的时候有没有哪些是很难收集到的,然后写了定向测试。2、C++有学过嘛 还记得C++的什么内容 C++成绩多少。12、config_db机制用过嘛 四个参数的含义。8、脚本语言有学过嘛 简单学过 简单是指代码数少嘛。13、run_phase和mian_phase的区别。10、覆盖机制的创建方法 ,是不是覆盖用的很少。6、call_back机制记得哪些细节的东西。9、uvm实战学了哪些章节 印象深刻的章节。

qq_42135020的博客 1083

C#如何获取某个对象的类型,以及GetType()与typeof()的区别

C#如何获取某个数据的类型 前言:在学习枚举类型之间的转换时,有时候我们不知道是否转换成我们想要的类型,所以就需要获取转后的类型。但是我也会技术小白,所以就查阅的相关的资料文献,将自己查阅总结的3种方法分享给大家,希望对读者能有一些帮助。 1、使用GetType( )方法 先了解一下GetType( )方法 ​ 1、从图中可以得知Tyoe在英语中是类型的意思。在C#中GetType()方法继承自Object(对象),也就是说类型来自于对象,换句话说C#中的任何对象都具有GetType( )方法。 ​

abraham_ly的博客 3万+

基于Simulink的四足机器人楼梯攀爬仿真模型

构建一个基于Simulink的四足机器人楼梯攀爬仿真模型,旨在模拟四足机器人在面对楼梯等障碍时的运动控制和稳定性调整。以下是一个详细的步骤指南,帮助你从零开始创建这样一个仿真模型。

xiaoheshang_123的博客 247

vs2019 创建c#代码片段

VS2019创建C#代码片段 找到内置代码片段所在目录 点击工具->代码片段管理器,弹出代码片段管理界面。 在界面中,语言选择CSharp,选择**Visual C#**节点下的一个代码片段,从位置中选择代码片段所在目录。 新建代码片段 这里,我复制了一份cw代码片段,作为模板文件,将文件结构修改如下: <?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/

yue1241630499的博客 1248

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

C#常用经典代码片段

本文主要记录项目中经常需要查阅的C#代码片段。 0x01 子线程操作UI 0x02 保存与读取二进制序列化文件 0x03 开机自启动

李乾文的博客 2695

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

C#常用代码片段备忘

C#常用代码片段备忘 来源 https://www.cnblogs.com/sanghg/p/8257024.html 参考:https://docs.microsoft.com/zh-cn/visualstudio/ide/visual-csharp-code-snippets?view=vs-2017 常用的代码片段: ctor 自动生成默认的构造函数prop自动生成get s...

weixin_30734435的博客 218

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

在Visual Studio中自定义C#代码片段

先点击工具选择代码片段管理器打开后显示如下界面,选择语言为CSharp,然后点击导入选择自定义的.snippet后缀的文件,点击打开选择代码片段导入Visual Studio的存放位置,然后点击完成点击刚刚导入的文件夹就可以看到我们导入的文件了,点击确定关闭界面。

weixin_42233704的博客 876

Visual Studio 2022 中自定义代码片段-C#图示详解

本文章主要讲解Visual Studio中自定义自己的代码片段,提高编译效率

猪猪的博客 3842

C#程序员经常用到的10个实用代码片段

转载地址:http://www.codeceo.com/article/10-csharp-code-spinet.html 如果你是一个C#程序员,那么本文介绍的10个C#常用代码片段一定会给你带来帮助,从底层的资源操作,到上层的UI应用,这些代码也许能给你的开发节省不少时间。以下是原文...

130

制作C#代码片段记录 propn

C# 代码片段

Hat_man_的博客 294

C# Snippet使用,代码片段管理

1、类注释 (原文地址https://www.cnblogs.com/netren/p/8031558.html)首先,找到安装路径下的Class.cs文件比如:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\CSharp\Code\2052\Class.cs用记事...

u012083407的博客 1657

c#代码片段新建(sinppet)

在c#里面我们经常用到 1、if 然后按2下Tab,出来下面一段代码 if (true) { } 2、propfull private int myVar; public int MyProperty { get { return myVar; } set { myVar = value; } } ……...

weixin_34413357的博客 193

C#自定义代码片段

需求:很多时候有些繁琐的重复的代码可以封装为代码片段,类似属性prop + tab。

代码改变世界 880

【学习笔记11】神奇的C#代码片段

VS的C#代码编辑时有一些神奇的代码片段 1 mbox 输入mbox后按2次Tab键,IDE会自动补全如下: MessageBox.Show("Test"); 并且选中Test可以直接方便输入修改。 2 for 输入for后按两次Tab键,IDE会自动补全如下: for (int i = 0; i < length; i++) { } 并且选...

REST2SQL ,JSON2WEB 667

Visual studio中C# 代码片段快捷输入和自定义代码片段

在visual studio中输入特殊字段的关键词然后双击Tab可以输入一段完整的代码片段,能够有效的提高开发效率。如输入`if`,两次Tab,会自动生成如下的代码片段,并且光标自动定义到if的条件内,修改完成按回车键自动定位到内部。 在代码片段管理器中找到代码片段存放路径随便找一个文件复制一份,比如for代码片段for.snippet文件,在vs2022中打开。

海盗Sharp的博客 3970

C# 自定义常用的代码片段

配置路径:工具=》代码片段管理器=》语言选择CSharp 和 VisualC#;就能看到代码片段的配置文件夹。可以复制一个到桌面来作为模板修改。配置路径:左上角=》文件=》首选项=》配置用户代码片段;在拉下框中选择csharp就会自动生成代码片段配置文件。class_ctor.snippet 带构造函数的class代码块。class.snippet 已经有默认的了,不过可以替换掉。

qq_26900081的博客 878
上一篇: C#开发日志[2013-12-6]内存暴增/class与struct的异同
下一篇: C#有用的类
cocoasprite
博客等级 码龄14年 72粉丝 101原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值