How to run a program using is extension association only. - c#

I would like to create a program that will allow a user to select a file they wish to open using an openFileDialog and then on return, my program will open that file in whatever program is associated with that file in the registry.
Apart from searching the registry for determining the location etc of the executing program, is there an simpler way as in using Process or similar?

You can still use Process.Start(filePath) for this functionality. You are not required to use a path to an executable.
For example, if you write
Process.Start("test.txt");
Windows will open test.txt in whatever program is currently associated with text files (e.g. notepad).

Related

How to find out which windows process is using a file?

I've seen several answers about using Handle or Process Monitor, but I would like to be able to find out in my code (C#) which process is locking a file.
Ex. I open two files like test.xlsx and test_1.xlsx and I want to close the only process of text.xlsx, but right whatever I find that close only my last open excel file or both files.
This only example, I have so many files of different types and I need to close from them only several.
I try to "Handle" but it needs administrative rights, and restart manager also not provide exact process id which I use from different examples.

How to raise the event while double click on any file

I want to write/capture the event when I double click on my file located in Windows folder. Assume that we have some txt file located in D:\MyTest\Example1.txt
When I double click on the file, the file should not open whereas it should fire an event/trigger/service to execute some tasks.
I tried with FileSystemWatcher in C#. But, when I double click on the file, the file is getting opened.(It should not).
A simple requirement for your better understanding given below.
When you double click on any txt file in Windows, it should invoke a simple batch file (Example: a.bat) to display the today's date.
Conditions: When you double click on the file, the .bat file should run without opening the actual txt file.
FileSystemWatcher monitor specific path for any file add/remove/modification and not for file execution.
There are different solution based on your needs :
The simplest solution is to register your program for own file extension. by double clicking on file your program got executed and you can just log it and do nothing any more or open the requested file using actual program. for example "Notepad.exe 1.txt"
Write a hook module and load it into explorer's process and watch for file execution but the chief drawback of this solution is third party apps or tricks to open a file that make no sense in your program. for example using CMD.exe or PowerShell.exe to open text file because you monitor specific program.
You can inject your hook module into all executables but it is possible to make performance issues.
The beset solution is to write MiniFilter driver to monitor file system but it need to good system, user mode, c++ and driver development knowledge.
It's possible to implement a virtual file system and control everything over it and again it need to driver development but there is good C# ports for existing libraries, the best one for C# is Dokan .Net
Describe the simplest way to implement :
Write simple program without any form like this :
static void Main(string[] args)
{
if (!args.Any())
return;
foreach (string argument in args)
{
if (Path.GetExtension(argument) != "txt")
continue;
if (Path.GetDirectoryName(argument) == #"D:\MyTest\")
{
// Call your service event here
return;
}
Process.Start("Notepad.exe", argument);
}
}
Register your program for own file extension (in your case .txt) using registry configuration. Here is Microsoft documentation and maybe this topic help you.
Now by double clicking on a .txt file your program got executed and path of the file(s) passed to your program as arguments. program check each file and if you want you can call something or notify your service using different mechanisms like Pipes or Messages or etc.
If the file is not match you want the original application called (in my sample code "nodepad.exe") and the file open normal.

Get username of user who has file open on network drive - Microsoft Office Style

I would like to add user friendly file locking to a software running under Windows (Windows 7 mostly), written in C#.
I already achieved the file locking part, by keeping the files in use "open" in the corresponding process. What I now still would like to add is recognition of the user who has a file currently open/locked.
The files being accessed lie on a mapped network drive, used by different users on different computers.
When a file is locked and a second person tries to open the file, he should be confronted with a dialog, similar to the "File in use"-dialog from the Microsoft Office programs. There, also the name of the user, currently editing the file, is displayed.
I found solutions to find out the processes, which have a certain file open (used this one: How do I find out which process is locking a file using .NET?)
and I'm also able to read the name of the user who created this process out of it. However, when opening a locked file on a network drive, the username yielded by doing it like this, is always my own one, instead of the one from the user locking the file.
Does anyone have an idea how one could achieve this? I mean Microsoft Office somehow can do this on my same PC with the same user permissions, too. I just'd like to know how...
Cheers!
Office uses a very simple technique, I'll talk about it in .NET terms. Whenever an Office app opens a document file, using FileShare.None, it also creates a hidden "lock-file" with a name that's based on the document file (say, with ".lockfile" appended). And writes Environment.UserDomainName into that file. The file is created with FileOptions.DeleteOnClose and FileShare.Read and kept open as long as the document file is open.
It closes the lock-file when the document is closed again. Using FileOptions.DeleteOnClose ensures that the lock-file disappears even when the program bombs.
When opening the file produces a locking violation, it goes looking for the lock-file and reads the user name. Easy peasy, simple to implement yourself. But can of course only work if it is one particular app that opens the file.

How to open a file using a specified third party program in C#?

so what I'm trying to do is open a file (well, actually two folders, but I figure I'll start with a single file for now) using a third party comparison tool called UltraCompare. I'm working in a C# website project in Visual Studio 2010 (Express edition). I've seen how to open a file using a different program, here: Open a file with Notepad in C#.
Problem is, this only lets you open it using the default program for that file type. But I want to open it in a specified program. For example, a text file should open in UltraCompare, not notepad. Here's the code which does this:
string textBoxContents1 = TextBox1.Text;
Process.Start(textBoxContents1);
The textbox on the webform accepts a string, in which the user types the file's full path (not the most user-friendly design I know, but I'm not sure how to allow them to browse for a folder using a GUI interface in asp.NET). The file is then passed into the Process.Start() method, which opens it using the default program for that file type.
Is there any way to modify this to make it open using UltraCompare??
You can specify the program you want to open the file in:
Process.Start("yourprogram.exe", textBoxContents1);
Update
To open two files in Ultracompare, you'd probably do something like that:
Process.Start("yourprogram.exe", "file1.txt file2.txt");
Keep in mind that the second parameter of Process.Start method are the arguments passed to the program.
I said this is probably going to work because I assumed to be very likely that Ultracompare expects 2 arguments, but this might not be the case.
Quick question: Are you trying to do this for the client machine? Hope not
And I guess it looks into the PATH variable for finding your exe

Get write access to local_machine\software in the registry for .net application

I am trying to change the friendly name of a USB sound card. I can don't it from windows as well as edit the registry location using regedit. But I get an exception when I try to open the subkey for write access.
Is there any way to get around this. For now my work around is to build the correct registry import file and run that, but I would like it much better if it could be implemented in code.
Are you running your c# application as an administrator? Only a user with administrative rights has access to write to HKLM.
The way I got around this is to write a text file in the format that can be imported into the registry and just call that file using Process.Start("edit.reg") or what ever you named your file. Works for now, only need to do this once on installation, for now.

Categories