0

In my project, I am trying to open a text file. Well the below code works but when the user click the button again and again, many files are being opened. (which I dont want)

System.Diagnostics.Process.Start(filePath);

I also tried this, File.Open and File.OpenText which are not opening the text file and also not showing any error (tried with try catch block)

File.Open(filePath); (or)
File.OpenText(filePath); (or)
FileStream fileStream = new FileStream(filePath, FileMode.Open);

I also tried this: (ERROR : Cannot be accessed with instance reference qualify with a type name instead)

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.Start(filePath);  /*red scribbles here*/
proc.WaitForExit();

How to show only one instance of the Text file(.txt). Am I doing something wrong in my attempts? please suggest.

EDIT:

I want to open other text files afterwards but not the same and also the application should be accessible after opening a text file(or many). I have only one form.

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • 2
    Define what you mean by "Open". Do you want to open it in Notepad? – Knaģis Nov 14 '12 at 10:28
  • next questions: 1) do you need to open multiple text files at the same time (one window for each file); 2) does your application need to keep doing something while the text file is open? – Knaģis Nov 14 '12 at 10:32
  • 1
    Hmm.. just disable the button after first click? – Shadow The GPT Wizard Nov 14 '12 at 10:33
  • the text files should open multiple but unique.... my application need to keep doing something while the text file is open :) – Mr_Green Nov 14 '12 at 10:33
  • @ShadowWizard no I want to open other text files (not same) afterwards – Mr_Green Nov 14 '12 at 10:34
  • 1
    @Mr_Green So assuming you want to open each file only once, add class member that store the files already opened (e.g. in a `List`) then when clicking the button, if the file chosen exists in that class member show alert instead of opening the file. – Shadow The GPT Wizard Nov 14 '12 at 10:36
  • Seems you have no way but reflecting on running processes, fishing notepads and see if you can get the filename somehow. – avishayp Nov 14 '12 at 10:37
  • You're going to have to check yourself whether a particular file has been opened by maintaining a list of already opened files. – Rik Nov 14 '12 at 10:37
  • 2
    Regarding solution to your current problem : do not open a file if it is already opened by same application. (not to your question=> check if a file is already opened) An easy way is to store filepath of each file in a `List` check your `filepath` if it does not already exists open it and add its path to list. – Sami Nov 14 '12 at 10:38
  • Okay I will do that on my own (actually I can) I need only solution for having single instance of the text with access to the application. :) – Mr_Green Nov 14 '12 at 10:39
  • @Mr_Green - see my answer - by storing the previously opened processes by file path you can make sure that each file is opened only once. – Knaģis Nov 14 '12 at 10:42
  • The whole 'maintaining a file list' isn't going to work. Users can, and will, just close notepad without letting your list know. – avishayp Nov 14 '12 at 10:42

2 Answers2

4

Create a dictionary at the form level:

public Dictionary<string, Process> OpenedProcesses = new Dictionary<string, Process>(StringComparer.OrdinalIgnoreCase);

Now change how you open the file (note the HasExited check - this is needed so that the user can close the Notepad and reopen it):

// make sure that path is always in form C:\Folder\file.txt - less chance of different 
// paths pointing to the same file.
filePath = System.IO.Path.GetFullPath(filePath);

Process proc;
if (this.OpenedProcesses.TryGetValue(filePath, out proc) && !proc.HasExited)
{
    MessageBox.Show("The file is already open!");
    // it could be possible to activate the window of the open process but that is another question on its own.
    return;
}

proc = System.Diagnostics.Process.Start(filePath);
this.OpenedProcesses[filePath] = proc;
Knaģis
  • 20,827
  • 7
  • 66
  • 80
0

Check if the file is opened

example

and then do your work as you want.

Community
  • 1
  • 1
ddarellis
  • 3,912
  • 3
  • 25
  • 53