Solved

Convert C# to VB.NET

Posted on 2015-01-18
12
249 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
  • 7
  • 3
  • 2
12 Comments
 
LVL 74

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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 74

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now