我找到了VBScript代码来检索用户的Outlook状态并显示在列表框中。我需要它是C#,并且没有在线转换工具:
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objRecipient = objNameSpace.CreateRecipient("kenmyer")
strFreeBusyData = objRecipient.FreeBusy(#11/11/2014#, 60)
dtmStartDate = #11/11/2014#
For i = 1 to Len(strFreeBusyData) Step 24
Wscript.Echo dtmStartDate
strDay = Mid(strFreeBusyData, i, 24)
For x = 1 to 12
If x = 1 Then
strTime = "12 AM: "
Else
strTime = x - 1 & " AM: "
End If
intFreeBusy = Mid(strDay, x, 1)
If intFreeBusy = 1 Then
strFreeBusy = "Busy"
Else
strFreeBusy = "Free"
End If
Wscript.Echo strTime & strFreeBusy
Next
For x = 13 to 24
If x = 13 Then
strTime = "12 PM: "
Else
strTime = x - 13 & " PM: "
End If
intFreeBusy = Mid(strDay, x, 1)
If intFreeBusy = 1 Then
strFreeBusy = "Busy"
Else
strFreeBusy = "Free"
End If
Wscript.Echo strTime & strFreeBusy
Next
Wscript.Echo
dtmStartDate = dtmStartDate + 1
If dtmStartDate > #11/12/2014# Then
Exit For
End If
Next
最终结果在约会列表框下的C#应用程序中看起来像这样:
11/11/2014
8 AM: Free
9 AM: Free
10 AM: Free
11 AM: Free
12 PM: Free
1 PM: Free
2 PM: Free
3 PM: Free
4 PM: Free
5 PM: Busy
6 PM: Free
到目前为止我所拥有的:
private void userschedule()
{
Outlook.Application oApp = new Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace ns = oApp.Application.Session;
Outlook.Recipient recipient = ns.CreateRecipient(username.Text);
DateTime datetime = DateTime.Now;
string freeBusy = recipient.AddressEntry.GetFreeBusy(datetime, 60, true);
string status = freeBusy.Substring(0, 1);
textBox1.Text = status;
}
如何将其转换为C#?另一组代码类似:
For i = 1 to Len(strFreeBusyData) Step 24
Wscript.Echo dtmStartDate
strDay = Mid(strFreeBusyData, i, 24)
For x = 1 to 12
If x = 1 Then
strTime = "12 AM: "
Else
strTime = x - 1 & " AM: "
End If
intFreeBusy = Mid(strDay, x, 1)
If intFreeBusy = 1 Then
strFreeBusy = "Busy"
Else
strFreeBusy = "Free"
End If
Wscript.Echo strTime & strFreeBusy
Next
新编码(我的问题的最终答案)
private void userstatus()
{
{
{
Outlook.Application oApp = new Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace ns = oApp.Application.Session;
Outlook.Recipient recipient = ns.CreateRecipient(username.Text);
DateTime datetime = DateTime.Now; // gets current date and time
DateTime startDate = DateTime.Today; //gets todays date
startDate.AddHours(8); //Skip to 8 am
string freeBusy = recipient.AddressEntry.GetFreeBusy(startDate, 60, true);
textBox1.Text = freeBusy;
foreach (char c in freeBusy) //iteration process
{
if (startDate.Hour == 0) //start at 12 AM
Contacts.Items.Add(startDate.ToString("dd/MM/yyyy"));
if (8 <= startDate.Hour && startDate.Hour <= 18) // 8AM to 6PM inclusive
{
listBox1.Items.Add(
String.Format(
"{0}: {1}",
startDate.ToString("hh tt"),
c == '0' ? "Free" : "Busy"));
}
startDate = startDate.AddHours(1);
if (startDate.Date > DateTime.Today)
break; // stop once we get to tomorrow.
}
}
}
private void button5_Click(object sender, EventArgs e)
{
userstatus();
}
对于我获得空闲/忙碌状态的用户,freeBusy
字符串看起来像这样。
我通过这样做得到了以下内容:
string freeBusy = recipient.AddressEntry.GetFreeBusy(datetime, 60, true);
textBox1.Text = freeBusy;
000000002200000000000000000000002200000000000000333333333333333333333333000000000000000000000000000000000000000000000000000000002000001100000000000000002000001000000000000000002000000000000000000000002022200000000000000000002000001000000000000000000000000000000000000000000000000000000000333333333333333333333333000000002001101000000000000000002000000000000000000000002000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000002000001100000000000000002000000000000000000000002000000000000000000000002020000000000000000000002000001000000000000000000000000000000000000000000000000000000000000000002000001100000000000000002000000000000000000000002000000000000000000000002000000000000000
答案 0 :(得分:3)
以下针对VBScript中的循环和逻辑
For i = 1 to Len(strFreeBusyData) Step 24
Wscript.Echo dtmStartDate
strDay = Mid(strFreeBusyData, i, 24)
...
Next
将等同于C#
中的以下内容for(int i = 0; i < strFreeBusyData.Length; i+= 24)
{
Console.WriteLine(dtmStartDate);
var strDay = strFreeBusyData.Substring(i, 24);
...
}
VBScript和C#之间的一个重要区别是VBScript在处理字符串时往往是1 - 而C#是基于0的。具体来说,Mid
函数的第一个参数需要索引到基于1的字符串(1表示从第一个字符开始),而string.Substring
的第一个参数应该是基于0的( 0意味着从第一个角色开始。)
修改强>
在睡觉之后,在C#中处理此问题的更好方法如下:
DateTime startDate = DateTime.Today;
string freeBusy = recipient.AddressEntry.GetFreeBusy(datetime, 60, true);
foreach(char c in freeBusy)
{
if(startDate.Hour == 0)
Console.WriteLine(startDate.ToString("dd/MM/yyyy"));
Console.WriteLine(
"{0}: {1}",
startDate.ToString("hh tt"),
c == '1' ? "Busy" : "Free");
startDate = startDate.AddHours(1);
}
这将得到今天有一个DateTime
的日期,但是时间是凌晨12点。然后当您遍历freeBusy
字符串中的每个字符时,当小时为0或12时,您将输出格式为dd / MM / yyyy的startDate
。然后你用12小时时间(hh)和AM / PM指示符(tt)写出小时,如果字符是&#34; 1&#34;则写出忙碌一词。或者免费。然后将startDate
增加一个小时。这避免了VBScript代码所具有的不必要的内部for循环。
修改强>
根据您更新的代码,您应该更改此
listBox1.Items.Add("{0}: {1}") ;
listBox1.Items.Add(startDate.ToString("hh tt") + " " + (c == '1' ? "Busy" : "Free"));
listBox1.Items.Add(c == '1' ? "Busy" : "Free");
startDate.AddHours(1);
到这个
listBox1.Items.Add(
string.Format(
"{0}: {1}",
startDate.ToString("hh tt"),
c == '1' ? "Busy" : "Free");
startDate = startDate.AddHours(1);
&#34; {0}:{1}&#34;用于格式化,Console.WriteLine
具有带格式字符串的重载。日期没有变化,因为DateTime
是不可变的,你必须捕获AddHours
的返回值,这是我的坏事。
修改强>
此处的代码仅显示今天的项目,且仅在上午8点到下午6点之间显示
foreach (char c in freeBusy) //iteration process
{
if (startDate.Hour == 0 ) //start at 12 AM
Contacts.Items.Add(startDate.ToString("dd/MM/yyyy"));
if(8 <= startDate.Hour && startDate.Hour <= 18) // 8AM to 6PM inclusive
{
listBox1.Items.Add(
String.Format(
"{0}: {1}",
startDate.ToString("hh tt"),
c == '1' ? "Busy" : "Free"));
}
startDate = startDate.AddHours(1);
if(startDate.Date > DateTime.Today)
break; // stop once we get to tomorrow.
}
如果需要,您可以在下午6点后优化此功能。它也可以缩短为以下
DateTime startDate = DateTime.Today; //gets todays date
string freeBusy = recipient.AddressEntry.GetFreeBusy(datetime, 60, true);
Contacts.Items.Add(startDate.ToString("dd/MM/yyyy"));
startDate = startDate.AddHours(8); //Skip to 8 am
foreach (char c in freeBusy.Skip(8).Take(11)) // skip to 8AM and only go to 6PM
{
listBox1.Items.Add(
String.Format(
"{0}: {1}",
startDate.ToString("hh tt"),
c == '0' ? "Free" : "Busy"));
startDate = startDate.AddHours(1);
}
修改强>
更改
c == '1' ? "Busy" : "Free"
到
c == '0' ? "Free" : "Busy"