Creating a xml sitemap from an url with c# - c#

I'm trying to create a utility that will create a xml sitemap using c#. I'm able to point it at a local folder and produce results that display in a browser, but am having trouble when I point it at a website. Any tips or hints would be appreciated! I'm rather new to c#.
static void Main(string[] args)
{
//String sDir = #"WWW.WEBSITE.COM";<---I'd like to index a site rather than a file.
String sDir = #"FOLDER_LOCATION";
String siteMap = #"FOLDER_LOCATION\sitemapworking.xml";//Where I'd like to save the sitemap
var sb = new System.Text.StringBuilder();
String[] allfiles = System.IO.Directory.GetFiles(sDir, "*", System.IO.SearchOption.AllDirectories);
String[] alldirects = System.IO.Directory.GetDirectories(sDir, "*", System.IO.SearchOption.AllDirectories);
sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset>");
sb.AppendLine("<url>\n\t<loc>" + sDir + "</loc>\n</url>");
foreach (string s in alldirects)
{
sb.AppendLine("<url>\n\t<loc>" + s + "</loc>\n</url>");
}
foreach (string s in allfiles)
{
System.IO.FileInfo file = new System.IO.FileInfo(s);
sb.AppendLine("<url>\n\t<loc>" + s + "</loc>\n\t<lastmod> " + file.LastWriteTime + "</lastmod>\n</url>");
}
sb.AppendLine("</urlset>");
File.WriteAllText(siteMap, sb.ToString());
}

Related

Delete specific line from a text file which i don't have the name

i need to find and delete all lines wich contain the word "recto",
i did search in stackoverflow forum, but all what i found is do that (delete the line) using path (Directory & FileName).
in my case i want to delete the line contain "recto" in all fils with specific extention (*.txt) in the directory.
thanks for help
here is my code so far
string sourceDir = #"C:\SRCE\";
string destinDir = #"C:\DIST\";
string[] files = Directory.GetFiles(sourceDir);
foreach (string file in files)
{
using (StreamReader sr_ = new StreamReader
(sourceDir + Path.GetFileName(file)))
{
string line = sr_.ReadLine();
if (line.Contains("recto"))
{
File.Copy(file, destinDir + Path.GetFileName(file));
string holdName = sourceDir + Path.GetFileName(file);
}
sr_.DiscardBufferedData();
sr_.Close();
}
}
}
You can try something like this. You were only identifying the files with the word but not making any try to remove it. At the end, you were copying the files that included the word "recto"
string sourceDir = #"C:\SRCE\";
string destinDir = #"C:\DIST\";
string[] files = Directory.GetFiles(sourceDir);
foreach (string file in files)
{
using (StreamReader sr_ = new StreamReader
(sourceDir + Path.GetFileName(file)))
{
string res = string.Empty;
while(!sr_.EndOfStream)
{
var l = sr_.ReadLine();
if (l.Contains("recto"))
{
continue;
}
res += l + Environment.NewLine;
}
var streamWriter = File.CreateText(destinDir + Path.GetFileName(file));
streamWriter.Write(res);
streamWriter.Flush();
streamWriter.Close();
}
}
If the files are not really big you can simplify a lot your code reading all lines in memory, processing the lines with Linq and then rewriting the files
string sourceDir = #"C:\SRCE\";
string destinDir = #"C:\DIST\";
string[] files = Directory.GetFiles(sourceDir);
foreach (string file in files)
{
var lines = File.ReadLines(file);
var result = lines.Where(x => x != "recto").ToArray();
File.WriteAllLines(Path.Combine(destinDir, Path.GetFileName(file)), result);
}

Get files from a folder that I have created in Xamarin.Android

I want get all files from an external storage folder(wall_e_imgs)..Here are codes-
public void getImages()
{
var path1 = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString();
string path = System.IO.Path.Combine(path1, "wall_e_imgs");
//var files= System.IO.Directory.GetFiles(Android.OS.Environment.ExternalStorageDirectory.ToString() + "wall_e_imgs");
//var files = System.IO.Directory.GetFiles(path);
//string path = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/wall_e_imgs";
//File directory=new File(path);
Java.IO.File directory = new Java.IO.File(path);
Java.IO.File[] files = directory.ListFiles();//always count is 0 even though there are lot files there
foreach (var i in files)
{
FileInfo info = new FileInfo(i.Name);
if (info.Name.Contains("Wall_e"))
{
di.Add(new DownloadedImages { Path1 = info.DirectoryName, Name1 = info.FullName });
}
}
}
But it always give 0 files even though there are lot of files.
Try this
var folder = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "yourfoldername";
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
var filesList = Directory.GetFiles(folder);
foreach (var file in filesList)
{
var filename = Path.GetFileName(file);
}
Try something like this:
// Use whatever folder path you want here, the special folder is just an example
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "wall_e_imgs");
if (Directory.Exists(folderPath))
{
var files = Directory.EnumerateFiles(folderPath);
foreach (var file in files)
{
// Do your stuff
}
}
Please note that this uses the Directory class from System.IO, not Java.IO
ffilelist will contain a list of mp3 files in "/storage/emulated/0/Music/"
string phyle;
string ffilelist = "";
public void listfiles()
{
try
{
var path1 = "/storage/emulated/0/Music/";
var mp3Files = Directory.EnumerateFiles(path1, "*.mp3", SearchOption.AllDirectories);
foreach (string currentFile in mp3Files)
{
phyle = currentFile;
ffilelist = ffilelist + "\n" + phyle;
}
//playpath(phyle); // play the last file found
}
catch (Exception e9)
{
Toast.MakeText(ApplicationContext, "ut oh\n"+e9.Message , ToastLength.Long).Show();
}
}

How can I filter out lines in a richtextbox?

I have code that shows the name of all .dll files within a directory on a rich textbox. How would I be able to filter/hide all filenames which I specify as unimportant and keep the rest?
Example:
Actual directory contains: 1.dll, 2.dll, 3.dll
Rich Textbox shows: 1.dll, 3.dll because 2.dll is specified as unimportant in the code.
Code I currently have that displays all files.
DirectoryInfo r = new DirectoryInfo(#"E:\SteamLibrary\steamapps\common\Grand Theft Auto V");
FileInfo[] rFiles = r.GetFiles("*.dll");
string rstr = "";
foreach (FileInfo rfile in rFiles)
{
rstr = rstr + rfile.Name + " ";
}
string strfinalR;
strfinalR = richTextBox3.Text + rstr;
richTextBox3.Text = (strfinalR);
Just make a blacklist :
string[] blacklist = new string[] { "2", "1337" };
and filter the filenames within your foreach
foreach(FileInfo rFile in rFiles)
{
if(blacklist.Any(bl => rFile.Name.Contains(bl)))
continue;
// your code
}
or when you retrieve files from r
r.GetFiles("*.dll").Where(file => !blacklist.Any( type => file.Name.Contains(type))).ToArray();
one approach is to make a list of files to ignore and then fetch all files excluding those in ignore list. Something like this:
//ignore list
List<string> filesToIgnore = new List<string>() { "2.dll", "some_other.dll" };
//get files and filter them
DirectoryInfo r = new DirectoryInfo(#"E:\SteamLibrary\steamapps\common\Grand Theft Auto V");
List<FileInfo> rFiles = r.GetFiles("*.dll").Where(x => !filesToIgnore.Contains(x.Name)).ToList();
//your existing code follows
string rstr = "";
foreach (FileInfo rfile in rFiles)
{
rstr = rstr + rfile.Name + " ";
}
string strfinalR;
strfinalR = richTextBox3.Text + rstr;
richTextBox3.Text = (strfinalR);

find and replace in file xml

i want open file xml and find string then replace.
but when replace string Only to find two strings and replace
this my code
var realpath = "~/template/xml/xmlback";
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/template/xml/xmltest") + ".xml");
var filePath2 = Path.Combine(HttpContext.Current.Server.MapPath("~/template/xml/xmlback/test2") + ".xml");
DirectoryInfo dir = new DirectoryInfo(Path.Combine(HttpContext.Current.Server.MapPath(realpath)));
foreach (FileInfo files in dir.GetFiles())
{
files.Delete();
}
string strVal = System.IO.File.ReadAllText(Server.MapPath("~/template/xml/xmltest") + ".xml");
strVal = strVal.Replace("Test1", "amir");
strVal = strVal.Replace("Test2", "amir1");
strVal = strVal.Replace("Test3", "amir2");
strVal = strVal.Replace("Test4", "amir3");
strVal = strVal.Replace("Test5", "amir4");
strVal = strVal.Replace("Test6", "amir5");
File.Copy(Path.Combine(filePath), Path.Combine(filePath2));
System.IO.File.WriteAllText(Server.MapPath("~/template/xml/xmlback/test2") + ".xml", strVal);
ProcessRequest(filePath2, Lcourseid.Text);
im try open xml with word and see resultpic
I've solved my issue
iam use this code But no difference with the above code does not
I think it was a corrupted file xml,
Could not find Test3 and Test4,...
Thank you all
Dictionary<string, string> wordsToReplace = new Dictionary<string, string>();
wordsToReplace.Add("Test1", "amir");
wordsToReplace.Add("Test2", "amir1");
wordsToReplace.Add("Test3", "amir3");
wordsToReplace.Add("Test4", "amir4");
wordsToReplace.Add("Test5", "amir5");
wordsToReplace.Add("Test6", "amir6");
string strVal = System.IO.File.ReadAllText(Server.MapPath("~/template/xml/xmltest") + ".xml");
foreach (var pair in wordsToReplace)
{
//Performs each replacement
strVal = strVal.Replace(pair.Key, pair.Value);
}
File.Copy(Path.Combine(filePath), Path.Combine(filePath2));
System.IO.File.WriteAllText(Server.MapPath("~/template/xml/xmlback/test2") + ".xml", strVal);
ProcessRequest(filePath2, Lcourseid.Text);

C# copying multiple files with wildcards and keeping file names

I need to copy multiple files from a directory using a textfile that doesnt contain complete info.
NCR.txt:
Red
target directory has in it:
red1.txt
red3.txt
red44.txt
dest directory needs to have:
red1.txt
red3.txt
red44.txt
My code:
System.IO.Directory.CreateDirectory(#"C:\nPrep\" + textBox1.Text + "\\red");
if (checkBox3.Checked)
{
String[] file_names = File.ReadAllLines(#"C:\NCR.txt");
foreach (string file_name in file_names)
{
string[] files = Directory.GetFiles(textBox2.Text, file_name + "*.txt");
foreach (string file in files)
System.IO.File.Copy(file, #"C:\nPrep\" + textBox1.Text + "\\red\\");
}
}
//FileInfo & DirectoryInfo are in System.IO
//This is something you should be able to tweak to your specific needs.
static void CopyFiles(DirectoryInfo source,
DirectoryInfo destination,
bool overwrite,
string searchPattern)
{
FileInfo[] files = source.GetFiles(searchPattern);
//this section is what's really important for your application.
foreach (FileInfo file in files)
{
file.CopyTo(destination.FullName + "\\" + file.Name, overwrite);
}
}
This version is more copy-paste ready:
static void Main(string[] args)
{
DirectoryInfo src = new DirectoryInfo(#"C:\temp");
DirectoryInfo dst = new DirectoryInfo(#"C:\temp3");
/*
* My example NCR.txt
* *.txt
* a.lbl
*/
CopyFiles(src, dst, true);
}
static void CopyFiles(DirectoryInfo source, DirectoryInfo destination, bool overwrite)
{
List<FileInfo> files = new List<FileInfo>();
string[] fileNames = File.ReadAllLines("C:\\NCR.txt");
foreach (string f in fileNames)
{
files.AddRange(source.GetFiles(f));
}
if (!destination.Exists)
destination.Create();
foreach (FileInfo file in files)
{
file.CopyTo(destination.FullName + #"\" + file.Name, overwrite);
}
}
All suggestions were great and thansk for all the advise but this was perfect:
if (checkBox3.Checked)
{
string[] lines = File.ReadAllLines(#"C:\NCR.txt");
foreach (string line in lines)
{
string[] files = Directory.GetFiles(textBox2.Text, line + "*.txt");
foreach (string file in files)
{
FileInfo file_info = new FileInfo(file);
File.Copy(file, #"C:\InPrep\" + textBox1.Text + "\\text\\" + file_info.Name);
}
}
}
string sourceDir = #"c:\";
string destDir = #"c:\TestDir";
var r = Directory.GetFiles(sourceDir, "red*.txt"); //Replace this part with your read from notepad file
foreach (var s in r)
{
var sourceFile = new FileInfo(s);
sourceFile.CopyTo(destDir + "\\" + s.Replace(sourceDir, string.Empty));
}

Categories