如何从winforms.core应用程序打开共享intranet驱动器上的文件



编辑:我通过部署我的应用程序让它发挥作用。这个问题只存在于我在visualstudio中以调试模式运行时。

我正在创建一个应用程序,它包含许多路径以便于访问。

var item = this.CockpitLinkDataTable.Rows[this.CockPitLinkDataTable.CurrentCell.RowIndex];

var tableRow = (Link)item.DataBoundItem;
new Process
{
StartInfo = new ProcessStartInfo($@"{tableRow.LinkPath}")
{
UseShellExecute = true
}
}.Start();

当我尝试打开本地文件或网页时,代码运行良好。当我尝试从intranet共享驱动器打开文件时,我得到错误消息:";系统找不到指定的驱动器">

但如果我复制路径并在windows.run中运行它,它会很好地打开。

您可以尝试以下代码从共享的internet驱动器打开文件。

private void button1_Click(object sender, EventArgs e)
{
bool status = false;
//connect to the shared folder use ip address ,username and password
status = connectState(@"\1xx.xx.xx.xxshare", "username", "password");
if (status)
{
DirectoryInfo theFolder = new DirectoryInfo(@"\1xx.xx.xx.xxShareHuan");
var fileInfo = theFolder.GetFiles("*.txt").First().FullName;  //get the path
var text = File.ReadAllLines(fileInfo); //read the txt file
}

}
public bool connectState(string path)
{
return connectState(path, "", "");
}
public  bool connectState(string path, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}

更新:直接方式:

DirectoryInfo theFolder = new DirectoryInfo(@"\1xx.xx.xx.xxShareHuan");
var fileInfo = theFolder.GetFiles("*.txt").First().FullName;  //get the path
var text = File.ReadAllLines(fileInfo); //read the txt file

最新更新