Ok, first things first. Hello everyone, I have a question.
How would I go about adding Shortcut Arguments to my program(Code shown below).

My idea:
I play a game called Counter-Strike:Source and recently I've needed to gather all the names of custom maps added to the \map
directory. To the point. I've created this program to read files with the extension of ".txt". While creating this program I thought why not try learning how to create Shortcut Arguments. What I mean by this is when you create a shortcut to any program a file gets created( a ".lnk" file) and we all know what this file does(points to the file of which was linked). Now with some programs they allow the use of Arguments. Here I show an example:
Before any arguments:

Target: "C:\Program Files\Mozilla Firefox\firefox.exe"

After:

Target: "C:\Program Files\Mozilla Firefox\firefox.exe" -txt

Now I doubt that this will change anything with firefox but I would like to add something that allows the usage of arguments.

Lets say if I wanted to get a list of all the files that had the extension ".123" I would add -123 to the end of the Target: in my programs shortcut properties.

Anyways I did my best to be as clear as I possibly could if you need a further explanation please let me know and I'll see what I can do to explain it further.

Thanks in advance. - Poab9200

using System;
using System.IO;

namespace LoopThroughFiles
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.Title = "Loop Trough Files";
            string sDirectoryName;
            if (args.Length == 0)
            {
                sDirectoryName = Directory.GetCurrentDirectory();
            }
            else
            {
                sDirectoryName = args[0];
            }
            FileInfo[] files = GetFileList(sDirectoryName);
            using (StreamWriter sw = new StreamWriter(@"Relative File Paths.txt"))
            {
                sw.WriteLine("// Program created by: Poab9200");
                foreach (FileInfo file in files)
                {
                    if (file.Extension == ".txt")
                    {
                        sw.WriteLine(file);
                    }
                    else { }
                }
            }
        }
        public static FileInfo[] GetFileList(string sDirectoryName)
        {
            FileInfo[] files = new FileInfo[0];
            try
            {
                DirectoryInfo di = new DirectoryInfo(sDirectoryName);
                files = di.GetFiles();
            }
            catch (Exception e)
            {
                Console.WriteLine("Directory \" + sDirectoryName" + "\" invalid");
                Console.WriteLine(e.Message);
            }
            return files;
        }
    }
}

Dani AI

Generated

As pointed out, the correct term is "command-line arguments" and already solved the basic case. The notes below add a few practical hardening tips and a compact parser you can drop into your app so a .lnk shortcut (or an IDE debug setting) can pass an extension and/or a directory reliably.

Windows places whatever you type after the quoted exe path into the process command line. The runtime splits that string into tokens and Main(string[] args) receives those tokens (Environment.GetCommandLineArgs() returns the exe name at index 0). Quoted arguments preserve spaces. Because shortcuts may set a different working directory, prefer an explicit output path or use AppDomain.CurrentDomain.BaseDirectory so output lands where you expect.

static (string Dir, string Ext) ParseOptions(string[] args)
{
    string dir = AppDomain.CurrentDomain.BaseDirectory;
    string ext = ".txt";

    foreach (var raw in args)
    {
        var a = raw.Trim().Trim('"');
        if (a.StartsWith("-ext:", StringComparison.OrdinalIgnoreCase) ||
            a.StartsWith("/ext:", StringComparison.OrdinalIgnoreCase))
        {
            ext = NormalizeExt(a.Substring(a.IndexOf(':') + 1));
            continue;
        }

        if (Directory.Exists(a)) { dir = a; continue; }

        // allow bare extensions like ".map" or "map"
        if (a.Length <= 6) { ext = NormalizeExt(a); }
    }

    return (dir, ext);
}

static string NormalizeExt(string e)
{
    e = (e ?? string.Empty).Trim().Trim('"');
    if (e.Length == 0) return ".txt";
    return e.StartsWith(".") ? e.ToLowerInvariant() : ("." + e.ToLowerInvariant());
}

Practical tips: use Directory.EnumerateFiles when scanning large folders (streams results instead of building a large array), compare extensions case-insensitively, avoid ambiguous flags like "-123" (prefer -ext:.123), test both from a command prompt and by double-clicking the shortcut, and accept an output path or use BaseDirectory to avoid surprises with working directory and permissions.

Recommended Answers

All 4 Replies

".lnk" file) and we all know what this file does(points to the file of which was linked)

???
I know a linker is a part of some compilers.
Do you mean command line arguments?
You seem to know that, so what is your problem?

???
I know a linker is a part of some compilers.
Do you mean command line arguments?
You seem to know that, so what is your problem?

Shortcuts use the extension .lnk. If you have shortcuts look at them via(Right click > Properties then click the general tab and it should show the name of the extension...

Ok, I did a little research and yes that is what I am looking for. But I want to have my program accept the command line arguments. How would I go about doing this?

Ok never mind I figured out a way to make it work and here is where I got the info to figure it out.

Access Command Line

That info helped out a lot. Anyways thanks for your help. You saying Command Line Arguments was the phrase I needed to find the info.

Thanks -Poab9200

Glad I could sort this out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.