用-problem-打开如何将文件的内容复制到MyProgram ..中的变量中. [英] open with -problem- How do I copy the content of that file into a variable from MyProgram.....

查看:82
本文介绍了用-problem-打开如何将文件的内容复制到MyProgram ..中的变量中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开-problem-
我有一个用自定义扩展名重命名的".txt"文件:.bzx".
我希望该文件在单击时能够:从"\ Program Files \或\ whatever"通过"MyCustomProgram打开".
如何使用StreamReader方法(或OtherMethod)将该文件的内容从MyCustomProgram复制到变量中?

Open With -problem-
I have a ".txt" file renamed with a custom extension: ".bzx".
I want this file, when I click on it, to be able to: "Open With" MyCustomProgram from "\Program Files\or\whatever".
How do I copy the content of that file into a variable from MyCustomProgram using StreamReader method(or OtherMethod) ?

StreamReader reader = new StreamReader(path + "\\?????!!!!??????.txt")



....一段时间后,我发现了可以正确问这个问题的术语(我希望这些术语是正确的)...
让我改一下:
如何将随机文件作为参数传递给Load方法?
那是我一直想问的问题……我对术语不是很友好.



....after some time I discover the terms to ask this question properly (I hope the terms are right)...
let me rephrase:
How do I pass a random file to the Load method as a parameter?
That is all the time I wanted to ask... im not very friendly with terms.

推荐答案

一种方法是将其作为字符串传递:
One way is to pass it as a string:
public void Load(string path)
{
    using(StreamReader reader = new StreamReader(path))
    {
        // do whatever you need to do with the file
    }
}




好的,您希望能够双击一个文件并在程序中打开它?好吧,听起来您已经将文件扩展名注册到了程序中,所以:

1.在Program.cs中,将字符串数组作为参数添加到Main()中.这将包含您双击的文件的路径.




Okay, you want to be able to double click a file and have it open in your program? Well, it sounds like you got the file extension registered to your program, so:

1. In Program.cs add a string array as a parameter to your Main(). This will contain the path to the file you double clicked on.

static void Main ( string[] args )
{
    if ( args.Length > 0 )
    {
        Application.Run( new MainFrm( args[0].Trim() ) );
    }
    else
    {
        Application.Run( new MainFrm() );
    }
}



2.重载程序的主窗体构造函数以获取字符串.这是将路径从程序的入口点传递到表单的方式.



2. Overload your program''s main form constructor to take a string. This is how you will pass the path from the program''s entry point to the form.

public partial class MainForm : Form
{
    private string file_path = string.Empty;

    public MainForm (){}
    public MainForm ( string path )
    {
        this.file_path = path;
    }
}



3.使用file_path变量打开文件,我将编写一个单独的LoadFile()函数,该函数可以从MainForm_Load事件处理程序或您可能希望从中加载文件的代码中的其他任何位置调用.



3. Use the file_path variable to open the file, I would write a separate LoadFile() function that can be called from either the MainForm_Load event handler or anywhere else in your code you might want to load a file from.

public partial class MainForm : Form
{
    private string file_path = string.Empty;

    public MainForm (){}
    public MainForm ( string path )
    {
        this.file_path = path;
    }

    private void MainForm_Load ( object sender, EventArgs e )
    {
        if ( this.file_path != string.Empty )
        {
            this.LoadFile();
        }
    }

    private void LoadFile()
    {
        using ( StreamReader reader = new StreamReader ( new FileStream ( this.file_path, FileMode.Open ) ) )
        {
            // do whatever you need to do to load into your program here
        }
    }
}


[/EDIT]


string txt = File.ReadAllText(path + "filename.ext");


这个问题和许多评论尚不清楚...但是无论如何,该答案应该给出一些指示:

1)要将扩展与特定程序相关联,最简单的方法是通过安装项目...

简单地复制一个文件通常不是一个好主意,因为它不会出现在添加/删除"程序中,下载后该文件可能会被阻止,不会在菜单或菜单上创建快捷方式桌面...

2)要构建路径,请使用Path类.最有用的方法是Combine:
Path.Combine [ Environment.GetFolderPath [ Assembly.GetExecutingAssembly [
The question and many comments are not clear... but anyway, that answer should give some directions:

1) To associate an extention with a particular program, the easiest way is through a setup project...

It is generally not a good idea to simply copy the file as it won''t appear in Add/Remove program, the file might get blocked after the download, it won''t create a shorthcut in the menu or on the desktop...

2) To build a path, you use Path class. The most useful method is Combine:
Path.Combine[^]

One advatange of using such function is that you don''t have to check if the directory ends with a / or a \ before combining parts.

3) To get the name of predefined folder, you use:
Environment.GetFolderPath[^]

Typically, an application would use some subdirectories of those predefined folders

4) You can get the path of the executable from executing assembly and its properties:
Assembly.GetExecutingAssembly[^]

It is not recommanded to use that folder or any subfolder of Program files for user data as that folder is "protected" in Windows Vista and Windows 7. User data should generally be under "My documents" or under user application data.


这篇关于用-problem-打开如何将文件的内容复制到MyProgram ..中的变量中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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