WebClient DownloadFile Illegal characters in path - c#

I'm a newbie, so I'm sure this is something really basic that I'm missing.
I have a simple program to run through a csv file that contains links to images to save those images in the specified save file location.
I am parsing the cell that contains the url into a List<string[]>.
If I put GetImage(#"http://www.example.com/picture.jpg", 1) my GetImage function performs as it should. When I try to use the loop and pass in the str[0] variable, I receive an error about illegal characters in path.
I've used a MessageBox to tell me what the difference is and as far as I can tell, when I pass the str[0] into the function it adds double quotes(i.e., "http://www.example.com" is displayed instead of http://www.example.com as it is when I just send the one string.
What am I doing wrong?
private void button2_Click(object sender, EventArgs e)
{
string fileName = textBox1.Text;
folderBrowserDialog1.ShowDialog();
string saveLocation = folderBrowserDialog1.SelectedPath;
textBox2.Text = saveLocation;
List<string[]> file = parseCSV(fileName);
int count = 0;
foreach (string[] str in file)
{
if (count != 0)
{
GetImage(str[0], str[4]);
}
count++;
}
//GetImage(#"http://www.example.com/picture.jpg", "1");
}
private void GetImage(string url, string prodID)
{
string saveLocation = textBox2.Text + #"\";;
saveLocation += prodID + ".jpg";
WebClient webClt = new WebClient();
webClt.DownloadFile(url, saveLocation);
}

No matter which function or method creates these quotes, you could replace them all.
String myUrl = str[0];
myUrl = myUrl.Replace("\"", "");
GetImage(myUrl, str[4]);
I think your files contains the quotes or the parseCSV method creates them.
Update:
I used this code and it works with no problem at all and without quotes:
static void Main(string[] args)
{
string fileName = "Test";
//folderBrowserDialog1.ShowDialog();
string saveLocation = ".\\";
//textBox2.Text = saveLocation;
List<string[]> file = new List<string[]>
{
new string[] { "http://www.example.com", "1", "1", "1", "1"},
new string[] { "http://www.example.com", "2", "2", "2", "2"},
};
int count = 0;
foreach (string[] str in file)
{
if (count != 0)
{
GetImage(str[0], str[4]);
}
count++;
}
//GetImage(#"http://www.example.com/picture.jpg", "1");
}
private static void GetImage(string url, string prodID)
{
string saveLocation = ".\\"; // textBox2.Text + #"\"; ;
saveLocation += prodID + ".jpg";
WebClient webClt = new WebClient();
Console.WriteLine(url);
webClt.DownloadFile(url, saveLocation);
}

Related

C# how to get proper string from GetString()

I use:
propItem.Value = System.Text.Encoding.UTF8.GetBytes(textBox1.Text + "\0");
where textBox1.Text contains "MMM", to set the Value and save it in a file (propItem.Value is byte[]), but when I try to read the file I use:
string myString = System.Text.Encoding.UTF8.GetString(propItem.Value);
and get: "M\0M\0M\0\0\0". Could anybody advice how to get the proper string, without '\0'. I have seen all the answers here regarding the similar problems, but none of the answers worked in my case.
Loading the file:
Image img0 = null;
string sourceFile;
private void btnLoad_Click(object sender, EventArgs e)
{
using (var selectFileDialog = new OpenFileDialog())
{
if (selectFileDialog.ShowDialog() == DialogResult.OK)
{
sourceFile = selectFileDialog.FileName;
img0 = Image.FromFile(sourceFile);
PropertyItem[] propItems = img0.PropertyItems;
textBox1.Text = "Nothing in the file.";
foreach (PropertyItem propItem in propItems)
{
if (propItem.Id == 0x9286)
{
string myString = System.Text.Encoding.UTF8.GetString(propItem.Value);
textBox1.Text = myString ;
}
}
}
}
}
It should be:
string myString = System.Text.Encoding.Unicode.GetString(propItem.Value);

Create .url file based on url input (special characters) with C#

I am creating a program that creates .url files based on a url. The file is supposed to have the URL's html 'title' as name. With the content of the file being the url header. For example:
Input
https://www.youtube.com/watch?v=fRh_vgS2dFE
Output: File name
Justin Bieber - Sorry (PURPOSE : The Movement).url
Output: File content
[InternetShortcut]
URL=https://www.youtube.com/watch?v=4Tr0otuiQuU
however the problem arises when I insert songs like the one in the example. Since it has a character unsupported by filenames in Windows (:).
Code
string _Path = #"C:\Users\Public\Music\";
private void bNewSong_Click(object sender, EventArgs e)
{
if (lbPlaylists.SelectedItem != null && lbPlaylists.SelectedItem.ToString() != "")
{
string songURL = Microsoft.VisualBasic.Interaction.InputBox("Enter song URL:", "New", lbPlaylists.SelectedItem.ToString(), 800, 450);
if (songURL != "" && songURL.Contains(#"https://www.youtube.com/watch?v="))
{
WebClient x = new WebClient();
string source = x.DownloadString(songURL);
string title = Regex.Match(source, #"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
title = title.Remove(title.Length - 10);
string fullPath = _Path + lbPlaylists.SelectedItem.ToString() + "\\" + title + ".url";
if (!File.Exists(fullPath))
{
using (StreamWriter writer = new StreamWriter(fullPath))
{
string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=" + songURL);
writer.Flush();
}
}
else
{
MessageBox.Show("Song already in playlist.");
}
}
else
{
MessageBox.Show("Enter a new playlist name.");
}
}
else
{
MessageBox.Show("Select a playlist to add a song to.");
}
}
So my question is:
How do I format the title to be a acceptable file name?
Thanks in advance.
You can replace invalid characters returned by
Path.GetInvalidFileNameChars()
https://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(v=vs.110).aspx
For example:
foreach (var c in Path.GetInvalidFileNameChars())
fullPath = fullPath.Replace(c, '-');

How to read a text file's data vertically or column wise

How can we read a text file column by column.
Check my new code: I can read the data row-wise using text.split (' ')
But how can be the file read as column wise? Lets assume that a file contains number of rows and columns but I was able to read the data/value horizontally. The code you see that below that's what I could execute!
SEE THE CODE BELOW:-
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string text = "";
text = textBox1.Text;
string[] arr = text.Split(' ');
textBox2.Text = arr[5];
textBox3.Text = arr[8];
}
private void button3_Click(object sender, EventArgs e)
{
string file_name = "c:\\Excel\\count.txt";
string txtline = "";
System.IO.StreamReader objreader;
objreader = new System.IO.StreamReader(file_name);
do
{
txtline = txtline + objreader.ReadLine() + "\r\n";
txtline = txtline + objreader.ReadToEnd() + "";
this.textBox1.Text = "subzihut";
}
while (objreader.Peek() != -1);
textBox1.Text = txtline;
objreader.Close();
}
private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = textBox2.Text + " " + textBox3.Text;
}
}
}
A textfile contains a sequence of characters, delimited by newline characters and probably other characters which are used as delimiters (usually a comma or a semiciolon).
When you read a file you simply read this stream of characters. There are helper functions which read such a file line-by-line (using the newline character as a delimiter).
In plain .Net there are no methods which read column-by-column.
So you should:
read the file line by line
split each line into fields/columns using string.Split() at the separator character(s)
access only the columns of interest
You can simply read the file line by line, splitt the lines and do whatever you want.
var lines = File.ReadLines(#"c:\yourfile.txt");
foreach(var line in lines)
{
var values = line.Split(' ');
}
public string getColumnString(int columnNumber){
string[] lines = System.IO.ReadAllLines(#"C:\inputfile.txt");
string stringTobeDisplayed = string.Empty;
foreach(string line in lines) {
if(columnNumber == -1){ //when column number is sent as -1 then read full line
stringTobeDisplayed += line +"\n"
}
else{ //else read only the column required
string [] words = line.Split();
stringTobeDisplayed += word[columnNumber] +"\n"
}
}
return stringTobeDisplayed;
}
Maybe this will help you:
public static void ReadFile(string path)
{
List<string> Col1 = new List<string>();
List<string> Col2 = new List<string>();
List<string> Col3 = new List<string>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.EndOfStream)
{
string header = sr.ReadLine();
var values = header.Split(' ');
Col1.Add(values[0]);
Col2.Add(values[1]);
Col3.Add(values[2]);
}
}
}
It's true that sometimes you just don't know where to start. Here are some pointers.
You'll have to read the whole file in, probably using something like a StreamReader.
You can parse the first row into column names. Use StreamReader.ReadLine() to get the first line and then do some simple string parsing on it.
You'll want to create some kind of class/object to store and access your data.
Once you have column names, continue to parse the following lines into the proper arrays.
Some here's a rough idea
using(StreamReader sr = new StreamReadeR("C:\\my\\file\\location\\text.csv"))
{
string header = sr.ReadLine();
List<string> HeaderColumns = new List<string>(header.split(" ", StringSplitOptions.RemoveEmptyEntires));
myModelClass.Header = HeaderColumns;
etc...
You might also consider making some kind of dictionary to access columns by header name and index.

SSIS script to remove date from file name

I need to create a SSIS script to remove the date from file name. for example file name is: TestFile_122413.CSV I need to rename it to TestFile.CSV. I don't know how to keep file extension and how to deal with the date changes on file. I receive this file every day. Here is my code:
`public void Main()
// TODO: Add your code here
const string DIRECTORY_PATH = #"E:\ScriptsTest";
//const string FILE_NAME_TEMPLATE = "SSS_PROF_010113.CSV";
const string FILE_NAME_TEMPLATE = "*.CSV";
if (Directory.Exists(DIRECTORY_PATH))
{
string[] filePathList = Directory.GetFiles(DIRECTORY_PATH);
foreach (string filePath in filePathList)
{
if (File.Exists(filePath))
{
File.Move(filePath, filePath.Replace(FILE_NAME_TEMPLATE, FILE_NAME_TEMPLATE.Substring(0,8)));
}
}
}
}`
This should work. BTW, have you tried using the ForEach task? That may be simpler.
public void Main()
{
const string DIRECTORY_PATH = #"C:\temp\";
const string FILE_NAME_TEMPLATE = "*_??????.CSV";
int underscoreAt = 0;
if (Directory.Exists(DIRECTORY_PATH))
{
string[] filePathList = Directory.GetFiles(DIRECTORY_PATH,FILE_NAME_TEMPLATE);
foreach (string filePath in filePathList)
{
if (File.Exists(filePath))
{
underscoreAt = filePath.LastIndexOf('_');
string newName = string.Format ("{0}.CSV", filePath.Substring(0, underscoreAt));
File.Move(filePath,newName );
}
}
}
}
Check out the SSIS File System Task. It has an operation to rename a file.
Here is a video on how it works.
Hope this helps!
Eric
Try this, it compiles but I haven't run it:
public class Foo
{
public void Main()
{
const string DIRECTORY_PATH = #"E:\ScriptsTest";
if (Directory.Exists(DIRECTORY_PATH))
{
string[] filePathList = Directory.GetFiles(DIRECTORY_PATH);
foreach (string filePath in filePathList)
{
if (File.Exists(filePath))
{
// Get the file name
string fileName = Path.GetFileName(filePath);
// Get the file extension
string fileExtension = Path.GetExtension(filePath);
// Get the file name without the date part
string fileTitle = fileName.Substring(0, fileName.IndexOf("_"));
File.Move(filePath, DIRECTORY_PATH + #"\" + fileTitle + "." + fileExtension);
}
}
}
}
}

Remove words from string c#

I am working on a ASP.NET 4.0 web application, the main goal for it to do is go to the URL in the MyURL variable then read it from top to bottom, search for all lines that start with "description" and only keep those while removing all HTML tags. What I want to do next is remove the "description" text from the results afterwords so I have just my device names left. How would I do this?
protected void parseButton_Click(object sender, EventArgs e)
{
MyURL = deviceCombo.Text;
WebRequest objRequest = HttpWebRequest.Create(MyURL);
objRequest.Credentials = CredentialCache.DefaultCredentials;
using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream()))
{
originalText.Text = objReader.ReadToEnd();
}
//Read all lines of file
String[] crString = { "<BR> " };
String[] aLines = originalText.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries);
String noHtml = String.Empty;
for (int x = 0; x < aLines.Length; x++)
{
if (aLines[x].Contains(filterCombo.SelectedValue))
{
noHtml += (RemoveHTML(aLines[x]) + "\r\n");
}
}
//Print results to textbox
resultsBox.Text = String.Join(Environment.NewLine, noHtml);
}
public static string RemoveHTML(string text)
{
text = text.Replace(" ", " ").Replace("<br>", "\n");
var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
return oRegEx.Replace(text, string.Empty);
}
Ok so I figured out how to remove the words through one of my existing functions:
public static string RemoveHTML(string text)
{
text = text.Replace(" ", " ").Replace("<br>", "\n").Replace("description", "").Replace("INFRA:CORE:", "")
.Replace("RESERVED", "")
.Replace(":", "")
.Replace(";", "")
.Replace("-0/3/0", "");
var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
return oRegEx.Replace(text, string.Empty);
}
public static void Main(String[] args)
{
string str = "He is driving a red car.";
Console.WriteLine(str.Replace("red", "").Replace(" ", " "));
}
Output:
He is driving a car.
Note: In the second Replace its a double space.
Link : https://i.stack.imgur.com/rbluf.png
Try this.It will remove all occurrence of the word which you want to remove.
Try something like this, using LINQ:
List<string> lines = new List<string>{
"Hello world",
"Description: foo",
"Garbage:baz",
"description purple"};
//now add all your lines from your html doc.
if (aLines[x].Contains(filterCombo.SelectedValue))
{
lines.Add(RemoveHTML(aLines[x]) + "\r\n");
}
var myDescriptions = lines.Where(x=>x.ToLower().BeginsWith("description"))
.Select(x=> x.ToLower().Replace("description",string.Empty)
.Trim());
// you now have "foo" and "purple", and anything else.
You may have to adjust for colons, etc.
void Main()
{
string test = "<html>wowzers description: none <div>description:a1fj391</div></html>";
IEnumerable<string> results = getDescriptions(test);
foreach (string result in results)
{
Console.WriteLine(result);
}
//result: none
// a1fj391
}
static Regex MyRegex = new Regex(
"description:\\s*(?<value>[\\d\\w]+)",
RegexOptions.Compiled);
IEnumerable<string> getDescriptions(string html)
{
foreach(Match match in MyRegex.Matches(html))
{
yield return match.Groups["value"].Value;
}
}
Adapted From Code Project
string value = "ABC - UPDATED";
int index = value.IndexOf(" - UPDATED");
if (index != -1)
{
value = value.Remove(index);
}
It will print ABC without - UPDATED

Categories