C#获取当前运行时环境信息

目录

获取框架信息,操作系统信息

csharp 复制代码
   //.net 框架信息
   var kkk = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;

   //获取应用程序多少位
   var bbb = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture;

   //获取操作系统 多少位?x64? x86?
   var ccc = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture;

   //获取操作系统信息
   var aaa = System.Runtime.InteropServices.RuntimeInformation.OSDescription;

返回结果1:

bash 复制代码
.NET Framework 4.8.9181.0
X64
X64
Microsoft Windows 10.0.22621 

获取当前堆栈跟踪信息,可用于判断是否为单元测试

csharp 复制代码
//当前堆栈跟踪信息,可以判断是否在"单元测试模式"里面;
string stackTrace = System.Environment.StackTrace;

//true则为单元测试环境下,false不是
bool unitTest = System.Environment.StackTrace.IndexOf("NUnit.Framework", StringComparison.CurrentCultureIgnoreCase) >= 0;

//true则为单元测试环境下,false不是
bool uniTest2=System.Environment.StackTrace.IndexOf("TestPlatform.MSTest", StringComparison.CurrentCultureIgnoreCase) >= 0;

返回结果2:

bash 复制代码
在 System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   在 System.Environment.get_StackTrace()
   在 UnitTestProject1.UnitTest3.<TestPdf>d__20.MoveNext() 位置 E:\Work\信息报送\zhi_mi_kt_information\V4_WxPush\WuZiFenGongSiInfomation\UnitTestProject1\UnitTest3.cs:行号 344
   在 System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
   在 UnitTestProject1.UnitTest3.TestPdf()
   在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   在 Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions.MethodInfoExtensions.InvokeAsSynchronousTask(MethodInfo methodInfo, Object classInstance, Object[] parameters)
   在 Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)
   在 Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodInfo.ExecuteInternal(Object[] arguments)
   在 Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodInfo.Invoke(Object[] arguments)
   在 Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.Execute(ITestMethod testMethod)
   在 Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodRunner.RunTestMethod()
   在 Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodRunner.Execute()
   在 Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner.RunSingleTest(TestMethod testMethod, IDictionary`2 testRunParameters)
相关推荐
WarPigs5 小时前
C# dll笔记
c#
淡笑沐白5 小时前
C# HttpClient完整使用指南
c#·httpclient
JaydenAI5 小时前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
小满Autumn5 小时前
MVVM Light 架构笔记:定位器、命令、消息与 IoC 实践
笔记·学习·架构·c#·上位机·mvvm
小满Autumn7 小时前
CommunityToolkit.Mvvm 架构笔记:现代 MVVM、源生成器与工程化实践
笔记·架构·c#·.net·wpf·mvvm
加号38 小时前
【C#】 JSON 序列化与反序列化:从入门到最佳实践
c#·json
胖纸不争11 小时前
自建 Copilot Cli 代理:让 GitHub Copilot 真正"Bring Your Own Key"
ai·c#
FuckPatience13 小时前
C# new List<T>(IEnumerable<T> collection),链表初始化时传入已存在链表
链表·c#·list
专注VB编程开发20年16 小时前
工控上位机开发为什么固死.net 4.5.2sdk?适配win7
python·信息可视化·c#
狂人开飞机17 小时前
18. 中介者模式(Mediator Pattern)
设计模式·c#·中介者模式