|
Hello friends,
I have written this small piece of code to detect autorun.inf files, particularly in the C: and the D: drive:-
<br />
using System;<br />
using System.IO;<br />
class search<br />
{<br />
public static void Main()<br />
{<br />
Console.WriteLine("Autorun checker");<br />
if (File.Exists("C:/autorun.inf") || File.Exists("D:/autorun.inf"))<br />
{<br />
Console.WriteLine("Autorun Detected. Press Enter to delete it.");<br />
Console.ReadLine();<br />
File.Delete("C:/autorun.inf");<br />
File.Delete("D:/autorun.inf");<br />
Console.WriteLine("Autorun Deleted successfully.");<br />
Console.ReadLine();<br />
}<br />
else<br />
{<br />
Console.WriteLine("No Autorun Detected. Press Enter to exit.");<br />
Console.ReadLine();<br />
}<br />
}<br />
}<br />
Now, if no autorun files are detected in C:/ or D:/ drive, the program exits displaying "No Autorun Detected. Press Enter to Exit." But if detected, it successfully deletes that autorun file but throws a runtime exception. Suppose, it has detected an autorun.inf file in the C:/ drive, it will delete that file but throw some kind of exception like this:
File.Delete("D:/autorun.inf");
<br />
IOException was unhandled. The device is not ready
Can anyone help me out! I am new to .NET programming and dont posses that much knowledge about Exception handling.
Hope to hear from you guys soon,
Rajdeep.NET
|
|
|
|
|
This may be a silly question, but is your D: drive by any chance a CD or DVD drive? If so, then deleting files may not be too easy...
Oh, and PLEASE, use the code block button - it makes things easier to read.
If what you are trying to do is handle the error cleanly:
using System;
using System.IO;
class search
{
public static void Main()
{
Console.WriteLine("Autorun checker");
if (File.Exists(@"C:\autorun.inf") || File.Exists(@"D:\autorun.inf"))
{
Console.WriteLine("Autorun Detected. Press Enter to delete it.");
Console.ReadLine();
try
{
File.Delete(@"C:\autorun.inf");
}
catch (IOException ex)
{
Console.WriteLine("Unable to delete C:\\autorun.inf:\n{0}", ex);
}
try
{
File.Delete(@"D:\autorun.inf");
}
catch (IOException ex)
{
Console.WriteLine("Unable to delete D:\\autorun.inf:\n{0}", ex);
}
Console.WriteLine("Autorun Deleted successfully.");
Console.ReadLine();
}
else
{
Console.WriteLine("No Autorun Detected. Press Enter to exit.");
Console.ReadLine();
}
}
}
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Hey Griff,
Thanks for correcting the code man. And yes, I remember to post the codes in the code block in my further posts.
Thanks once again,
Rajdeep.NET
|
|
|
|
|
Answer[^]
My failometer is detecting vast quantities of FAIL!
"Its SQL - hardly programming..." (Caslen)
|
|
|
|
|
Don't you think its about time you begged, borrowed or stole a C# Primer Book and worked through it?
You really should be fed up with posting such elementary mistakes, I know I am.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hear hear.
|
|
|
|
|
Hello,
I am writing a small console application which will accept user age and name under the Public void Accept_Details() function. Now I want that, on the basis of the input provided by the user in the public void Display_Details() function, he be shown the desired output. Like this:
Console.WriteLine("Enter your choice. Type 'age' to view user age or 'name' to view user name);
Now, if the user enters 'age', he will be shown this:
Console.WriteLine("User Age is: {0}", age);
Similar will be the case, if the user enters 'name'. How to do that?
Please help me out,
sorry for my poor english,
Rajdeep.NET
|
|
|
|
|
Um . . . take a look at the last question you just posted. It shows you how to get input from the user and how to make an if statement to write different output to the console.
or did i misunderstand what you need help with?
|
|
|
|
|
Seriously?? How about an if statement checking what the user entered.
Pick up a book on C# for beginners and work through it.
|
|
|
|
|
on a simple window form, there is a small panel, along with button and groupbox inside the panel.
When I change the main window form color (i.e. this.BackColor = Color.Red) and the main form does turn red, and also the panel and groupbox were also changed to red. HUH?
I just wanted the main form background color to be red, while the inside of the panel (and the panel itself) to remain unchanged.
How do I do that? I tried goggling and turns out no luck.
HELP!
|
|
|
|
|
because the backcolor properties for the panel and groupbox were not initialised.
If you set their backcolors first then change the forms backcolor they will not change.
|
|
|
|
|
hmm interesting... thanks and tried it and does work.
I have not realized this.
Thanks a heap!
|
|
|
|
|
FYI The BackColor property, along with several others, is what is called an Ambient Property. This means that it gets its value from its parent control if it has not been changed first.
Have a Google for ambient properties c# to get more details.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Well if you want that I can give you little source code wish that help you : I give this code here in vb 2005 it's almost same in c# . dont know where you want to use visual basic or c#
Private Sub lblbgcl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblbgcl.Click
Dim mybackcolor As New ColorDialog()
If mybackcolor.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.BackColor = mybackcolor.Color
Me.MenuStrip1.BackColor = nothing
'' Write here what you want
Me.Groupbox1.backcolor=nothing
End If
End Sub
|
|
|
|
|
|
Don't post the same question in multiple forums. It's considered rude and will not get you an answer any faster.
|
|
|
|
|
I cannot find much on this.. but I am looking to get the Department and Company field from active directory... here is what I have:
PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, "********");
UserPrincipal findUser = UserPrincipal.FindByIdentity(adPrincipalContext, Name);
return findUser.EmailAddress;
Now I can get the email address just fine.. but I do not see anything for UserPRincipal in getting the Department and Company fields?
|
|
|
|
|
Hello;
I'm building an application in winforms c# 3.0. I want to disable the mouses's right button click when the application is running on all the system (in order to prevent copy/paste).
I succeeded in blocking the keyboard keys but not the mouse button. Can you figure out why the code for the mouse does not work?
The problem comes from LowLevelMouseProc()
Thanks for help.
#region
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
#endregion
namespace GlobalHook
{
public class UserActivityHook
{
private const int VmRButton = 0x02;
private const int VkLControl = 0xA2;
private const int VkRControl = 0xA3;
private const int VkControl = 0x11;
private const int VkSnapshot = 0x2C;
private const int WhKeyboardLl = 13;
private const int WhMouseLl = 14;
private const int WmKeydown = 0x0100;
private const int WmMousedown =0x0201;
private static IntPtr kbh_Handle;
private static IntPtr kbh_Handle1;
private static HookProc KeyboardHookProcedure;
private static HookProc MouseHookProcedure;
private int HMouseHook;
private int HKeyboardHook;
#region Windows Function Imports
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
#endregion
#region Windows constants
#endregion
~UserActivityHook()
{
Stop(true, true, false);
}
public void Start()
{
if (HMouseHook == 0)
{
MouseHookProcedure = new HookProc(LowLevelMouseProc);
HMouseHook = SetWindowsHookEx(WhMouseLl, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
if (HMouseHook== 0)
{
int errorCode = Marshal.GetLastWin32Error();
Stop(true, false, false);
throw new Win32Exception(errorCode);
}
}
if (HKeyboardHook == 0)
{
KeyboardHookProcedure = new HookProc(LowLevelKeyboardProc);
HKeyboardHook = SetWindowsHookEx(WhKeyboardLl, KeyboardHookProcedure,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
0);
if (HKeyboardHook == 0)
{
int errorCode = Marshal.GetLastWin32Error();
Stop(false, true, false);
throw new Win32Exception(errorCode);
}
}
}
public void Stop()
{
Stop(true, true, true);
}
public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook, bool throwExceptions)
{
if (HMouseHook != 0 && uninstallMouseHook)
{
int retMouse = UnhookWindowsHookEx(HMouseHook);
HMouseHook = 0;
if (retMouse == 0 && throwExceptions)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}
}
if (HKeyboardHook != 0 && uninstallKeyboardHook)
{
int retKeyboard = UnhookWindowsHookEx(HKeyboardHook);
HKeyboardHook = 0;
if (retKeyboard == 0 && throwExceptions)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}
}
}
private static int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode < 0)
{
CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
return 0;
}
if (wParam == WmKeydown)
{
IntPtr kbdll = lParam;
Kbdllhookstruct kbdllstruct = (Kbdllhookstruct) Marshal.PtrToStructure(kbdll, typeof (Kbdllhookstruct));
if ((kbdllstruct.VkCode == VkSnapshot) || (kbdllstruct.VkCode == VkControl) || (kbdllstruct.VkCode == VkLControl) || (kbdllstruct.VkCode == VkRControl))
return -1;
}
return CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
}
private static int LowLevelMouseProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode < 0)
{
CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
return 0;
}
if (wParam == WmMousedown)
{
IntPtr msdll = lParam;
MouseLlHookStruct msdllstruct = (MouseLlHookStruct)Marshal.PtrToStructure(msdll, typeof(MouseLlHookStruct));
if (msdllstruct.MouseData == VmRButton)
return -1;
}
return CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
}
[StructLayout(LayoutKind.Sequential)]
private struct Point
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MouseLlHookStruct
{
public Point Point;
public int MouseData;
public int Flags;
public int Time;
public int ExtraInfo;
}
#region Nested type: Kbdllhookstruct
[StructLayout(LayoutKind.Sequential)]
public struct Kbdllhookstruct
{
public int VkCode;
public int ScanCode;
public int Flags;
public int Time;
public int ExtraInfo;
}
#endregion
}
}
|
|
|
|
|
teycir wrote: (in order to prevent copy/paste).
This is a lot of work just to find out your premise is flawed. You are not going to prevent all possible Copy'N'Paste possibilities just by hooking the mouse and eating the right button clicks. Besides, applications use the right button for other things other than Copy'N'Paste.
|
|
|
|
|
teycir wrote: in order to prevent copy/paste
So you're writing a piece of malware and asking for help.
Is there a good reason to prevent copy/paste? I don't think so.
Disabling the right mouse button is not going to help though.
|
|
|
|
|
Why are you trying to disable copy/paste systemwide? I can't think of a single legit reason to do so. If your requirement is to keep the user from copying data out of your application, your focus should be on changing the behavior of your application only; not affecting the entire system.
It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains.
-- Pride and Prejudice and Zombies
|
|
|
|
|
Others are right, forget this line of reasoning. As an example of another approach - you can monitor the clipboard and whenever something appears, just erase it. This will effectively disable copy/paste in the entire system. But why you need to do that is beyond me. If it's giving you thrills to write some kind of stupid malware, just do us a favor and get yourself a gf.
|
|
|
|
|
The software I'm working on is a demand of a client to prevent the theft of data from his employees when they use a software. I'm not building this for myself.
If you have no idea on how to proceed to solve the problem, please don't post your dumb comments.
|
|
|
|
|
In my defense - the idea of monitoring and erasing the clipboard wasn't that dumb, better that your hooking the mouse. Don't forget to disable the print screen as well, when you are at it.
|
|
|
|
|
Printscreen and ctrl l ctrl r ctrl keys are disabled. U should have noticed that, I mentionned it in the post and it's easy to see in u read and understood a little bit the code.
|
|
|
|
|