在C#中将Process.StartInfo.Arguments作为字符串传递,该字符串内部包含双引号(“) [英] Passing Process.StartInfo.Arguments in C# as a string that contains double-quotes (") within itself
问题描述
我希望通过我的C#代码运行mongoexport.exe进程,为此,其参数如下:
I wish to run the mongoexport.exe process through my C# code, for which, let's say, the arguments go something as follows:
-查询 query_syntax
以上所有文本均应存储在C#代码的字符串变量中,并作为必需的参数传递。
The whole of the above text shall be stored in a string variable in the C# code and passed as the required argument.
现在的问题是C#在内部使用转义符()存储此类字符串。相同的转义符甚至保留在命令行中,使我的最终参数看起来如下:
The issue now is that C# internally stores such strings with the escape character (). The same escape character even gets retained in the command-line, making my final argument look something as follows:
-query \ query_syntax\
上述转义字符是我的代码失败的原因,我无法找到任何解决方法。
The above escape character is the reason fro the failure of my code and I fail to find any way about it.
public static string dateConverter(DateTime dt)
{
long decimalNumber = (long)(dt.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
return (Convert.ToString(decimalNumber, 16));
}
public static void Main(string[] args)
{
try
{
CultureInfo provider = CultureInfo.InvariantCulture;
string instr;
Console.WriteLine("Enter the start date");
instr = Console.ReadLine();
DateTime.TryParseExact(instr, "yyyy/MM/dd", provider, DateTimeStyles.None, out startDate);
Console.WriteLine("Enter the end date");
instr = Console.ReadLine();
DateTime.TryParseExact(instr, "yyyy/MM/dd", provider, DateTimeStyles.None, out endDate);
queryFilter = "{_id:{$gte: ObjectId('" + dateConverter(startDate) + "0000000000000000'), $lte: ObjectId('" + dateConverter(endDate) + "ffffffffffffffff')}}";
string expstring = " --db yourDatabaseName --collection yourCollectionName --type json --query " + queryFilter + " --out yourFilePath --jsonArray";
Process export = new Process();
export.StartInfo.FileName = ExportEXEPath;
export.StartInfo.Arguments = expstring;
export.Start();
}
catch (Exception ex)
{
Console.WriteLine("[ERROR]: " + ex.Message);
}
}
推荐答案
无互联网上提到的解决方案中的一部分对我来说效果很好,所以我想出了一个逃生计划,而不是解决问题的办法。我将双引号作为字符存储在变量中,并根据需要将其附加到字符串构建语句中。
然后,我的代码如下所示:
None of the solutions mentioned on the internet worked well for me, so I came up with an escape plan rather than a solution to the problem. I stored the double-quotes as a character in a variable, and appended it to the string building statement as per needs. My code then looked something as follows:
public static string dateConverter(DateTime dt)
{
long decimalNumber = (long)(dt.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
return (Convert.ToString(decimalNumber, 16));
}
public static void Main(string[] args)
{
try
{
CultureInfo provider = CultureInfo.InvariantCulture;
string instr;
char ch = '"'; //added statement
Console.WriteLine("Enter the start date");
instr = Console.ReadLine();
DateTime.TryParseExact(instr, "yyyy/MM/dd", provider, DateTimeStyles.None, out startDate);
Console.WriteLine("Enter the end date");
instr = Console.ReadLine();
DateTime.TryParseExact(instr, "yyyy/MM/dd", provider, DateTimeStyles.None, out endDate);
queryFilter = "{_id:{$gte: ObjectId('" + dateConverter(startDate) + "0000000000000000'), $lte: ObjectId('" + dateConverter(endDate) + "ffffffffffffffff')}}";
//reframed statement
string expstring = " --db yourDatabaseName --collection yourCollectionName --type json --query " + ch + queryFilter + ch + " --out yourFilePath --jsonArray";
Process export = new Process();
export.StartInfo.FileName = ExportEXEPath;
export.StartInfo.Arguments = expstring;
export.Start();
}
catch (Exception ex)
{
Console.WriteLine("[ERROR]: " + ex.Message);
}
}
这篇关于在C#中将Process.StartInfo.Arguments作为字符串传递,该字符串内部包含双引号(“)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!