Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, how do i detect if my program is opened trough a file

For example, if i open a txt it opens in notepad

How do i detect the opening in my program?
Posted

C#
public bool IsFileinOpen(FileInfo file)
{
     FileStream stream = null;
 
     try
     {
         stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
     }
     catch (IOException)
     {
         //the file is unavailable because it is:
         //still being written to
         //or being processed by another thread
         //or does not exist (has already been processed)
         return true;
     }
     finally
     {
         if (stream != null)
         stream.Close();
     }
     return false; 
}

FileInfo fileObj = new FileInfo("C:\\yourfile.txt");
bool iSOpen = IsFileinOpen(fileObj);

If this method returns false then file is not used otherwise it is used in any other program.
 
Share this answer
 
v3
Comments
kylai12n 9-Oct-15 4:10am    
I dont really understand this, how would i use this?

IsFileinOpen(OpenedFile); ?

I need it to detect like

When i open example1.sssd

It shows it opened with that file

And with example2.sssd
(sssd) is a file extention in my case
[no name] 9-Oct-15 4:26am    
Try like below:

FileInfo fileObj = new FileInfo("C:\\yourfile.txt");
IsFileinOpen(fileObj);

Here I have provided the text file name, you need to change as per your need.
That's a pretty advanced topic, you need to implement a hook, and you have to do it with native code. See, for instance: "API hooking revealed"[^].
 
Share this answer
 
Comments
kylai12n 9-Oct-15 4:20am    
This does not really look like the solution to be honest,

Basicly: i can run the files and then it starts my program

BUT i want the program to know its run by a file

Maby some parameters?
CPallini 9-Oct-15 6:38am    
Files doesn't run. Programs runs. Programs aren't run by files.
Maybe I didn't get OP problem, but your comment is a bit obscure to me.
kylai12n 9-Oct-15 12:12pm    
For example:

I have a program called: NotNotepad.exe
NotNotePad opens when you open a .nottxt file

How would my program be able to know it opened that file?

For example (This is not a real script)

Form1_Load:
if(File.OnRun)
{
MessageBox.Show("Opened with a .nottxt file");
}

Do you understand?
CPallini 9-Oct-15 12:29pm    
Yes, I've got it. You have to process the arguments passed to your application. I see you already found the solution.
kylai12n 9-Oct-15 12:39pm    
Yeah thanks for your time tough
I managed to fix it myself

Here's the solution:

Add under (Dont remove)

public Form1()
{
InitializeComponent();

}

--->

public Form1(string caller)
{
InitializeComponent();
//Caller = the path to the file.
}

Program.cs:
static void Main(string [] args)


And:

C#
if (args.Count() == 1)
    Application.Run(new Form1(args[0]));
else
    Application.Run(new Form1());
 
Share this answer
 

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