1

You can set Windows to open .doc files with Word or another application. How can I create such a c# applications, which can handle, if for excample I open a .txt file with that application? So the plan is: There's a information.kkk file wich is a text file and there's a number in it. I want my c# application (Visual Studio 2010) to receive that number if the file gets opened by it.

tshepang
  • 12,111
  • 21
  • 91
  • 136
weiszam
  • 187
  • 4
  • 14
  • 1
    There is an older answer in [SO here][1] that seems to cover your requirements. [1]: http://stackoverflow.com/questions/69761/how-to-associate-a-file-extension-to-the-current-executable-in-c-sharp – renick Nov 11 '12 at 14:01
  • Agree I was just about to paste that,.,. you beets me! – FlavorScape Nov 11 '12 at 14:02
  • 1
    http://stackoverflow.com/questions/2681878/associate-file-extension-with-application This one seems a cleaner approach tho – Machinarius Nov 11 '12 at 14:02
  • where do I get the string of the file in c#? I didn't quite understand it there. so you can set in Windows to open the file with your application, but inside c# - how do you handle the string of the file? – weiszam Nov 11 '12 at 14:07
  • @weiszam - You would have to write code to read the string. – Security Hound Nov 11 '12 at 16:17

3 Answers3

4

In Console application use args parameter in Main function. First arg is path to opening file.

For example:

class Program
{
    static void Main(string[] args)
    {
        var filePath = args[0];

        //...
    }
}

In WPF application use Application_Startup event:

private void Application_Startup(object sender, StartupEventArgs e)
{
    var filePath = e.Args[0];
    //...
}

Or use Enviroment class - anywhere in your .net application:

string[] args = Environment.GetCommandLineArgs();
string filePath = args[0];
mveith
  • 477
  • 1
  • 4
  • 14
  • What do I do wrong if I use WindowsFormsApplication? It does not recognise the args part. ![image](http://weisza.uw.hu/fajlok/args.png). – weiszam Nov 12 '12 at 17:27
  • @weiszam: Use Enviroment class (edited answer) or add parameter "string[] args" to Main method (Program.cs) - static void Main(string[] args). [Example link](http://www.blackwasp.co.uk/WindowsFormsStartParams.aspx) – mveith Nov 12 '12 at 18:46
  • It seems to work perfectly now. I'll paste the code of an excample application below. Many thanks. – weiszam Nov 13 '12 at 19:57
2

If you open a ddd.txt with your application (the exe file), then the string[] Args will have two items: the program's path itself and the ddd.txt path. The following sample code shows you how you put your ddd.txt file in a textBox on Form1. Many thanks everyone for the help.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static class Environment
        {
        }
        public Form1()
        {
            InitializeComponent();
        }

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] args = System.Environment.GetCommandLineArgs();
        string filePath = args[0];
        for (int i = 0; i <= args.Length - 1; i++)
        {
            if (args[i].EndsWith(".exe") == false)
            {
                textBox1.Text = System.IO.File.ReadAllText(args[i],
                Encoding.Default);
            }
        }
    }
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        string[] args = System.Environment.GetCommandLineArgs();
        string filePath = args[0];
    }


}
public sealed class StartupEventArgs : EventArgs
{

}

}
weiszam
  • 187
  • 4
  • 14
0
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();            


// Set filter for file extension and default file extension 

dlg.DefaultExt = ".kkk"; 

dlg.Filter = "KKK documents (.kkk)|*.kkk"; 
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122