0

I would like to move a file from Windows Mobile to a specific folder in Windows. The file in the mobile device is into the My Documents path. The device is connected to the WiFi network, and the folder shared in windows is called "folder".

How could I do, I tried this but doesn't work:

var f= System.Enviroment.GetFolderPath(System.Enviroment.SpecialFolder.Personal);
FileInfo fi = new FileInfo(f.ToString() + @"\file.txt");
fi.CopyTo(@"\\MYPERSONAL-PC\folder",true);

the error is :

in System.IO.__Error.WinIOError(Int32 errorCode, String str)
in System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
in System.IO.FileInfo.CopyTo(String destFileName, Boolean overwrite)
in Project.MainForm.SaveButton_Click(Object sender, EventArgs e)
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
in System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
in Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
in System.Windows.Forms.Application.Run(Form fm)
in Project.Program.Main()

enter image description here

I also tried to use WNetAddConnection3, but still the same the connection to the network resource is ok, but return me always the same the code is here:

[StructLayout(LayoutKind.Sequential)]
    internal struct NetResource
    {
        public uint dwScope;
        public uint dwType;
        public uint dwDisplayType;
        public uint dwUsage;
        [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
        public string lpLocalName;
        [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
        public string lpRemoteName;
        [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
        public string lpComment;
        [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
        public string lpProvider;
    }


[DllImport("coredll.dll")]
private static extern int WNetAddConnection3(IntPtr hWndOwner,
ref NetResource lpNetResource, string lpPassword, string lpUserName, int dwFlags);

[DllImport("coredll.dll")]
static extern int WNetCancelConnection2(string lpName, Int32 dwFlags, bool bForce);

var f= System.Enviroment.GetFolderPath(System.Enviroment.SpecialFolder.Personal);

NetResource logsResource = new NetResource();
logsResource.lpLocalName = "logs";
logsResource.lpRemoteName = @"\\MYPERSONAL-PC\folder";
logsResource.dwType = 0x1; 
logsResource.dwScope = 0;
logsResource.dwUsage = 0;
logsResource.dwDisplayType = 0;

//try to connect the network resource
WNetAddConnection3(new IntPtr(0), ref logsResource, @"pass", @"dom\user", 0);
FileInfo fi = new FileInfo(f.ToString() + @"\file.txt");
**fi.CopyTo(@"\\MYPERSONAL-PC\folder", true);**
puti26
  • 431
  • 3
  • 14
  • 31
  • 2
    What doesn't work with this solution? – Patrick Hofman Feb 19 '14 at 10:52
  • Return me IOException, but i don't know why. – puti26 Feb 19 '14 at 10:55
  • 1
    Is there an error code? – Patrick Hofman Feb 19 '14 at 11:00
  • I update the question with the error. – puti26 Feb 19 '14 at 11:08
  • I see the error **message** but usually `IOException`s have a `ErrorCode` which is a number. That describes the specific error. – Patrick Hofman Feb 19 '14 at 11:13
  • Only this message : IOException was unhandled (IOException) . – puti26 Feb 19 '14 at 11:22
  • Look inside the exception. – Patrick Hofman Feb 19 '14 at 11:24
  • I think ‘CopyTo` needs the destination file name too. Actually, I'm pretty sure. And check the exception information in the debugger. – Silvermind Feb 19 '14 at 11:29
  • 1
    The function WNetAddConnection3 returns ERROR_SUCCES if OK. If not you have to use GetLastWin32Error() to get a more meaningful error code than just the exception. Further on I would enlcose the block within Try/catch and setp thru it with a debugger. Possibly the source file is missing or you do not have write permissions to the network share. Did you try to connect using mobile file eplorer and then copy a file to the share? – josef Feb 20 '14 at 18:20
  • I tried to open the shared folder with File explorer. I did this procedure: FileExplorer - Menu - Go to - Open path - New Path.. and add this: \\MYPERSONAL-PC\folder, but give me this error : **The network path was not found** and **Network resource cannot be found or you do not have permission to access the network** – puti26 Feb 21 '14 at 07:44

2 Answers2

1

A few things to try:

  1. Can you connect to the file share from a laptop or other PC on the wireless network? I want to verify it's not a configuration or authentication problem.

  2. Assuming #1 works: try replacing MYPERSONAL-PC with the IP address of the PC?

  3. Assuming #1 works, but #2 does not: try to P/Invoke WNetAddConnection3 to make a local connection to a network resource (like mapping a network drive) and copy to that.

PaulH
  • 7,759
  • 8
  • 66
  • 143
  • I'm tring to use this solution (the second answer): [link](http://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials) but return me error about *`Can't find DLL PInvoke 'Mrp.dll'`* – puti26 Feb 20 '14 at 09:28
  • 1
    If you look at the MSDN link, towards the bottom, it says that in Windows Mobile 6.5, WNetAddConnection3 is exported by coredll not mpr.dll. – PaulH Feb 20 '14 at 15:53
  • Did you verify the WNetAddConnection3() call succeeded? After you make the connection, your CopyTo() command should point to the connected location fi.CopyTo(@"logs", true); – PaulH Feb 20 '14 at 15:56
1

I would set the redir registry entry RegisterFSRoot to 1 and see the \Network folder on the device: http://msdn.microsoft.com/en-us/library/aa922326.aspx. Then you can use the file copy function to copy to \Network\. Where shared folder is the one you mapped from the sharing server. If you did not Connect to the share using mobile File Explorer, you can use WNetAddConnection3 to add the connection http://msdn.microsoft.com/en-us/library/aa916067.aspx and http://msdn.microsoft.com/en-us/library/aa917445.aspx. When usig C# you have to p/invoke the APIs: http://www.pinvoke.net/default.aspx/mpr.wnetaddconnection3 (replace mpr.dll with coredll.dll)

josef
  • 5,951
  • 1
  • 13
  • 24