C#中 string[]转为string及VS中如何带命令行调试、main函数获取命令行参数

数组:string[] s = { "a", "b", "c", "d", "e" };
①Join转换字符串:string str1 = string.Join(",", s);

②格式化字符串:string str2 = String.Format("{0},{1},{2},{3},{4}", s);

在VS2008 控制台应用程序工程中,右击项目名称-->属性-->调试-->启动选项-->命令行参数,输入命令参数即可

命令参数通过main(string[] args)中的args传入程序,然后利用上面的方法可转成string类型,赋给程序中的变量来使用,命令长度可通过args.length.toString()来打印出来。

可创建.bat文件,输入如:@echo off

                                             D:\work_lq\环回工具备份\NetServer\bin\Release\NetServer.exe 5000

保存后双击此文件便可运行程序,其中绝对路径也可改为相对路径(但此时.bat文件需放在程序目录下)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 调用 C++ DLL 函数的时候,如果函数的参数是 std::string 类型,需要进行一些特殊的处理。因为在 C++ ,std::string 类型实际上是一个类,而在 C# 没有对应的类型。 一种解决方案是,将 C++ 函数参数的 std::string 类型改为 char* 类型,并且增加参数来指定字符串的长度。在 C# ,可以使用 Marshal 类的各种方法来将字符串换为 char* 类型,并将字符串的长度传递给 C++ 函数。 下面是一个示例代码,演示了如何在 C# 调用一个 C++ DLL 函数,该函数的参数类型为 std::string: C++ DLL 函数的代码: ```c++ #include <string> #include <iostream> // 定义一个使用 std::string 作为参数的函数 void printString(std::string str) { std::cout << str << std::endl; } ``` 在 C# 调用该函数的代码: ```c# using System; using System.Runtime.InteropServices; class Program { // 声明 C++ DLL 函数 [DllImport("MyCppLib.dll", CallingConvention = CallingConvention.Cdecl)] static extern void printString([MarshalAs(UnmanagedType.LPStr)] string str, int length); static void Main(string[] args) { // 要传递给 C++ DLL 函数的字符串 string myString = "Hello, world!"; // 将字符串换为 char* 类型,并获取字符串的长度 byte[] strBytes = System.Text.Encoding.ASCII.GetBytes(myString); int strLength = strBytes.Length; // 调用 C++ DLL 函数 printString(myString, strLength); } } ``` 在 C# ,使用 [MarshalAs(UnmanagedType.LPStr)] 特性将 C++ 函数参数的 std::string 类型换为 char* 类型,使用 System.Text.Encoding.ASCII.GetBytes() 方法将字符串换为 byte[] 类型,并使用该数组的长度作为字符串的长度参数传递给 C++ 函数。 希望这个示例代码对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值