/// <summary> /// 视频格式转为Flv /// </summary> /// <param name="vFileName">原视频文件地址</param> /// <param name="ExportName">生成后的Flv文件地址</param> public bool ConvertFlv(string vFileName, string ExportName) { if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(NSWebPath.GetServerPath(vFileName)))) { return(false); } vFileName = NSWebPath.GetServerPath(vFileName); ExportName = NSWebPath.GetServerPath(ExportName); string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\""; //Flv格式 System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = ffmpegtool; p.StartInfo.Arguments = Command; p.StartInfo.WorkingDirectory = NSWebPath.GetServerPath("tools"); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = false; p.Start(); p.BeginErrorReadLine(); p.WaitForExit(); p.Close(); p.Dispose(); return(true); }
/// <summary> /// 运行mencoder的视频解码器转换 /// </summary> public string MChangeFilePhy(string vFileName, string playFile, string imgFile) { string tool = NSWebPath.GetServerPath(VideoConvert.mencodertool); if ((!System.IO.File.Exists(tool)) || (!System.IO.File.Exists(vFileName))) { return(""); } string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv"); string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments = " " + vFileName + " -o " + flv_file + " -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1 -vf scale=" + widthOfFile + ":" + heightOfFile + " -ofps 12 -srate 22050"; try { System.Diagnostics.Process.Start(FilestartInfo); CatchImg(flv_file, imgFile); } catch { return(""); } return(""); }
/// <summary> /// 删除文件 /// </summary> /// <param name="file">要删除的文件路径和名称</param> public static void DeleteFile(string file) { if (File.Exists(NSWebPath.GetServerPath(file))) { File.Delete(NSWebPath.GetServerPath(file)); } }
protected string Read_Txt(string filename) { Encoding code = Encoding.GetEncoding("gb2312"); string temp = NSWebPath.GetServerPath("Precious\\" + filename + ".txt"); string str = ""; if (File.Exists(temp)) { StreamReader sr = null; try { sr = new StreamReader(temp, code); str = sr.ReadToEnd(); // 读取文件 } catch { } sr.Close(); sr.Dispose(); } else { str = ""; } return(str); }
/// <summary> /// 转换文件并保存在指定文件夹下 /// </summary> /// <param name="fileName">上传视频文件的路径(原文件)</param> /// <param name="playFile">转换后的文件的路径(网络播放文件)</param> /// <param name="imgFile">从视频文件中抓取的图片路径</param> /// <returns>成功:返回图片虚拟地址;失败:返回空字符串</returns> public string ChangeFilePhy(string fileName, string playFile, string imgFile) { string ffmpeg = NSWebPath.GetServerPath(VideoConvert.ffmpegtool); if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName))) { return(""); } string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv"); string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file; try { System.Diagnostics.Process.Start(FilestartInfo); //转换 CatchImg(fileName, imgFile); //截图 } catch { return(""); } return(""); }
/// <summary> /// 删除指定目录及其所有子目录 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static void DeleteDirectory(string directoryPath) { directoryPath = NSWebPath.GetServerPath(directoryPath); if (IsExistDirectory(directoryPath)) { Directory.Delete(directoryPath, true); } }
/// <summary> /// 移动文件(剪贴--粘贴) /// </summary> /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param> /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param> public static void MoveFile(string dir1, string dir2) { dir1 = dir1.Replace("/", "\\"); dir2 = dir2.Replace("/", "\\"); if (File.Exists(NSWebPath.GetServerPath(dir1))) { File.Move(NSWebPath.GetServerPath(dir1), NSWebPath.GetServerPath(dir2)); } }
/// <summary> /// 复制文件 /// </summary> /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param> /// <param name="dir2">目标位置,并指定新的文件名</param> public static void CopyFile(string dir1, string dir2) { dir1 = dir1.Replace("/", "\\"); dir2 = dir2.Replace("/", "\\"); if (File.Exists(NSWebPath.GetServerPath(dir1))) { File.Copy(NSWebPath.GetServerPath(dir1), NSWebPath.GetServerPath(dir2), true); } }
static LogFactory() { XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead(NSWebPath.GetServerPath("/XmlConfig/log4net.config"))); repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); }
/// <summary> /// 路径转换(转换成绝对路径) /// </summary> /// <param name="path"></param> /// <returns></returns> public static string WebPathTran(string path) { try { return(NSWebPath.GetServerPath(path)); } catch { return(path); } }
/// <summary> /// 创建文件 /// </summary> /// <param name="dir">带后缀的文件名</param> /// <param name="pagestr">文件内容</param> public static void CreateFile(string dir, string pagestr) { dir = dir.Replace("/", "\\"); if (dir.IndexOf("\\") > -1) { CreateDir(dir.Substring(0, dir.LastIndexOf("\\"))); } System.IO.StreamWriter sw = new System.IO.StreamWriter(NSWebPath.GetServerPath(dir), false, System.Text.Encoding.GetEncoding("GB2312")); sw.Write(pagestr); sw.Close(); }
/// <summary> /// 删除目录 /// </summary> /// <param name="dir">要删除的目录路径和名称</param> public static void DeleteDir(string dir) { if (dir.Length == 0) { return; } if (Directory.Exists(NSWebPath.GetServerPath(dir))) { Directory.Delete(NSWebPath.GetServerPath(dir)); } }
public Log4netProvider() { XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead(NSWebPath.GetServerPath("/XmlConfig/log4net.config"))); repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); //log4net.Config.XmlConfigurator.Configure(new FileInfo(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config", "log4net.xml"))); log4netLogger = log4net.LogManager.GetLogger(this.GetType()); }
/// <summary> /// 清空指定目录下所有文件及子目录,但该目录依然保存. /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static void ClearDirectory(string directoryPath) { directoryPath = NSWebPath.GetServerPath(directoryPath); if (IsExistDirectory(directoryPath)) { //删除目录中所有的文件 string[] fileNames = GetFileNames(directoryPath); for (int i = 0; i < fileNames.Length; i++) { DeleteFile(fileNames[i]); } //删除目录中所有的子目录 string[] directoryNames = GetDirectories(directoryPath); for (int i = 0; i < directoryNames.Length; i++) { DeleteDirectory(directoryNames[i]); } } }
protected void Write_Txt(string FileName, string Content) { Encoding code = Encoding.GetEncoding("gb2312"); string htmlfilename = NSWebPath.GetServerPath("Precious\\" + FileName + ".txt"); //保存文件的路径 string str = Content; StreamWriter sw = null; { try { sw = new StreamWriter(htmlfilename, false, code); sw.Write(str); sw.Flush(); } catch { } } sw.Close(); sw.Dispose(); }
/// <summary> /// 生成Flv视频的缩略图 /// </summary> /// <param name="vFileName">视频文件地址</param> /// <returns>返回视频缩略图位置</returns> public string CatchImg(string vFileName) { if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(NSWebPath.GetServerPath(vFileName)))) { return(""); } try { string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + ".jpg"; string Command = " -i " + NSWebPath.GetServerPath(vFileName) + " -y -f image2 -t 0.1 -s " + sizeOfImg + " " + NSWebPath.GetServerPath(flv_img_p); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = ffmpegtool; p.StartInfo.Arguments = Command; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; try { p.Start(); } catch { return(""); } finally { p.Close(); p.Dispose(); } System.Threading.Thread.Sleep(4000); //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; if (System.IO.File.Exists(NSWebPath.GetServerPath(flv_img_p))) { return(flv_img_p); } return(""); } catch { return(""); } }
/// <summary> /// Get the absolute path of the file, for the window program and web program can be used /// </summary> /// <param name="relativePath">Relative path address</param> /// <returns>Absolute path address</returns> public static string GetAbsolutePath(string relativePath) { if (string.IsNullOrEmpty(relativePath)) { throw new ArgumentNullException("para relativePath is not null!"); } relativePath = relativePath.Replace("/", "\\"); if (relativePath[0] == '\\') { relativePath = relativePath.Remove(0, 1); } //Judgment is the Web program or window program if (NSHttpContext.Current != null) { return(NSWebPath.GetServerPath(relativePath));//Path.Combine(NSHttpRuntime.AppDomainAppPath, relativePath); } else { return(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)); } }
/// <summary> /// 根据Key修改Value /// </summary> /// <param name="key">要修改的Key</param> /// <param name="value">要修改为的值</param> public static void SetValue(string key, string value) { System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(NSWebPath.GetServerPath("/XmlConfig/Config.xml")); System.Xml.XmlNode xNode; System.Xml.XmlElement xElem1; System.Xml.XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']"); if (xElem1 != null) { xElem1.SetAttribute("value", value); } else { xElem2 = xDoc.CreateElement("add"); xElem2.SetAttribute("key", key); xElem2.SetAttribute("value", value); xNode.AppendChild(xElem2); } xDoc.Save(NSWebPath.GetServerPath("/XmlConfig/Config.xml")); }
public string CatchImg(string fileName, string imgFile) { string ffmpeg = NSWebPath.GetServerPath(VideoConvert.ffmpegtool); string flv_img = imgFile + ".jpg"; string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img; try { System.Diagnostics.Process.Start(ImgstartInfo); } catch { return(""); } if (System.IO.File.Exists(flv_img)) { return(flv_img); } return(""); }
/// <summary> /// 转换文件并保存在指定文件夹下 /// </summary> /// <param name="fileName">上传视频文件的路径(原文件)</param> /// <param name="playFile">转换后的文件的路径(网络播放文件)</param> /// <param name="imgFile">从视频文件中抓取的图片路径</param> /// <returns>成功:返回图片虚拟地址;失败:返回空字符串</returns> public string ChangeFileVir(string fileName, string playFile, string imgFile) { string ffmpeg = NSWebPath.GetServerPath(VideoConvert.ffmpegtool); if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName))) { return(""); } string flv_img = System.IO.Path.ChangeExtension(NSWebPath.GetServerPath(imgFile), ".jpg"); string flv_file = System.IO.Path.ChangeExtension(NSWebPath.GetServerPath(playFile), ".flv"); string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img; System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file; try { System.Diagnostics.Process.Start(FilestartInfo); System.Diagnostics.Process.Start(ImgstartInfo); } catch { return(""); } ///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; ///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败; if (System.IO.File.Exists(flv_img)) { return(flv_img); } return(""); }
/// <summary> /// 本地路径 /// </summary> /// <param name="path"></param> /// <returns></returns> public static string MapPath(string path) { return(NSWebPath.GetServerPath(path)); }
public RequestWrapper LoadSettingXml(string url) { settingXml = XElement.Load(NSWebPath.GetServerPath(url)).ToString(); return(this); }
/// <summary> /// 获取文件相对路径映射的物理路径 /// </summary> /// <param name="virtualPath">文件的相对路径</param> public static string GetPath(string virtualPath) { return(NSWebPath.GetServerPath(virtualPath)); }
/// <summary> /// 实现解压缩,需要rar.exe上传到网站根目录 /// </summary> /// <param name="s"></param> /// <param name="d"></param> /// <example>unrar("e:/www.svnhost.cn.rar", "e:/");</example> public static void UnRar(string s, string d) { ExeCommand(NSWebPath.GetServerPath("rar.exe") + " x \"" + s + "\" \"" + d + "\" -o+"); }