检查文件是否在期望的位置

上一篇 / 下一篇  2010-02-09 19:33:51 / 个人分类:c#测试

程序执行完后,可能会在远端的某个地方创建一个文件,检查它是否存在可以分为三个步骤。

1. 获取访问远端机器的权限 (net use 到那个机器)

2. 检查是否存在

3. 断开和远端机器的连接 (删除 net use)

下面是对应的三个函数

public static void NetUse(string machine, string pwd, string user)
        {
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;
            startInfo.Arguments = string.Format(@"/c net use \\{0}\c$ {1} /user:{2}", machine, pwd, user);
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            process.Close();
        }

 

public static bool FileExist(string machineName, string dirName, string fileName)
        {
            FileInfo fileInfo = null;
            bool needReleaseNetUse = false;
            if (machineName == "." || string.IsNullOrEmpty(machineName) || machineName.ToLower().Contains("local"))
            {
                fileInfo = new FileInfo(string.Format(@"{0}\{1}", dirName, fileName));
            }
            else
            {
                NetUse(machineName, "PsPemDep", @"pensam.dom\pemdep");
                fileInfo = new FileInfo(string.Format(@"\\{0}\{1}\{2}", machineName, dirName.Replace(":", "$"), fileName));
                needReleaseNetUse = true;
            }
            bool exist = fileInfo.Exists;
            if (!exist)
            {
                Helper.WriteDebug(string.Format("File: '{0}' doesnot exist.", fileInfo.FullName));
            }
            if (needReleaseNetUse)
            {
                NetUseDelete(machineName);
            }
            return exist;
        }

 

public static void NetUseDelete(string machine)
        {
            try
            {
                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;
                startInfo.Arguments = string.Format(@"/c net use \\{0}\c$ /delete", machine);
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                process.Close();
            }
            catch (Exception)
            {
                WriteDebug(string.Format("Exception happen wehn net use to machine {0} ", machine));
            }
        }


TAG:

 

评分:0

我来说两句

日历

« 2024-04-28  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 6172
  • 日志数: 10
  • 文件数: 2
  • 建立时间: 2010-01-29
  • 更新时间: 2010-02-10

RSS订阅

Open Toolbar