C# proper way for file associations - c#

I've been looking for a proper way to get file associations working on WinXP and above. The association should be replaced if it already exists. The application I've developed always run in admin mode, so rights shouldn't be a problem.'
I've come across some old posts that had some sample code, but some of them didn't work well enough and some not at all. I'm using this post to make a final desicion on the method I will attempt to use and am looking for as much feedback as possible.

I think you need to make changes to the registry as shown here
http://msdn.microsoft.com/en-us/library/cc144154%28v=vs.85%29.aspx#registration

Related

Any way to get the DNS cache entries in C#?

I've been looking for answers and couldn't quite find anything that worked for me, hopefully there's someone who can help me out with this.
I need to get the number of entries in the DNS cache of the computer. Not really even interested in the particular entries, just the total count.
This answer (Is there a way to monitor the system's DNS cache on c#?) comes close but I can't manage to download the powershell module to try to get it to work, plus it would be best if I could get a solution that worked without requiring any extra tools to be installed.
Thanks in advance.
I have managed to do it by using the PowerShell Get-DnsClientCache function.
DnsClient functions are apparently loaded by default in the non-server Windows OSs as well, unlike the dnsserver module.
Anyone interested can see an example on how to execute PowerShell scripts in the link posted in the original question.

Deploying a C# application - user form (novice)

I'm virtually a complete novice, I've tried Googling for answers and become totally confused.
Using Visual Studio 2010, I have a C# application which is an email notifier for a friend. The external (Arduino) hardware works, the main code (from a website) works but I'm sending it to her on the other side of the world to use and she is very 'non-technical' - hence the need for a 'setup form'.
I have created a form where she can enter comm port (selected from a list), username and password (all to be used by the main code), but that form should run only when the application is first installed on the PC.
At the moment it runs in VS-2010 (though I need to iron out a couple of snags), validates and hides - but I don't know how to a) store the data and make it available to the main code, b) ensure that the form only runs at setup, or c) exactly what I need to do or include to create an installable application.
Could somebody either help or direct me to some tutorials that don't assume I understand all the terminology?
I just want to create something that she can instal from a memory stick. I know it can be done and it's proababy quite simple for those who understand - I'm trying to learn but I'm no longer young and it's a struggle.
Thanks
a) store the data and make it available to the main code,
write the data on a file!
you have millions of possibilities, for isntance reading and writing a plain text file can be done with few lines of code, but if you want to encrypt your file (it may be the case if you want to store the password) you can use System.Security.Cryptography as shown in this guide
b) ensure that the form only runs at setup,
once you have written the file, then it means that the program has run already at least once, so you don't need to ask the user again (just read the data from the file)
c) exactly what I need to do or include to create an installable application.
Visual Studio already comes with the Setup project for this task. See this good guide.
From your comment and link to the code project for the Arduino, I gather that this is your first venture into writing code in C#, or very close to it. And ideally you'd like to make this as easy for your friend as possible. The best advice I can give you is not to try to run before you learn to walk. If you try to create a custom setup project and use a configuration file, which is what you are talking about doing, you may hit so many barriers that you never get to a successful end of the project. That kind of experience is discouraging and I'd hate for you to lose the drive to ever want to try another software project.
Make this initial project easy on yourself. This is not good programming practice for most situations, but if you only have one user, hard-code her configruation information for this first version. In other words, put her username, password, com port, etc directly into the main program. This eliminates the need for both the configuration, and any custom setup form. If you still want to make the whole thing configurable and versatile, do that in your next version. Custom setup is not a beginner task. It will be a lot easier to take on with the encouragement of your friend's excitement and compliments over a first version that works.

Id from Process.Start match sometimes or sometimes not

I'd like to run from C# code ANSYS Fluent exe file
And now for comparison, I'll give two examples
Process fluent = Process.Start(#"C:\Program Files\ANSYS Inc\v130\fluent\ntbin\win64\fluent.exe", #"2ddp file.jou");
Process browser = Process.Start("IExplorer.exe", "http://www.google.com");
Why is the browser.ID in C# code the same as in TaskManager? Why is fluent.ID in code different than in TaskManager ?
It's because of Fluent's characteristics or I make some mistake?
And main question: how to run Fluent and catch its ID in C# code?
It's almost certainly something to do with Fluent's characteristics. If you can't get (a relevant) PID out of Process.Start, you might need to resort starting the process, and then going into a wait-loop and attempting to retrieve the process by name (see: Process.GetProcessesByName - I think you'd pass in the EXE's name without the .exe).
Exactly how robust this solution would be depends on how well you can predict Fluent's characteristics, which is already looking quite difficult. I would definitely recommend using Process Explorer (as suggested above), as well as getting familiar with the other SysInternals tools, they give tons of insight in these cases.
Answer Daniel B is ok, but if I need launch several instances of Fluent it doesn't work. So the best solution I found is appropiate CommandLine arguments and then searching in Task Manager

How to find if app has been installed before?

Is it possible for a .NET application to leave a trace so that it can be found if the application is re-installed?
Of course, a trace that is difficult to be removed.
Create a file
Create a registry key
Create a global variable
A combination of all above
...and then check for existence the next time install is attempted.
While it is better practice to remove applications in their entirety, I assume this is for 'trial' software (one time install) purposes or a similar reason.
Well, generally, if you un-install something, you'd hope it completely removes itself, and doesn't leave a trace of the fact that it was there. Otherwise, if it doesn't, it hasn't really un-installed.
This is language-agnostic anyway.
So the answer is: Yes, but don't do that.
What problem are you really trying to solve?
Many application leave behind traces (eg in registry) to detect previous installation. However tools like RevoUninstaller can be used to completely remove those traces. One easy way of doing so is to send the machine id to your server.
I've done some Googling to find better information (with no success) but I can remember mention of something referred to as the "Manufacturers Section" of a hard drive, an area which is outside that of normal storage to which it's possible to write information that won't be lost if the drive is re-formatted. The specific product I'm remembering was AutoCAD.
The fact that you are asking for something that is "difficult to be removed" leads me to believe that you are looking for something like this?
It's unlikely that the use of this sort of technology would make you very popular with your customers, particularly with the bad feeling directed towards DRM, rootkits and the like.

Application-wide Restriction of objects, methods etc

I have a very theoretical question: Is there a way to ban the use of some methods, objects etc. inside of my application/project map in C#, .Net and/or Visual Studio?
To be more specific: I'm developing a DMS System where it should never be possible to delete files from an archive. The archived files are just files inside a Windows folder structure.
So, whenever someone tries to perform a System.IO.File.Delete() this should be forbidden. Instead I would force to use a custom-made FileDelete()-method which always ensures that the file to delete is not a file from inside an archive.
(This doesn't have to happen automatically. It's ok when there is an error/exception that informs the developer of a banned method-call.)
Another way to implement this could be to observe all calls of System.IO.File.Delete() at runtime, catch them and execute my own FileDelete()-method.
Of course these are a really theoretical questions but I would just know if there could be a way to implement this.
P.S.: I'm using C# with Visual Studio 2005. So it doesn't matter if I can realize this through my programming language or by Visual Studio (or by any other way I forgot).
Wouldn't it be simpler to control delete permissions to the archived files?
you can define methods and adorn them with declarative security attributes
http://msdn.microsoft.com/en-us/library/dswfd229.aspx
HTH
The closest I can come to a solution is to write you own System.IO.File class and keeping that in exe project. That way you'll get a ambiguity compile error that can be resolved with giving you own implementation in an alias (using File=System.IO.File, Version=[version], cultuer=[correct culture], publicKey=[public key]). If you're unsure about what to write make a break point and write something like ?typeof(System.IO.File).AssemblyQualifiedName in the immediate window.
It's not bullet proof but at least it will enforce the developer to be concious about the decision and you could even (tho I personally wouldn't do it) change the default class template to include the using directive for every class
Not for existing library functions.
For your own code, you could apply code-access-security on methods, but code running as "full trust" will breeze past this; so to check for abuse via reflection you would probably have to check the caller manually (Assembly.GetCallingAssembly) - which is painful and still not 100% robust...
There is specific file/IO permissions, but again full trust will ignore it.
I think "no" is a safer answer.
One way you could go about doing this is to create a special user account and only grant that account the permissions necessary to remove the files.
Just keep in mind that the user is in control of his computer (if he has administrative privileges ;) and while you can put some obstacles in his way there really is nothing you can do about it (and that's the way it should be).
What about writing your own FxCop rule for that case?
With such a rule it will be impossible to compile if you treat warnings as errors.

Categories