Tuesday, April 19, 2005

.Net Remoting



Process currentProcess = Process.GetCurrentProcess();
Process [] duplicateProcesses = Process.GetProcessesByName(currentProcess.ProcessName);
if (duplicateProcesses.Length == 1)
{
AssociateFileTypes();
TcpChannel chan = new TcpChannel(8420);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(
System.Type.GetType("InteractiveLogBook.RemoteExecute, InteractiveLogBook"),
"RemoteExecute", WellKnownObjectMode.SingleCall);
}
else
{
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
RemoteExecute obj = (RemoteExecute) Activator.GetObject(typeof(RemoteExecute),
"tcp://127.0.0.1:8420/RemoteExecute");
if (obj == null)
MessageBox.Show("Could not locate running Interactive LogBook", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
else
obj.ProcessArgs(args);
return;
}




public class RemoteExecute : MarshalByRefObject
{
public RemoteExecute()
{
}
public bool ProcessArgs(String[] Args)
{

InteractiveLogBook.FormILB.mainForm.ProcessCommandLine(Args);
return true;
}

}



Downloading a file from Sharepoint 2

// C#
public void DownloadDocument(string localFile,string remoteFile)
{
try
{
// Create the web request object
WebRequest request = WebRequest.Create(remoteFile);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Method = "GET";
// Get a web response back
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream reader = response.GetResponseStream();
FileStream fstream = new FileStream(localFile, FileMode.OpenOrCreate, FileAccess.Write);
byte [] buffer = new byte[1024];
int bytesRead = 0;
do
{
bytesRead = reader.Read(buffer,0,1024);
fstream.Write(buffer,0,bytesRead);
} while(bytesRead >0);
response.Close();
fstream.Close();
}
catch(Exception ex)
{
MessageBox.Show(host.HostForm,ex.Message,"Error Downloading",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

Uploading a file to Sharepoint 2

//C#
public void UploadDocument(string localFile, string remoteFile)
{
// Read in the local file
FileStream fstream = new FileStream(localFile, FileMode.Open, FileAccess.Read);
byte [] buffer = new byte[fstream.Length];
fstream.Read(buffer, 0, Convert.ToInt32(fstream.Length));
fstream.Close();
try
{
// Create the web request object
WebRequest request = WebRequest.Create(remoteFile);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Method = "PUT";
request.ContentLength = buffer.Length;
// Write the local file to the remote system
BinaryWriter writer = new BinaryWriter(request.GetRequestStream());
writer.Write(buffer, 0, buffer.Length);
writer.Close();
// Get a web response back
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
}
catch(Exception ex)
{
MessageBox.Show(host.HostForm,ex.Message,"Error Uploading");
}
}
 
Jeffrey Ting Jeffrey Ting on Facebook Jeffrey Ting on Spock Jeffrey Ting on Plaxo Jeffrey Ting on Spoke Jeffrey Ting on LinkedIn