Hello,
I want to create and delete a text file on a remote server. File's directory is not shared but I have machine's user and pass informations.
How can I do that?
---------------------
File.Delete("C:\\test.txt");
StreamWriter dosya = new StreamWriter("C:\\test.txt");
-------------------
Loading
VulpesPosted Sep 28, 2011, 12:14 PM
The code will be something like this:
using System;
using System.Management;
class Test
{
static void Main()
{
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.60.88\root\CIMV2", options);
scope.Connect();
query = new ObjectQuery(@"SELECT * FROM CIM_Datafile WHERE name = 'c:\\c$\\text.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);
}
Console.ReadKey();
}
}
Rahul MPosted Sep 28, 2011, 12:33 PM
yokzuPosted Sep 28, 2011, 7:51 AM
Basically, my username is ; admin and pass : 12345. How may I authenticate?
Rahul MPosted Sep 28, 2011, 7:27 AM
Rahul MPosted Sep 28, 2011, 7:26 AM
yokzuPosted Sep 28, 2011, 7:01 AM
File.Delete(@"\\192.168.60.88\c$\test.txt");
I am getting this error;
"Logon error, unknown user name or bad password"
Hemant KumarPosted Sep 28, 2011, 6:38 AM
File.Delete(@"\\192.168.60.88\\c$\\test.txt");
VulpesPosted Sep 28, 2011, 6:34 AM
File.Delete("\\\\192.168.60.88\\c$\\test.txt");
or use a verbatim string:
File.Delete(@"\\192.168.60.88\c$\test.txt");
yokzuPosted Sep 28, 2011, 6:26 AM
File.Delete("\\192.168.60.88\\c$\\test.txt");
yokzuPosted Sep 28, 2011, 5:57 AM
I can reach the computer but after user authentication. How can I do that in C#?
\\192.168.60.88\\c$\\test.txt
Hemant KumarPosted Sep 28, 2011, 5:21 AM
\\machinename\c$\. You have to be an admin on the machine to reach that path. This works with the System.IO objects (File, Directory, etc.)