程序访问远程文件夹,。我使用的是影射网络驱动器的方法,怎么老提示拒绝访问啊 ??
这种方法的缺点就是有时候可以访问,有时候就老提示拒绝访问!
大家请支个招吧??如果有程序实现远程共享受文件夹的访问或者远程访问FTP的也可以~~~~~~~~
结帖散分~~~
这种方法的缺点就是有时候可以访问,有时候就老提示拒绝访问!
大家请支个招吧??如果有程序实现远程共享受文件夹的访问或者远程访问FTP的也可以~~~~~~~~
结帖散分~~~
public static bool Ping(string remoteHost)
{
if(bCancelTransfer)
{
log.Info("DisConnect: [ " + remoteHost + " ]") ;
return false;
}
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 = @"ping -n 1 " + remoteHost;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (proc.HasExited == false)
{
proc.WaitForExit(500);
}
string pingResult = proc.StandardOutput.ReadToEnd();
if (pingResult.IndexOf("(0% loss)") != -1)
{
Flag = true;
}
proc.StandardOutput.Close() ;
}
catch (Exception ex)
{
log.Debug(ex.Message) ;
}
finally
{
try
{
proc.Close();
proc.Dispose();
}
catch
{
}
}
return Flag;
} public static bool Connect(string remoteHost, string userName, string passWord)
{
if(!Ping(remoteHost))
{
return false;
}
if(bCancelTransfer)
{
log.Info("DisConnect: [ " + remoteHost + " ]") ;
return false;
}
bool Flag = true;
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 \\" + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (proc.HasExited == false)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
if (errormsg != "")
{
log.Debug(errormsg);
Flag = false;
}
proc.StandardError.Close() ;
}
catch (Exception ex)
{
log.Debug(ex.Message, ex);
Flag = false;
}
finally
{
try
{
proc.Close();
proc.Dispose();
}
catch
{
}
}
return Flag;
}先ping在connect
连接上就像访问本地一样了
F:\ZD\xmltest\xmlreader.aspx.cs(538): 找不到类型或命名空间名称“log”(是否缺少 using 指令或程序集引用?)
F:\ZD\xmltest\xmlreader.aspx.cs(505): 名称“bCancelTransfer”在类或命名空间“xmltest.xmlreader”中不存在
{
DirectoryInfo objDir = new DirectoryInfo(strDir);
DirectoryInfo[] objDirs = objDir.GetDirectories();
FileInfo[] objFiles = objDir.GetFiles();
string strInner = "";
for(int i=0;i<iLevel;i++) strInner = " " + strInner;
if(objDirs.Length > 0 || objFiles.Length > 0)
{
Console.WriteLine(strInner + "+[D]" + objDir.Name);
}
else
{
Console.WriteLine(strInner + "-[D]" + objDir.Name);
}
for(int i=0;i<objDirs.Length;i++)
{
ListFolder(objDirs[i].FullName,iLevel+1);
}
strInner += " ";
for(int i=0;i<objFiles.Length;i++)
{
Console.WriteLine(strInner + "-[F]" + objFiles[i].Name);
}
} public static void CopyFolder(string strFromPath,string strToPath)//文件夹的拷贝
{
//如果源文件夹不存在,则创建
if (!Directory.Exists(strFromPath))
{
OpenDirectory(strFromPath,"Administrator","qazwsxedc"); //取得要拷贝的文件夹名
// string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\") + 1,strFromPath.Length-strFromPath.LastIndexOf("\\")-1);
string strFolderName=strFromPath.Substring(strFromPath.LastIndexOf("\\")+1,strFromPath.Length-strFromPath.LastIndexOf("\\")-1);
string ip=strFromPath.Substring(strFromPath.IndexOf("\\")+1,strFromPath.Length-1);
//如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹
if (!Directory.Exists(strToPath + "\\" + strFolderName))
{
Directory.CreateDirectory(strToPath + "\\" + strFolderName);
}
//创建数组保存源文件夹下的文件名
//string ss=strFromPath.p
string[] strFiles = Directory.GetFiles(strFromPath); //循环拷贝文件
for(int i = 0;i < strFiles.Length;i++)
{
//取得拷贝的文件名,只取文件名,地址截掉。
//string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\") + 1,strFiles[i].Length - strFiles[i].LastIndexOf("\\")-1);
string strFileName=strFiles[i].Substring(strFiles[i].LastIndexOf("\\")+1,strFiles[i].Length-strFiles[i].LastIndexOf("\\")-1);
//开始拷贝文件,true表示覆盖同名文件
File.Copy(strFiles[i],strToPath + "\\" + strFolderName + "\\" + strFileName,true);
File.Delete(strFiles[i]);
} //创建DirectoryInfo实例
DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
//取得源文件夹下的所有子文件夹名称
DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
for (int j = 0;j < ZiPath.Length;j++)
{
//获取所有子文件夹名
string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();
//把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝
CopyFolder(strZiPath,strToPath + "\\" + strFolderName);
}
}
else
{
}
}
保证CopyFile的代码正确
然后再把路径换成网络路径
在copy前
用net use命令
这种方法的缺点就是有时候可以访问,有时候就老提示拒绝访问!
大家请支个招吧??这个问题,不是网络映射驱动器没有建成,而是驱动器建成之后,aspnet用户没有权限访问。如果老是动态创建映射驱动器的话,把aspnet用户放到administrators组中;否则,设置aspnet用户对驱动器有访问权限。