我有一个工具只支持打开" * .TMC"通过拖动文件 - 或使用其"打开"按钮我将通过Windows命令参数(文件路径作为参数)使此工具支持打开文件,例如
开始"" " TMC Polygon Tool.exe" " E:\ tmcfile \ kasumi.tmc"
以下是源文件http://www.mediafire.com/download/mc72m97h876i550/TMC.zip
的链接这是" App.g.i.cs"的一部分。 (这是原始代码,但不能通过命令参数接受打开)
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
TMC_Tool.App app = new TMC_Tool.App();
app.InitializeComponent();
app.Run();
}
和#34; MainWindow.xaml.cs"
的一部分public MainWindow()
{
InitializeComponent();
this.MouseLeftButtonDown += (sender, e) => this.DragMove();
MainWindowTitle();
changeLanguage();
MessageWindow.lang(txt);
ObjectSelectWindow.lang(txt);
objSelWindow = new ObjectSelectWindow();
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
}
我尝试添加string [] args,以使用现有方法" OpenFile(args [0])" ,但它会导致错误"对象引用未设置为对象的实例。"
这是我的代码(导致错误)" App.g.i.cs"
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main(string[] args)
{
TMC_Tool.App app = new TMC_Tool.App();
app.InitializeComponent();
app.Run(new MainWindow(args));
}
" MainWindow.xaml.cs"
public MainWindow(string[] args)
{
InitializeComponent();
if (args.Length > 0)
{
String FileEx = Path.GetExtension(args[0]).ToUpper();
if (FileEx == ".TMC")
OpenFile(args[0]);
}
this.MouseLeftButtonDown += (sender, e) => this.DragMove();
MainWindowTitle();
changeLanguage();
MessageWindow.lang(txt);
ObjectSelectWindow.lang(txt);
objSelWindow = new ObjectSelectWindow();
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
}
编辑: 它现在有效, Environment.GetCommandLineArgs()非常有帮助
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
TMC_Tool.App app = new TMC_Tool.App();
app.InitializeComponent();
app.Run();
}
public string filePath = null;
public MainWindow()
{
var args = Environment.GetCommandLineArgs();
// original code ....
filePath = args.Length > 1 ? args[1] : null;
if (!string.IsNullOrEmpty(filePath))
{
OpenFile(filePath);
}
}
由于
答案 0 :(得分:0)
只需将此行添加到原始代码中即可。
public MainWindow()
{
var args = Environment.GetCommandLineArgs();
// path will be in args[1] if there is any.
}