向文件中写入数据linux,C# 向共享文件中写入数据

先写一个判断此时是否可以正常连接网络共享主机:

private static bool connectState()

{

bool flag = false;

Process process = new Process();

try

{

process.StartInfo.FileName = "cmd.exe";

process.StartInfo.UseShellExecute = false;

process.StartInfo.RedirectStandardInput = true;

process.StartInfo.RedirectStandardOutput = true;

process.StartInfo.RedirectStandardError = true;

process.StartInfo.CreateNoWindow = true;

process.Start();

string dosLine = @"net use \\IP_ADDRESS\PATH /User:username password /PERSISTENT:YES";

process.StandardInput.WriteLine(dosLine);

process.StandardInput.WriteLine("exit");

while (!process.HasExited)

{

process.WaitForExit(1000);

}

string errorMsg = process.StandardError.ReadToEnd();

process.StandardError.Close();

if (String.IsNullOrEmpty(errorMsg))

{

flag = true;

}

else

{

throw new Exception(errorMsg);

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

process.Close();

process.Dispose();

}

return flag;

}

上面的这一段代码是摘抄的, 当然这一段就是访问网络文件的核心了,之后就是正常的操作文件了(仅限局域网)我的是这样子的,很简单的一个:

public static void logs(string str)

{

using (FileStream fs = new FileStream("//IP_ADDRESS/PATH/hook.txt", FileMode.Append, FileAccess.Write))

{

using (StreamWriter sw = new StreamWriter(fs))

{

sw.WriteLine(str);

}

}

        }0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值