本文整理汇总了C#中FileInfo.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.Replace方法的具体用法?C# FileInfo.Replace怎么用?C# FileInfo.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileInfo
的用法示例。
在下文中一共展示了FileInfo.Replace方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// Write a program that removes from a text file
// all words listed in given another text file.
// Handle all possible exceptions in your methods.
static void Main()
{
try
{
string path = "../../text12.txt";
string backupPath = "../../text12Backup.txt";
string pattern = @"\b(" + String.Join("|", File.ReadAllLines("../../listOfWords.txt")) + @")\b";
using (StreamReader sr = new StreamReader(path))
using (StreamWriter sw = new StreamWriter("../" + path))
{
string line = sr.ReadLine();
while (line != null)
{
sw.WriteLine(Regex.Replace(line, pattern, ""));
line = sr.ReadLine();
}
}
//create new file,delete temp and create backup of original
FileInfo fi = new FileInfo("../" + path);
fi.Replace(path, backupPath);
}
catch (Exception ex)
{
Console.WriteLine("Oops something went wrong!" + "\n" + ex.Message);
}
}
示例2: Main
static void Main(string[] args)
{
var outputDirPath = System.IO.Path.Combine(Application.StartupPath, "Output");
if (!Directory.Exists(outputDirPath))
{
try
{
Directory.CreateDirectory(outputDirPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
return;
}
}
foreach (var fn in Directory.GetFiles(Application.StartupPath, "Template_*.dll"))
{
var asm = Assembly.LoadFile(fn);
var t = TemplateScaner.GetTemplate(asm);
var shortfn = new FileInfo(fn).Name;
shortfn = shortfn.Substring(0, shortfn.LastIndexOf('.'));
t.Name = shortfn.Substring("Template_".Length);
var path = System.IO.Path.Combine(outputDirPath, shortfn.Replace(".", "_"));
if (!Directory.Exists(path))
{
try
{
Directory.CreateDirectory(path);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
return;
}
}
LoadSaves.LoadSaveAll(t, outputDirPath);
GenCPP.Gen(t, path);
// more gen here
}
Process.Start("explorer.exe", outputDirPath);
}
示例3: Post
public HttpResponseMessage Post(string action, string dirPath = "")
{
WebUtils.CheckRightsForAdminPostPages(false);
HttpPostedFile file = HttpContext.Current.Request.Files[0];
action = action.ToLowerInvariant();
if (file != null && file.ContentLength > 0)
{
var dirName = string.Format("/{0}/{1}", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"));
var fileName = new FileInfo(file.FileName).Name; // to work in IE and others
// iOS sends all images as "image.jpg" or "image.png"
fileName = fileName.Replace("image.jpg", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
fileName = fileName.Replace("image.png", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png");
if (!string.IsNullOrEmpty(dirPath))
dirName = dirPath;
if (action == "filemgr" || action == "file")
{
string[] ImageExtensnios = { ".jpg", ".png", ".jpeg", ".tiff", ".gif", ".bmp" };
if (ImageExtensnios.Any(x => fileName.ToLower().Contains(x.ToLower())))
action = "image";
else
action = "file";
}
var dir = BlogService.GetDirectory(dirName);
var retUrl = "";
if (action == "import")
{
if (Security.IsAdministrator)
{
return ImportBlogML();
}
}
if (action == "profile")
{
if (Security.IsAuthorizedTo(Rights.EditOwnUser))
{
// upload profile image
dir = BlogService.GetDirectory("/avatars");
var dot = fileName.LastIndexOf(".");
var ext = dot > 0 ? fileName.Substring(dot) : "";
var profileFileName = User.Identity.Name + ext;
var imgPath = HttpContext.Current.Server.MapPath(dir.FullPath + "/" + profileFileName);
var image = Image.FromStream(file.InputStream);
Image thumb = image.GetThumbnailImage(80, 80, () => false, IntPtr.Zero);
thumb.Save(imgPath);
return Request.CreateResponse(HttpStatusCode.Created, profileFileName);
}
}
if (action == "image")
{
if (Security.IsAuthorizedTo(Rights.EditOwnPosts))
{
var uploaded = BlogService.UploadFile(file.InputStream, fileName, dir, true);
return Request.CreateResponse(HttpStatusCode.Created, uploaded.AsImage.ImageUrl);
}
}
if (action == "file")
{
if (Security.IsAuthorizedTo(Rights.EditOwnPosts))
{
var uploaded = BlogService.UploadFile(file.InputStream, fileName, dir, true);
retUrl = uploaded.FileDownloadPath + "|" + fileName + " (" + BytesToString(uploaded.FileSize) + ")";
return Request.CreateResponse(HttpStatusCode.Created, retUrl);
}
}
if (action == "video")
{
if (Security.IsAuthorizedTo(Rights.EditOwnPosts))
{
// default media folder
var mediaFolder = "Custom/Media";
// get the mediaplayer extension and use it's folder
var mediaPlayerExtension = BlogEngine.Core.Web.Extensions.ExtensionManager.GetExtension("MediaElementPlayer");
mediaFolder = mediaPlayerExtension.Settings[0].GetSingleValue("folder");
var folder = Utils.ApplicationRelativeWebRoot + mediaFolder + "/";
//var fileName = file.FileName;
UploadVideo(folder, file, fileName);
return Request.CreateResponse(HttpStatusCode.Created, fileName);
}
}
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
示例4: ReplaceSubstringInText
public static void ReplaceSubstringInText(string path, string search, string replace, string backupPath)
{
using (StreamReader sr = new StreamReader(path))
using (StreamWriter sw = new StreamWriter("../" + path))
{
char[] ch = new char[search.Length];
while (sr.Peek() != -1)
{
//check if array equals hunting string
if (new string(ch) == search)
{
sw.Write(replace);
for (int i = 0; i < ch.Length; i++)
{
ch[i] = (char)0;
}
}
else
{
if (ch[0] != (char)0)
{
sw.Write(ch[0]);
}
//shift left char array
for (int i = 0; i < ch.Length; i++)
{
if (i < ch.Length - 1)
{
ch[i] = ch[i + 1];
}
else
{
//read chars while they are \n or \r
while (((char)sr.Peek() == (char)10 || (char)sr.Peek() == (char)13) && false)
{
sr.Read();
}
ch[i] = (char)sr.Read();
}
}
}
}
//check if last word is our hunting string
if (new string(ch) == search)
{
sw.Write(replace);
}
else
{
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] != (char)0)
{
sw.Write(ch[i]);
}
}
}
}
//create new file,delete temp and create backup of original
FileInfo fi = new FileInfo("../" + path);
fi.Replace(path, backupPath);
}