/// <summary> /// 下载文件(将服务器上的文件copy到本地) /// </summary> /// <param name="localPath_Str">本地路径</param> /// <param name="sourcePath">服务器上的文件资源路径</param> void getDirs(string localPath_Str, string sourcePath) { string[] fileNames = Directory.GetFiles(sourcePath);//获得文件夹内的文件数量 if (fileNames.Length > 0) { for (int i = 0; i < fileNames.Length; i++) { int k = fileNames[i].LastIndexOf("\"); string fileName_Str = fileNames[i].Substring(k + 1); if (!File.Exists(localPath_Str + "\" + fileName_Str))//检查本地是否存在将要下载的文件 { File.Create(localPath_Str + "\" + fileName_Str).Close();//创建文件 } File.Copy(fileNames[i], localPath_Str + "\" + fileName_Str, true);//替换掉文件内容。 } } string[] dirInfo = Directory.GetDirectories(sourcePath);//检查是否有文件夹 if (dirInfo.Length > 0) { for (int i = 0; i < dirInfo.Length; i++) { int k = dirInfo[i].LastIndexOf("\"); string dirInfo_Str = dirInfo[i].Substring(k + 1); if (!Directory.Exists(localPath_Str + "\" + dirInfo_Str))//检查本地是否有相同的文件夹 { DirectoryInfo directory = new DirectoryInfo(localPath_Str + "\" + dirInfo_Str); directory.Create(); } getDirs(localPath_Str + "\" + dirInfo_Str, dirInfo[i]); } } }
/// <summary> /// 将目标文件读成流,写入到目标文件中 /// </summary> /// <param name="serverPath">源文件</param> /// <param name="localPath">要写入的文件</param> public void DownLoadFils(string serverPath, string localPath) { StreamReader reader = new StreamReader(serverPath);//初始化读取 StreamWriter writer = new StreamWriter(localPath);//初始化写入 string readAll = reader.ReadToEnd();//从流中读取全部 //将流写到到所创建的文件中去。 writer.Write(readAll); writer.Flush(); reader.Close(); writer.Close(); }
public static void DownLoadFiles(string serverPath,string localPath) { if ((File.Exists(serverPath)) && (File.Exists(localPath))) { FileStream fSource = File.Open(serverPath, FileMode.Open); //初始化文件流 byte[] bty_Ary = new byte[fSource.Length];//初始化字节数组 fSource.Read(bty_Ary, 0, bty_Ary.Length);//读取流中数据把它写到字节数组中 fSource.Close();//关闭流 FileStream fTarget = File.Open(localPath, FileMode.Append);//初始化文件流 fTarget.Write(bty_Ary, 0, bty_Ary.Length);//将字节数组写入文件流 fTarget.Close();//关闭流 } //string str = Encoding.Default.GetString(bty_Ary);//将字节数组内容转化为字符串 //byte[] array = Encoding.UTF8.GetBytes("Hello World!你好");//给字节数组赋值 }
var Info = System.Diagnostics.FileVersionInfo.GetVersionInfo(Path);
public static bool CheckFolderExist(string path) { if (path.Trim().Length > 0) { DirectoryInfo directory = new DirectoryInfo(path); return directory.Exists; } else { return false; } }
public static void CreateDirectory(string path) { if (path.Trim().Length > 0) { DirectoryInfo directory = new DirectoryInfo(path); directory.Create(); } }
FileInfo f1 = new FileInfo(@"F:A_插件james.txt"); f1.Create();
public static bool ByteStreamToFile(string Path, byte[] StreamByte, bool isZip) { try { byte[] bytes; if (isZip == true) { bytes = GZip.Decompress(StreamByte); } else { bytes = StreamByte; } //创建文件流(创建的方式,文件存在则覆盖) FileStream fileStream = new FileStream(Path, FileMode.Create); //把文件流写到文件中 fileStream.Write(bytes, 0, bytes.Length); fileStream.Flush(); fileStream.Close(); fileStream.Dispose(); return true; } catch { return false; } }
public static void CopyFile(string sourceFileName, string destFileName) { File.Copy(sourceFileName, destFileName, true); }
public static void DeleteFile(string filePath) { try { //c# 不能删除只读文件 File.SetAttributes(filePath, FileAttributes.Normal); if (File.Exists(filePath)) { File.Delete(filePath); } } catch (Exception ex) { // MessageBox.Show(ex.ToString()); throw ex; } }
string[] Info_Ary = Directory.GetFileSystemEntries(@"F:A_插件");
string path = context.Server.MapPath(@"~/CISWeb/SMT_SOP"); DirectoryInfo theFolder = new DirectoryInfo(path); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 string strName = ""; foreach (DirectoryInfo NextFolder in dirInfo) { // this.listBox1.Items.Add(NextFolder.Name); FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 strName = NextFile.Name; }
public void UseApp() { system.Diagnostics.Process.Start("C:SayHi.exe","para1 para2"); } // para1 和para 2是调用的时候,传入的两个参数