我有一个应用程序,用户可以从他的许多参数开始.如果我没有弄错的话,我想我可以在我的应用程序中得到那些执行参数.
应用程序是控制台应用程序
试图谷歌它,但没有找到如何.
我正在开展一个项目,我必须提示用户输入几个日期.这是我提出的用于提示用户日,月和年的代码.
Console.WriteLine("Day: ");
var dateDay = Console.ReadLine();
var dateDayInt = Convert.ToInt32(dateDay);
Console.WriteLine("Month: ");
var dateMonth = Console.ReadLine();
var dateMonthInt = Convert.ToInt32(dateMonth);
Console.WriteLine("Year: ");
var dateYear = Console.ReadLine();
var dateyearInt = Convert.ToInt32(dateYear);
DateTime myDate = new DateTime(dateYearInt, dateMonthInt, dateDayInt, 00, 00, 00, 000);
Run Code Online (Sandbox Code Playgroud)
我在一个方法中使用这个日期,所以它需要采用这种格式.
如您所见,这是一个重复的代码.如果我甚至添加一两个日期,我的项目将变得非常混乱.假设我需要20个日期.有没有更简单的方法来做到这一点,比如每个日期只有一个提示?提前致谢!
您好,我正在尝试学习一些 C# 编程,但我遇到了一些我很难理解的东西。我通常大部分时间都在处理VB6、VB.NET和VBA,所以我对编程有一些了解。这是我不确定的...
假设您有两个代码片段......
1)
static int Area(int h, int w)
{
return h*w;
}
Run Code Online (Sandbox Code Playgroud)
2)
static void Main(string[] args)
{
int res = Area(w: 5, h: 8);
Console.WriteLine(res);
}
Run Code Online (Sandbox Code Playgroud)
所以在第一个片段中,我正在执行 return ,它执行 H * W 的乘法,但我们没有像 #2 中那样用Console.WriteLine()?来写它。
返回实际上有什么作用?
在 #2 中 - 第一行static void Main(string[] args)- 特别是 string[] args 部分 - 这是什么意思?
我无法将参数传递给我的控制台应用程序。我是这样试的:
App.exe arg1
App.exe "arg1"
App.exe
Run Code Online (Sandbox Code Playgroud)
当我使用参数运行应用程序时,应用程序将退出运行,没有任何消息。
调试时没有任何内容string[] args。
我的 C# 项目是一个普通的 .net 4.5.2 命令行项目。我的操作系统是 Windows 10 x64。
示例代码:
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
namespace Setup
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
using (PowerShell psInstance = PowerShell.Create())
{
string cmd = "Set-ExecutionPolicy RemoteSigned -Force; " +
"echo \"Set-ExecutionPolicy RemoteSigned -Force\"; echo \"\"; " +
"Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force; ";
psInstance.AddScript(cmd);
Collection<PSObject> PSOutput = psInstance.Invoke();
Console.WriteLine(psInstance.Streams.Error[0].ErrorDetails.Message);
foreach (var …Run Code Online (Sandbox Code Playgroud) 我是 C# 的新手。我读了一个关于 C# 的教程,上面说使用 CMD 创建一个新项目。有命令行:
dotnet new console -n AppName -o 。
我的问题是为什么我们应该使用这个命令行?我们可以用 IDE 创建一个非常简单的新项目。
我有两个文件,File1.txt 和 File2.txt,我需要使用 C# 控制台应用程序将 file1 中的特定字符串替换为 file2 的内容。
文件1:
这是一个示例文件
..
代码块..
一些文字
文件结尾
文件2:
// 一些 C# 代码 //
我需要用文件 2 的内容替换字符串“code-block”。
编辑1:
我曾尝试将文件读取为数组和列表
var fileContent = File.ReadAllLines(file_path);
List<string> allLinesText = File.ReadAllLines(file_path).ToList();
string parentdirectory = Directory.GetParent(Path.GetDirectoryName(file_path)).FullName.ToString();
foreach( var line in fileContent)
{
if ( line.Contains("{% code-block %}"))
{
string code_path = line.Replace("{%", string.Empty).Replace("%}", string.Empty).Trim();
string code_fullpath = Path.Combine(parentdirectory, code_path);
if(File.Exists(code_fullpath))
{
var code_content = File.ReadAllLines(code_fullpath);
// int insert_code_at =
// allLinesText.Insert( allLinesText.IndexOf(line)
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为一个应用程序编写一个登录服务器,我将需要在同一个项目中我在另一台服务器上创建的所有对象,但是现在我想要一些方法在sepertate控制台屏幕中将这两个应用程序分开,而不运行2个serperate c#应用程序
我一直在尝试制作销售计划,向客户询问他将购买的商品数量.例如,如果他输入"5",下一个窗口将询问他5个问题.我的问题是,我需要使用数组,所以有什么方法可以改变
string[] arrmerch = new string[6];
Run Code Online (Sandbox Code Playgroud)
新字符串中的数字是"6",用户输入了什么?这是循环.项目选择是问题.
for (int i = 0; i <= arrmerch.Length; i++)
{
Console.Write("Item choice: ");
arrmerch[i] = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud) 我想知道如何将DateTime转换为字符串值(yyyy-mm-dd)。我有一个控制台应用程序,我希望用户能够将Date编写为“ yyyy-mm-dd”,然后将其转换为字符串。
我已经尝试过了,但是它似乎在相反的方向上起作用。这个想法是用户使用Console.ReadLine输入开始日期和结束日期。然后将这些值存储为字符串A和B中的字符串,以便以后使用。那可能吗?
string A = string.Empty;
string B = string.Empty;
DateTime Start = DateTime.ParseExact(A, "yyyy-mm-dd",CultureInfo.InvariantCulture);
Console.WriteLine("Enter StartDate! (yyyy-mm-dd)");
Start = Console.ReadLine();
DateTime End = DateTime.ParseExact(A, "yyyy-mm-dd",CultureInfo.InvariantCulture);
Console.WriteLine("Enter EndDate! (yyyy-mm-dd)");
End = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个如下所示的应用程序,由另一个控制台应用程序通过一个Process.Start()方法(即通过两个参数)在一个循环中触发两次.这里有第三种方法Lastmethod();将发送邮件包含输出startfirst();和 startsecond();.但我不要在完成前两种方法之后,我不知道如何触发第三种方法.我设置了两个标志,但是通过发送部分结果的邮件(没有完成前两个方法)显示有些时候是正确的并且有时错误的结果
static void Main(string[] args)
{
bool first = false;
bool second = false;
if (args[0] == "test")
{
startfirst();//time consuming process which will trigger internal child process
first = true;
}
if (args[0] == "finish")
{
startsecond();//time consuming process which will trigger internal child process
second =true;
}
if(first && second)
{
Lastmethod();
}
Console.ReadLine();
}
private static void Lastmethod()
{
//sending mail of the out put of startfirst(); and startsecond(); …Run Code Online (Sandbox Code Playgroud)