private void OnSaveAsSas(object sender, EventArgs e) { // Save the content of the friends data as a SAS program, // to the file system SaveFileDialog dlg = new SaveFileDialog(); if (me != null) { dlg.FileName = string.Format("Friends_of_{0}", me.Name.Replace(' ', '_')); } dlg.Filter = "SAS programs (.sas) | *.sas"; dlg.OverwritePrompt = true; dlg.CheckPathExists = true; if (DialogResult.OK == dlg.ShowDialog(this)) { try { // create and save the file string program = FacebookToSas.GetSasProgram(me, Friends, Schools, Statuses, FriendPairs, chkUnicode.Checked); System.IO.File.WriteAllText(dlg.FileName, program); MessageBox.Show(string.Format("Your SAS program has been saved to:\n\n {0}\n\nYou can open your program in SAS Enterprise Guide or SAS display manager and run PROCs on your Facebook friends!", dlg.FileName)); System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(dlg.FileName)); } catch { MessageBox.Show(string.Format("Unable to save file to: {0}", dlg.FileName)); } } }
public string GetSasProgram() { return(FacebookToSas.GetSasProgram(me, Friends, Schools, Statuses, FriendPairs, chkUnicode.Checked)); }
static public string GetSasProgram(FbFriend me, Dictionary <string, FbFriend> friends, List <FbSchool> schools, List <FbStatus> statuses, List <string> friendPairs, bool preserveEncoding) { StringBuilder sb = new StringBuilder(); sb.AppendLine(sasOdsPaths); sb.AppendFormat("%let myFacebookName = {0};\n", me.Name); sb.Append(sasFriends); foreach (string key in friends.Keys) { sb.AppendLine(string.Format("\"{0}\",\"{1}\"", friends[key].UserID, FixUpDatalinesString(friends[key].Name))); } sb.AppendLine(";;;;"); sb.AppendLine("run;"); sb.AppendLine(); sb.Append(sasFriendDetails); foreach (string key in friends.Keys) { sb.AppendLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",{6},{7},{8}", friends[key].UserID, FixUpDatalinesString(friends[key].LastName), FixUpDatalinesString(friends[key].FirstName), friends[key].Gender, FixUpDatalinesString(friends[key].Link), friends[key].RelationshipStatus, friends[key].BirthdayYear, friends[key].BirthdayMonth, friends[key].BirthdayDay )); } sb.AppendLine(";;;;"); sb.AppendLine("run;"); sb.AppendLine(); sb.Append(sasStatus); foreach (FbStatus s in statuses) { sb.AppendLine(buildStatusRecord(s)); } sb.AppendLine(";;;;"); sb.AppendLine("run;"); sb.AppendLine(); sb.Append(sasSchools); foreach (FbSchool s in schools) { sb.AppendLine(buildSchoolRecord(s)); } sb.AppendLine(";;;;"); sb.AppendLine("run;"); if (friendPairs.Count > 0) { sb.Append(buildFriendMatrix(friendPairs)); } sb.Append(FacebookToSas.ReadFileFromAssembly("SAS.FbFriends.FbReport.sas")); if (!preserveEncoding) { Encoding enc = Encoding.ASCII; byte[] encoded = enc.GetBytes(sb.ToString()); return(enc.GetString(encoded)); } else { return(sb.ToString()); } }