2

I want to create and delete a file on a remote machine of which i have admin username and password.

I am using this code

ConnectionOptions options = new ConnectionOptions();
    options.Username = "admin";
    options.Password = "12345";
    ManagementScope scope =  null;
    ObjectQuery query = null;
    ManagementObjectSearcher searcher = null;

    try
    {
       scope = new ManagementScope(@"\\192.168.3.125\root\CIMV2", options);
       scope.Connect();
       query = new ObjectQuery(@"SELECT * FROM CIM_Datafile WHERE name = 'c:\\c$\\Testing\\Test.txt'");
       searcher = new ManagementObjectSearcher(scope, query); // EDIT forgot to include 'scope' previously
    }
    catch(Exception ex)
    {
       Console.WriteLine(ex.Message);
       return;
    }

    foreach(ManagementObject mo in searcher.Get())
    {
       uint returnCode = (uint)mo.InvokeMethod("Delete", null);
       if (returnCode == 0)
         Console.WriteLine("File was successfully deleted");
       else
         Console.WriteLine("Deletion failed due to return code " + returnCode);
    }

But it is giving me invalid query error and also i want to know how to Create a file on Remote machine.

and i even cant access the path \\192.168.3.125\C$\Testing\Test.txt

My file location is c:\Testing\Test.txt

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Hitesh
  • 111
  • 1
  • 2
  • 8
  • Do you have any Domain name to access this Remote machine. If so options.Domain = "Domain"; – sk7730 Jan 02 '14 at 09:50
  • If it'd have been an authentication error it would appear on .Connect() not during query execution. – Zegar Jan 02 '14 at 09:54

2 Answers2

3

Firstly can you access the file via the windows explorer from the machine (start -> run -> \192.168.3.125\C$\Testing\Test.txt)

If so what's wrong with

File.Delete(@"\\192.168.3.125\C$\Testing\Test.txt");
Liath
  • 9,913
  • 9
  • 51
  • 81
  • No i cant access the file with \192.168.3.125\C$\Testing\Test.txt – Hitesh Jan 02 '14 at 10:11
  • There should be a double dash at the start of that (it got stripped out for some reason). However if you can't access it from the machine there's no way your code will be able to! – Liath Jan 02 '14 at 10:39
  • I still cant access that file.What's the alternative?? – Hitesh Jan 02 '14 at 11:09
  • You need to work out why you can't access the file yourself. Can you access the machine? Is the IP right? Do you have permissions to read the file/folder? – Liath Jan 02 '14 at 11:12
  • Its working now i tried with another system.Except for first time it didn't asked me username and password(I think it needs to connect first). My question is how to connect the two remote systems through c# so that i can create or delete files. – Hitesh Jan 02 '14 at 11:48
  • http://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share – Liath Jan 02 '14 at 11:50
0

In my case, when I was connecting two windows based computers, you could just put:

@"\\PC-NAME\NEXT-FOLDER\NEXT-FOLDER\test.txt"
Frostytheswimmer
  • 720
  • 4
  • 19
  • when i access it through my browser it asks me username and password (only first time) but how to do this through c# code(means how to connect to other system via c#)?? – Hitesh Jan 06 '14 at 07:18