As your code looks ltl messed up with more than one responsibility.
For Starter what you can do here is:
- Refactor your code into small code snippets and move those dependencies into another classes.
- when you will be done with first step you will be able to mock those classes using MOQ or NMock or another framework.
Let me know if you have any doubt in above points.