Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am creating a notepad in C#, but I got a little problem
I can open files from the programme by clicking the open file button, when I click a file in Windows Explorer and opens it with my notepad I see it's an empty document whereas when I open the same file With Windows Notepad I see some text.
How can I make my application open up the file directly from its location like Windows Notepad?
Posted

It sounds like you're not actually reading the file. To do so, you need to get the path to the file. The easiest way to do that is to use the first parameter passed to the main method of your program. As a very simple example:

namespace Foo
{
  public static class Bar
  {
    public static int Main ( string[] args )
    {
      if ( args.Length > 0 )
      {
        System.Console.WriteLine ( args [ 0 ] ) ;

        System.IO.FileInfo fi = new System.IO.FileInfo ( args [ 0 ] ) ;

        if ( fi.Exists )
        {
          System.Console.WriteLine ( fi.OpenText().ReadToEnd() ) ;
        }
      }

      return ( args.Length ) ;
    }
  }
}
 
Share this answer
 
[After OP's clarification]

Now I see: the problem is how to work with the Windows Shell.

First of all, here is how it works: Explorer is just one of the applications using Windows Shell. It could be any other application (alternative or 3rd-party file manager, or something else, correctly using the Shell API). What it does is this: it executes your application with some command-line parameters. By default, for format is "yourApplication.exe some_file_name". To start with, let's assume you simply created the application and then used "Open with" context menu item, and then selected your application having the file name selected.

Then your application is started, and then the file name is passed to it in a command line. You can get its name when it is passed to your Main function (entry point) or using the property System.Environment.CommandLine. Please see:
http://en.wikipedia.org/wiki/Main_function#C.23[^],
http://msdn.microsoft.com/en-us/library/system.environment.commandline.aspx[^].

Now, if you also register your file type to be used with your application by default, you can get to additional features:
  1. your application can be loaded by default on click of the data file;
  2. your application can introduce more complex command line with any number of additional features, as the command line format is also registered in the system Registry.


This topic is more advanced. Most usually, registration of file types is used when you also deploy your application with the setup (based on MSI and Windows Installer API). The setup you produce should take care about this registration. I think the topic is too big to be explained in a single Quick Answer. You should choose some installation framework and learn it.

If you want to make your application self-registering the file type(s), please see, for example: http://stackoverflow.com/questions/69761/how-to-associate-a-file-extension-to-the-current-executable-in-c-sharp[^].

If your command-line parsing needs to be a bit more advances or even very complex, I could suggest using my library, which is also very easy to use. Please see my CodeProject article: Enumeration-based Command Line Utility[^].

In this article, I also reference yet another, more complex solution; please see.

Good luck,
—SA
 
Share this answer
 
v2
No, you are not creating a "notepad". "Notepad" is a Microsoft application bundled with all versions of Windows, you cannot possibly create it as it is already created. And, I must say, it is a really rudimentary application. Prior to Windows 7 version, it was so bad in features that it was hardly usable. You can create a different text editor, but it won't be "Notepad".

Now, back to your question. It does not look correct just because "to open a file directly from it's location" is probably the only option. How it can be done not directly? Or not "from its location". Its looks like a bomb which has a special advanced feature: ability to aim exactly in the epicenter of the explosion :-).

You can use a standard file open dialog to allow the user to select an existing file, and then read the file using System.IO.StreamReader. Another, simpler option is to use the methods System.IO.File.ReadAllLines. Please see:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^].

—SA
 
Share this answer
 
Comments
sid2x 14-Jul-13 0:49am    
Sorry but your answer didn't satisfy my question, I want my application to open the file not through the openfiledialog but through the Windows Explorer like other applications who open up the editor from the files itself'
.
Sergey Alexandrovich Kryukov 14-Jul-13 0:53am    
You did not explain that. Why didn't you say that in first place. Again, the problem is the formulation of your question. I'll answer...
—SA
Sergey Alexandrovich Kryukov 14-Jul-13 1:10am    
Answered in detail, please see Solution 3.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900