/// <summary> /// Return true if Mobius can write to the specified file (usually in another user's Share area or SharePoint site) from the service side /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool CanWriteFileFromServiceAccount(string path) { try { if (path.StartsWith(@"\\")) { string shareName = NetworkResourceUtil.NormalizeConnectionStringToUncShareName(path); //bool shareConnected = NetworkResourceUtil.DirectoryExists(shareName); // just verifying directory is faster if share already connected //if (!shareConnected) //{ string userName, pw; if (!AccountAccessMx.TryGetMobiusSystemWindowsAccount(out userName, out pw)) { return(false); } NetworkResourceUtil.AddConnection(path, userName, pw, false); //} FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); fs.Close(); return(true); } else if (SharePointUtil.IsSharePointName(path)) { string msg = SharePointUtil.CanWriteFile(path); // check for write, note: uses current user id not SystemAccountName return(Lex.IsNullOrEmpty(msg)); } else { return(false); } } catch (Exception ex) { DebugLog.Message("CanWriteFileFromServiceAccount failed for path: " + path + "\r\n" + ex.Message); return(false); } }
/// <summary> /// Copy file from server to client /// </summary> /// <param name="serverFile"></param> /// <param name="clientFile2"></param> public static void CopyToClient( string serverFile, string clientFile) { FileStream fw = null; try { string tempFile = TempFile.GetTempFileName(); if (ServiceFacade.UseRemoteServices) { fw = new FileStream(tempFile, FileMode.Create, FileAccess.Write); while (true) { NativeMethodTransportObject resultObject = ServiceFacade.CallServiceMethod( ServiceCodes.MobiusFileService, MobiusFileService.CopyToClient, new object[] { serverFile }); byte[] buffer = (byte[])resultObject.Value; if (buffer == null) { break; } fw.Write(buffer, 0, buffer.Length); if (buffer.Length < UAL.ServerFile.TransferChunkSize) { break; } serverFile = null; // null to get subsequent chunks System.Windows.Forms.Application.DoEvents(); } fw.Close(); } else // direct call { fw = new FileStream(tempFile, FileMode.Create, FileAccess.Write); while (true) { byte[] buffer = UAL.ServerFile.CopyToClient(serverFile); if (buffer == null) { break; } fw.Write(buffer, 0, buffer.Length); if (buffer.Length < UAL.ServerFile.TransferChunkSize) { break; } serverFile = null; // null to get subsequent chunks System.Windows.Forms.Application.DoEvents(); } fw.Close(); } bool normalFile = !SharePointUtil.IsSharePointName(clientFile); if (normalFile) { // normal file, move temp file to dest file FileUtil.ReplaceFile(clientFile, tempFile); } else // sharepoint { SharePointUtil.CopyToSharePoint(tempFile, clientFile); File.Delete(tempFile); // delete any temp file } return; } catch (Exception ex) // close file on any exception { if (fw != null) { try { fw.Close(); } catch { } } throw new Exception(ex.Message, ex); } }