Solved

Convert C# to VB.NET

Posted on 2015-01-18
12
262 Views
Last Modified: 2015-01-24
Hey guys, can someone help me convert the below code from C# to VB.NET?

I've tried a few online conversion tools but they don't seem to do it correctly.

' Get a ScheduledTasks Object for the computer named "DALLAS"
Dim st As New ScheduledTasks("\\DALLAS")
 
' Get an array of all the task names
Dim taskNames As String() = st.GetTaskNames()
 
' Open each task, write a descriptive String to the console
For Each name As String In taskNames
	Dim t As Task = st.OpenTask(name)
	Console.WriteLine("  " + t.ToString())
	t.Close()
Next
 
' Dispose the ScheduledTasks Object to release COM resources.
st.Dispose()

Open in new window


Or, if it's easier, what I'm trying to do is get a list of tasks from my local machine.
0
Comment
Question by:amaru96
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40556843
ScheduledTasks st = new ScheduledTasks(@"\\DALLAS");
string[] taskNames = st.GetTaskNames();

foreach (string name in taskNames)
{
    Task t = st.OpenTask(name);

    Console.WriteLine("  " + t.ToString());
    t.Close();
}

st.Dispose();

Open in new window

0
 
LVL 1

Author Comment

by:amaru96
ID: 40556850
That's still C#, I need it in VB
0
 
LVL 40
ID: 40556878
The "below code" is in VB. How do you want us to convert it from C# if we do not have the C# code?

Ask a meaningful question, and you might get a meaningful answer.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:amaru96
ID: 40556902
Sorry, my mistake, I pasted my attempted converted VB code. Apologies.

Below is the C# code, which is effectively what kaufmed provided.
// Get a ScheduledTasks object for the computer named "DALLAS"
ScheduledTasks st = new ScheduledTasks(@"\\DALLAS");

// Get an array of all the task names
string[] taskNames = st.GetTaskNames();

// Open each task, write a descriptive string to the console
foreach (string name in taskNames) {
    Task t = st.OpenTask(name);
    Console.WriteLine("  " + t.ToString());
    t.Close();
}

// Dispose the ScheduledTasks object to release COM resources.
st.Dispose();

Open in new window


However, my inadvertently pasted VB code doesn't seem to work either. It doesn't like the first line and wants to convert ScheduledTasks to Scheduled_Tasks. But doing that creates other problems and makes other lines underlined too.

Again, sorry for the mixup.
0
 
LVL 40
ID: 40556928
What type of application are you trying to develop?

ScheduledTask (singular) is part of the Microsoft.Phone namespace and works only on phone applications.

ScheduledTasks (plural) that is used in your code is part of the Microsoft.Office.Excel.Server.Addins namespace and works only when you are developing an Addin for Excel.

Be careful when you get code somewhere. Make sure that it is created for the type of application you are working on. What is available for Phone or and AddIn might not be available for a standard Windows or Web application. And vice-versa. When you get an error that you do not understand, check the documentation for the classes/methods/properties involved in order to make sure that they are available in your environment.

Scheduled_Tasks is not part of the Framework. Could it be that you have something else in your application that has that name. If IntelliSense does not see ScheduleTasks in your environment but see Scheduled_Tasks, it will try to help you by changing it, thinking that you made a typo error.

This being said, "It doesn't like the first line" is like saying "I have a problem, do you want to solve it", with nothing else. What is the problem? What is the exact message that you get. When a line gets underlined, you can hover over it to get a message that is more meaningful than simply "it doesn`t like it".
0
 
LVL 1

Author Comment

by:amaru96
ID: 40556938
I'm trying to write a small app for windows, which lists all the scheduled tasks on the local machine, along with the description and execution date\time. I didn't realise the ScheduledTask is part of the Microsoft.Phone namespace.

From the sound of things, I think I'm using the wrong commands.

After a bit more searching I think the below code is more appropriate, but I'm not sure what to do with the "do something here" part of the code...

Private Sub EnumAllTasks()
        Using ts As New TaskService()
            EnumFolderTasks(ts.RootFolder)
        End Using
    End Sub

    Private Sub EnumFolderTasks(ByVal fld As TaskFolder)
        For Each task As Task In fld.Tasks
            ActOnTask(task)

        Next task
        For Each sfld As TaskFolder In fld.SubFolders
            EnumFolderTasks(sfld)
        Next sfld
    End Sub

    Private Sub ActOnTask(ByVal t As Task)
        'do something here
    End Sub

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 200 total points
ID: 40557027
There is not class in .Net to deal with the Windows Task Scheduler, so this code references a Library that comes from somewhere outside of .NET.

There are a lof of such libraries available on different sites, and they all use the same names for their classes, so it is impossible to know which one you use only by look at your piece of code.

Do you have Declare or DllImports statements somewhere else in the example that you found? We would need them.

Did you reference a dll that contained the definitions for theses classes. If so, what is that dll and where did you get it?

If you type t followed by a dot in the ActOnTask method, do you get a list of properties and/or methods available on the Task object? If so, you should find properties that give you the information that you need.
0
 
LVL 1

Author Comment

by:amaru96
ID: 40557045
I have the following imports:
Imports Microsoft.Win32.TaskScheduler
Imports System.IO
Imports System.Threading

And I'm referencing the following DLLs:
Microsoft.Win32.TaskScheduler
Microsoft.Win32.TaskScheduler.resources
Microsoft.Win32.TaskSchedulerEditor

And yes, I do get some other methods when I type t.

There are quite a few so I'll try and work my way through.

Thanks for your help.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 300 total points
ID: 40557117
It looks like you're trying to use code that makes use of this library:

http://taskscheduler.codeplex.com/

To get the scheduled tasks from the local machine, you could do something like:

Imports Microsoft.Win32.TaskScheduler

Module Module1

    Sub Main()
        Using service As New TaskService()
            For Each tsk As Task In service.AllTasks()
                Console.WriteLine("Task '{0}' last ran at {1} and will run again at {2}", tsk.Name, tsk.LastRunTime, tsk.NextRunTime)
            Next
        End Using

        Console.ReadKey()
    End Sub

End Module

Open in new window

0
 
LVL 1

Author Comment

by:amaru96
ID: 40557237
Kaufmed , that's right I am trying to use that library.

I tried using your code but it doesn't like:
For Each tsk As Task In service.AllTasks()

I get "AllTasks is not a member of Microsoft.win32.taskscheduler.taskservice"

While I'm asking, any chance of including the description of the task?

Thanks, appreciate your help.
0
 
LVL 1

Accepted Solution

by:
amaru96 earned 0 total points
ID: 40558926
OK, so I got it working.

Attaching the code in case anyone else finds it handy.

Using ts As New TaskService()
            Dim tf As TaskFolder = ts.RootFolder
            For Each t As Task In tf.Tasks
                Try
                    MsgBox("Task Name: " & t.Name)
                    MsgBox("Task Description: " & t.Definition.RegistrationInfo.Description)
                    If t.LastRunTime = "12:00 AM" Then
                        MsgBox("Task last run: Never")
                    Else
                        MsgBox("Task last run: " & t.LastRunTime)
                    End If
                    MsgBox("Task next run: " & t.NextRunTime)
                Catch
                End Try
            Next t
        End Using

Open in new window


Kaufmed, you got me the closest so thanks.

Also thanks to Jacques for clarifying things for me.

Appreciate the help.
0
 
LVL 1

Author Closing Comment

by:amaru96
ID: 40567970
Proposed options did not provide complete solution.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question