3 Replies - 16710 Views - Last Post: 29 May 2009 - 10:39 AM Rate Topic: -----

#1 PurpleFairy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-May 09

using kernel32 function

Posted 29 May 2009 - 12:20 AM

Hi,

I am stuck on trying to call a kernel32 function into my c# program. Basically I am trying to copy files that have really long names. I get the normal windows PathTooLong exception thrown at me wth System.IO.File.Copy() and has been trying to find a work around using kernel32.

both the CopyFile and the GetShortPathName do not return anything.

is there a security setting to set?
they don't give errors either.

thanks for your help.

PF.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
//using Microsoft.Win32;


namespace Test_string
{
	public class Program

	{
	   [DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError=true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		static extern bool CopyFileW(
				   [MarshalAs(UnmanagedType.LPStr)] string lpExistingFileName,
				   [MarshalAs(UnmanagedType.LPStr)] string lpNewFileName,
				   [MarshalAs(UnmanagedType.Bool)] bool bFailIfExists);

		[DllImport("kernel32.dll", CharSet = CharSet.Auto)]

		public static extern int GetShortPathName(

				 [MarshalAs(UnmanagedType.LPTStr)]

				   string path,

				 [MarshalAs(UnmanagedType.LPTStr)]

				   StringBuilder shortPath,

				 int shortPathLength

				 );


		static void Main(string[] args)
		{

			//Test copy from Uploaded Docs in ARIES to local directory
			string fileName = @"ReallyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyLoooonnnngggggggggggggggggggggBbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbblllllllllllllllllllllllllllllllllllll.pdf";
			//string fileName = @"testfile.pdf";
			string sourcePath = @"C:\Users\PurpleFairy\Documents\RO\Test_string\";
			string targetPath = @"C:\Users\PurpleFairy\Documents\RO\Test_string\output\";


			//Use Path class to manipulate file and directory paths

			string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
			string f_sourceFile = @"\\?\" + sourceFile;
			//Console.WriteLine(f_sourceFile);
			//string destFile = System.IO.Path.Combine(targetPath, fileName);
			StringBuilder shortPath = new StringBuilder(200);
			GetShortPathName(sourceFile, shortPath, shortPath.Capacity);
			Console.WriteLine("short name: " + shortPath.ToString());

			// To copy a folder's contents to a new location:
			// Create a new target folder, if necessary.
			if (!System.IO.Directory.Exists(targetPath))
			{
				Console.WriteLine("hey");
				System.IO.Directory.CreateDirectory(targetPath);
			   //System.IO.Directory.SetCurrentDirectory(targetPath);
			}

			// To copy a file to another location and 
			// overwrite the destination file if it already exists.
			string destFile = System.IO.Path.Combine(targetPath, fileName);
			string f_destFile = @"\\?\" + destFile;
			//Console.WriteLine(f_destFile);
			//System.IO.File.Copy(sourceFile, destFile, true);
			Program.CopyFileW(f_sourceFile,f_destFile,true);
			Console.WriteLine(Program.CopyFileW(sourceFile, destFile, true));
			//System.IO.File.Copy(sourceFile,destFile,true);
			Console.WriteLine("after copy");
			Console.Read();
		}
	}

}



This post has been edited by PurpleFairy: 29 May 2009 - 12:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: using kernel32 function

#2 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: using kernel32 function

Posted 29 May 2009 - 08:23 AM

Here's a great site that shows you the how to use PInvoke for tons of functions: http://www.pinvoke.net/
Was This Post Helpful? 0
  • +
  • -

#3 debjit625   User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 452
  • Joined: 06-September 08

Re: using kernel32 function

Posted 29 May 2009 - 09:52 AM

string fileName = @& #34;ReallyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyLoooonnnnggggggggggggggg
ggggggBbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbllllllllllllllllllllllllllllllll
lllll.pdf";



Check this one your fileName is not in a valid format.Give a valid format.

I am not very much sure but you can try this.

Your strings (file and path) are not converting properly.As the function you are using
it takes a LPSTR .i.e.. Long pointer string,so pass it like this

let the file name with path is "C:\Code\My.cs"

then

string sourcefile = "C:\\Code\\My.cs";



Good luck

This post has been edited by debjit625: 29 May 2009 - 09:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 masteryee   User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: using kernel32 function

Posted 29 May 2009 - 10:39 AM

The Windows OS puts a limitation on the path/file lengths. In any case, google is everyone's friend, and the following links may be able to help you out:

http://www.experts-e...Q_21001688.html

http://www.csharp411...-with-ellipsis/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1