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

VulpesRank #55 · 29.2K reputationPosted Sep 28, 2011, 8:14 AM EDT
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();
}
}
Hemant KumarRank #1,202 · 1.5K reputationPosted Sep 28, 2011, 1:21 AM EDT
\\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.)yokzuRank #5,421 · 300 reputationPosted Sep 28, 2011, 1:57 AM EDT
I can reach the computer but after user authentication. How can I do that in C#?
\\192.168.60.88\\c$\\test.txt
yokzuRank #5,421 · 300 reputationPosted Sep 28, 2011, 2:26 AM EDT
File.Delete("\\192.168.60.88\\c$\\test.txt");
VulpesRank #55 · 29.2K reputationPosted Sep 28, 2011, 2:34 AM EDT
File.Delete("\\\\192.168.60.88\\c$\\test.txt");
or use a verbatim string:
File.Delete(@"\\192.168.60.88\c$\test.txt");
Hemant KumarRank #1,202 · 1.5K reputationPosted Sep 28, 2011, 2:38 AM EDT
File.Delete(@"\\192.168.60.88\\c$\\test.txt");
yokzuRank #5,421 · 300 reputationPosted Sep 28, 2011, 3:01 AM EDT
File.Delete(@"\\192.168.60.88\c$\test.txt");
I am getting this error;
"Logon error, unknown user name or bad password"
Rahul M20 reputationPosted Sep 28, 2011, 3:26 AM EDT
Rahul M20 reputationPosted Sep 28, 2011, 3:27 AM EDT
yokzuRank #5,421 · 300 reputationPosted Sep 28, 2011, 3:51 AM EDT
Basically, my username is ; admin and pass : 12345. How may I authenticate?
Rahul M20 reputationPosted Sep 28, 2011, 8:33 AM EDT