0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

exeへの引数処理

Last updated at Posted at 2019-12-22

練習とメモ用
定期的に機能増やす予定

機能
引数1つの場合のみ対応
コンソールとドロップで実行可能
絶対パスとファイル名、拡張子が取得可能
.lnkの時に参照元のパスを取得
""で引数を渡されなくても実行可

注意点
Windows Script Host Object Modelを参照しなければならない

Program.cs
using System;

namespace FileGet
{
    class Program
    {
        static void Main(string[] args)
        {
            GetFile getFile = new GetFile(args);
            Console.ReadKey();
        }
    }
}
GetFile.cs
using System;
using System.Linq;

namespace FileGet
{
    class GetFile
    {
        private IGetFileProcess pro;
        public GetFile(string[] args)
        {
            string[] Path = Environment.GetCommandLineArgs();
            if (args.Any())
            {
                pro = new FileArgument(args);
            }
            else if (Path.Length > 1)
            {
                pro = new FileDrop(Path);
            }
            else { Console.WriteLine("引数とドロップはありませんでした。"); return; }
            Console.WriteLine("FilePath:{0}", pro.FilePath);
            Console.WriteLine("FileName:{0}", pro.FileName);
            Console.WriteLine("FileType:{0}", pro.FileType);
        }
    }
}
IGetFileProcess.cs
namespace FileGet
{
    public interface IGetFileProcess
    {
        string FilePath { get; set; }
        string FileName { get;}
        string FileType { get;}
    }
}
FileDrop.cs
using System.IO;
using IWshRuntimeLibrary;

namespace FileGet
{

    class FileDrop : IGetFileProcess
    {
        public string FilePath { get; set; }
        public string FileName 
        {
            get { return Path.GetFileName(@FilePath); }
        }
        public string FileType
        {
            get { return Path.GetExtension(@FilePath); }
        }
        public FileDrop(string[] arg)
        {
            FilePath = arg[2];
            if (Path.GetExtension(@FilePath) == ".lnk")
            {
                WshShell Shell = new IWshRuntimeLibrary.WshShell();
                IWshShortcut Shortcut = (IWshShortcut)Shell.CreateShortcut(FilePath);
                FilePath = Shortcut.TargetPath.ToString();
            }
        }
    }
}

FileArgument.cs
using System;
using System.IO;
using IWshRuntimeLibrary;

namespace FileGet
{
    class FileArgument : IGetFileProcess
    {
        public string FilePath { get; set; }
        public string FileName 
        {
            get { return Path.GetFileName(@FilePath); }
        }
        public string FileType
        {
            get { return Path.GetExtension(@FilePath); }
        }

        public FileArgument(string[] arg)
        {
            FilePath = String.Join(" ", arg);
            if (FilePath.Contains("\""))
            {
                FilePath = FilePath.Replace("\"", "");
            }
            if (Path.GetExtension(@FilePath) == ".lnk")
            {
                WshShell Shell = new WshShell();
                IWshShortcut Shortcut = (IWshShortcut)Shell.CreateShortcut(FilePath);
                FilePath = Shortcut.TargetPath.ToString();
            }
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?