Convert C# to VB.NET

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.
LVL 1
amaru96Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
amaru96Connect With a Mentor Author Commented:
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
 
käµfm³d 👽Commented:
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
 
amaru96Author Commented:
That's still C#, I need it in VB
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
amaru96Author Commented:
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
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
amaru96Author Commented:
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
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
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
 
amaru96Author Commented:
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
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
amaru96Author Commented:
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
 
amaru96Author Commented:
Proposed options did not provide complete solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.