Save file copy function in C#

If you want to copy a file to a folder, which is polled by a import service, you want the import service to start processing the file, when it is fully copied to the folder. This can be done with the following code:

 

/// <summary>
/// Copy a file to a destinationfolder, with the fileextension renamed to [tempFileExtension]
/// After copy, rename the destination file to it's original file extension
/// This methode can be used in scenario's, where a service is polling a folder and you don't want the service to start processing the file, when it is not fully copied yet
/// </summary>
/// <param name="sourceFolder">Can't be empty</param>
/// <param name="destinationFolder">Can't be empty</param>
/// <param name="fileName">Can't be empty. Should be the name of the file, including the file extension</param>
/// <param name="tempFileExtension">Can't be empty</param>
public static void SaveFileCopy(string sourceFolder, string destinationFolder, string fileName, string tempFileExtension)
{
// Determine source file path
string sourceFile = Path.Combine(sourceFolder, fileName);
// Determine temp destination file path
string tempDestinationFile = string.Format("{0}{1}", Path.Combine(destinationFolder, fileName), tempFileExtension);
// Set temp destination file to [not-readonly]
if (File.Exists(tempDestinationFile))
{
File.SetAttributes(tempDestinationFile, FileAttributes.Normal);
}
// Copy the file as temp file extension
File.Copy(sourceFile, tempDestinationFile, true);
// Determine destination file path
string destinationFile = Path.Combine(destinationFolder, fileName);
// Set destination file to [not-readonly]
if (File.Exists(destinationFile))
{
File.SetAttributes(destinationFile, FileAttributes.Normal);
}
// Delete the destination file if it exists
if (File.Exists(destinationFile)) { File.Delete(destinationFile); }
// Rename the file to it's original file extension
File.Move(tempDestinationFile, destinationFile);
}

Solving: Object reference not set to an instance of an object, when calling a WCF service

 

I was getting a [Object reference not set to an instance of an object.] error, when calling a WCF service from a client application. The WCF service was build in [Any CPU] configuration, but an external dll was build in x86 modus. In this case the WCF service will only work, when you set the application pool property [Enable 32-Bit Applications] to True

image

 

 

 

Exception

[Object reference not set to an instance of an object.]
Server stack trace:
System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood
System.ServiceModel.Channels.ServiceChannel.HandleReply
System.ServiceModel.Channels.ServiceChannel.Call
System.ServiceModel.Channels.ServiceChannelProxy.InvokeService
System.ServiceModel.Channels.ServiceChannelProxy.Invoke
Exception rethrown[0]:
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke

How to determine the active directory server IP address or name

If you logon to a domain you can get the IP address of the active directory server by executing the following steps:

start > cmd > echo %LOGONSERVER%

 

You could also try to ping you’re domain:

start > cmd > ping mydomain.local

 

If you want to determine the domain controller name in C#, use:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
string controller = context.ConnectedServer;
Console.WriteLine(string.Format("Domain Controller: {0}", controller));
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
}
}

Result

image

Solving: Error : rsAccessDenied (The permissions granted to user ‘MyDomain\Administrator’ are insufficient for performing this operation.)

When deploying reports by using the rs.exe you can get the error: "Error : rsAccessDenied (The permissions granted to user ‘MyDomain\Administrator’ are insufficient for performing this operation.)".

 

Solution

 

image

 

  • Go to the home folder
  • Go to Properties
  • Security
  • Add you’re account to the roles [Browser, Content Manager, My Reports, Publisher, Report Builder]

If the security chain is broken in the subfolders you will have to repeat the steps for each subfolder

 

image

How to send an executable as an E-mail attachment with BPOS

Out of the box sending executables as attachments with BPOS is not possible. BPOS will replace the executable with a *.txt file that contains the text:

FILE QUARANTINED

Microsoft Online has removed winmail.dat->Unnamed Attachment->YoureApplication.exe since it
was found to match the FILE FILTER= unnamed: *.exe file filter.

Even if you zip or rar the file and rename the file extension, BPOS will replace the executable in the zip or RAR file with the txt file.

To prevent BPOS from removing the executable you can ask you administrator to edit the FILE FILTER rule or you can follow the steps below:

  • Zip the executable file
  • Open the zip file with a hexeditor (I use Notepad++ with the HexEditor.dll plugin)
  • Change the first 2 letters “PK” to some other letters, for example “LL”
  • Save the zip file
  • Send it to the customer
  • Customer must open the zip file with a hexeditor
  • Change the first 2 letters “LL” back to “PK” and save the zip file
  • Then the customer can extract the zip file and use the “YoureApplication.exe”

IE 9: Microsoft JScript runtime error: Unable to get value of the property ‘srcElement’: object is null or undefined

When accessing an .net 3.5 web application in Microsoft Internet Explorer 9, I get the error:

Microsoft JScript runtime error: Unable to get value of the property ‘srcElement’: object is null or undefined

 

When the "Compatibility View" button is pushed the error is resolved, but I am still investigating what the cause of the error is and if it can be fixed without reminding all the users of the website to push the "Compatibility View" button Winking smile