我如何获得文件路径 [英] How do I get File Path

查看:56
本文介绍了我如何获得文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说,我已经用WPF编写了一些简单的文本编辑程序,例如NotePad.

当我使用命令打开方式"(我的编辑器)打开任何".txt"文件时,如何从程序中获取该文件的路径?

在此先谢谢您.

Lets say, I''ve wrote with WPF some simple text editing program like NotePad.

When I open any ".txt" file with command "Open with" (my editor) how do I get path of this file from my program?

Thanks in advance.

推荐答案

这取决于在文件类型注册中编写的命令行.最典型地,使用第一个参数,并使用带有输入文档的文件.您可以使用System.Environment.GetCommandLineArgs()[1]方法获取其名称,请参见:
http://msdn.microsoft.com/en-us/library/system.environment. getcommandlineargs.aspx [ ^ ].

重要的是检查命令行参数的数量,以检查由上面显示的函数返回的数组的长度-命令行中可能没有参数.在所有情况下,第一个参数始终是正在运行的应用程序的入口程序集的主要可执行模块的文件名.

另一种方法是使用传递给应用程序入口点的参数.通常,这是Main方法.请参阅:
http://en.wikipedia.org/wiki/Main_function#C.23 [ ^ ].

为此,应将签名与args一起使用,并使用args[0](第一个参数是命令行中的第一个参数,而不是可执行文件名).您应该始终检查数组的长度是否大于零.命令行为空时为零.

有关易于使用的复杂的命令行设置和解析的信息,请参阅我关于以下主题的文章:
基于枚举的命令行实用程序 [
It depends on the command line written in the file type registration. Most typically, the first parameter is used and the file with input document. You get its name using the method System.Environment.GetCommandLineArgs()[1], please see:
http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx[^].

It''s important to check up the number of command line arguments checking the length of the array returned by the function shown above — the command line may not have parameters. In all cases, the very first parameter is always a file name of a main executable module of the entry assembly of the running application.

Another method is using the parameters passed to the entry point of the application; normally, this is the Main method. Please see:
http://en.wikipedia.org/wiki/Main_function#C.23[^].

For this purpose, you should use the signature with args and use args[0] (first parameter is the first parameter in the command line, not executable file name). You should always check up if the length of the array is more than zero; zero is the case of empty command line.

For a sophisticated yes easy-to-use command line setup and parsing, please see my article on the topic:
Enumeration-based Command Line Utility[^].

—SA


// Create OpenFileDialog

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();          

 

// Set filter for file extension and default file extension

dlg.DefaultExt = ".txt";

dlg.Filter = "Text documents (.txt)|*.txt";

 

// Display OpenFileDialog by calling ShowDialog method

Nullable<bool> result = dlg.ShowDialog();

 

// Get the selected file name and display in a TextBox

if (result == true)

{

    // Open document

    string filename = dlg.FileName;

    FileNameTextBox.Text = filename;

 }
</bool>




来源:
WPF中的OpenFileDialog [




Source: OpenFileDialog in WPF[^]


这篇关于我如何获得文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆