簡體   English   中英

C#中的命令行參數

[英]Command line arguments in c#

我對c#很陌生。 命令行參數出現問題。 我想做的是利用第三個cmd行參數並將其寫入。 我已經指定了要寫入的文件的路徑以及其他內容。 但是這里的問題是我可以從用戶定義的函數訪問命令行參數(例如args [3])嗎? 我們該怎么做? 下面是我的代碼。

public class Nodes
{
public bool isVisited;
public string parent;
public string[] neighbour;
public int nodeValue;

public Nodes(string[] arr, int nodeValue)
{
    this.neighbour = new string[arr.Length];
    for (int x = 0; x < arr.Length; x++)
        this.neighbour[x] = arr[x];//hi...works??
    this.isVisited = false;
    this.nodeValue = nodeValue;
}


}

public class DFS
{
static List<string> traversedList = new List<string>();

static List<string> parentList = new List<string>();
static BufferBlock<Object> buffer = new BufferBlock<object>();
static BufferBlock<Object> buffer1 = new BufferBlock<object>();
static BufferBlock<Object> buffer3 = new BufferBlock<object>();
static BufferBlock<Object> buffer2 = new BufferBlock<object>();

public static void Main(string[] args)
{

    int N = 100;
    int M = N * 4;
    int P = N * 16;

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    List<string> global_list = new List<string>();


    StreamReader file = new StreamReader(args[2]);

    string text = file.ReadToEnd();

    string[] lines = text.Split('\n');


    string[][] array1 = new string[lines.Length][];
    Nodes[] dfsNodes = new Nodes[lines.Length];

    for (int i = 0; i < lines.Length; i++)
    {
        lines[i] = lines[i].Trim();
        string[] words = lines[i].Split(' ');

        array1[i] = new string[words.Length];
        dfsNodes[i] = new Nodes(words, i);
        for (int j = 0; j < words.Length; j++)
        {
            array1[i][j] = words[j];
        }
    }
    StreamWriter sr = new StreamWriter(args[4]);

    int startNode = int.Parse(args[3]);

    if (args[1].Equals("a1"))
    {
        Console.WriteLine("algo 0");
        buffer.Post(1);
        dfs(dfsNodes, startNode, "root");
    }
    else if (args[1].Equals("a2"))
    {
        Console.WriteLine("algo 1");
        buffer1.Post(1);
        dfs1(dfsNodes, startNode, "root",sr);
    }
    else if (args[1].Equals("a3"))
    {
        buffer3.Post(1);
        List<string> visitedtList = new List<string>();
        Console.WriteLine("algo 2");
        dfs2(dfsNodes, startNode, "root", visitedtList,sr);
    }

    stopwatch.Stop();

    Console.WriteLine(stopwatch.Elapsed);
    Console.ReadLine();
}

public static void dfs(Nodes[] node, int value, string parent,StreamWriter sr1)
{
    int id = (int)buffer.Receive();
    sr1=new StreamWriter(arg
    Console.WriteLine("Node:" + value + " Parent:" + parent + " Id:" + id);
    sr1.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    id++;
    traversedList.Add(value.ToString());
    buffer.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        if (!traversedList.Contains(node[value].neighbour[z]))
        {
            dfs(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr1);
        }

    }
    return;



}

public static void dfs1(Nodes[] node, int value, string parent, StreamWriter sr)
{
    int id = (int)buffer1.Receive();
    sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    node[value].isVisited = true;
    node[value].parent = parent;
    id++;
    buffer1.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
        if (!isVisited())
        {
            dfs1(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr);
        }

    }
    return;



}

public static void dfs2(Nodes[] node, int value, string parent, List<string> visitedtList, StreamWriter sr)
{
    int id = (int)buffer3.Receive();
    sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    id++;
    visitedtList.Add(value.ToString());
    buffer3.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
        if (!visitedtList.Contains(node[value].neighbour[z]))
            dfs2(node, int.Parse(node[value].neighbour[z]), value.ToString(), visitedtList,sr);

    }
    return;



}

public static bool isVisited()
{
    Nodes node = (Nodes)buffer2.Receive();
    return node.isVisited;
}

}

因此,我想將每個dfs的輸出寫入指定為命令行參數的文件中。 所以我可以訪問dfs,dfs1方法中的args嗎??? 謝謝。

您可以保留一個靜態字段來保存它,也可以只使用Environment.GetCommandLineArgs()

好吧,以最簡單的形式,將其保存以備后用

class Program
{
    static string _fpath;
    static void Main(string[] args)
    {
        // ...stuff
        _fpath = args[3];
    }

    static void WriteFile()
    {
        using(var stream = File.Open(_fpath, ...))
        {
            // write to file
        }
    }
}

不一定確切我會怎么做,但是您明白了。

另外,關於這段代碼...

this.neighbour = new string[arr.Length];
for (int x = 0; x < arr.Length; x++)
    this.neighbour[x] = arr[x];//hi...works??

你可以簡單地寫

this.neighbour = arr;

啊,托管代碼的奇跡:D。 無需將元素復制到第二個數組。 當然,您需要考慮以下事實:對參數數組( arr )中元素的更改現在將反映在內部數組中。

最好將參數傳遞給函數,而不要依靠某種“隱藏”的方式傳遞參數。

靜態變量和GetCommandLineArgs均可用於以隱藏方式傳遞它們(如其他答案所指出)。 缺點很難測試(因為需要設置靜態共享依賴項),並且對將來的讀者來說不清楚存在這種隱藏的依賴項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM