I have some file paths stored in a List and need to attach them to an email. But how can I access the values (in my case: file paths as string values) of my List Items?
Here is the code:
List<string> filesToSend = new List<string>();
filesToSend = (List<string>)Session["filesListForFilesToSend"];
for (int i = 0; i < filesToSend.Count; i++)
{
//message.Attachments.Add(filesToSend[i].????????????????????);
}
Thanks in advance
filesToSend[i] will return the path string you want
Try This
foreach(string EachString in filesToSend)
{
message.Attachments.Add(EachString)
}
First, you do not need to instance a first list the after read the list in session, just:
List<string> filesToSend = (List<string>)Session["filesListForFilesToSend"];
When you access and List by index you will get the object of generic type. You can do it using a lot of ways, for sample:
using for loop:
for (int i = 0; i < filesToSend.Count; i++)
message.Attachments.Add(filesToSend[i]);
or foreach
foreach(string file in filesToSend)
message.Attachments.Add(file);
or while
int i = filesToSend.Lenght;
while(i--)
message.Attachments.Add(filesToSend[i]);
I would use foreach statement, but while will give you more performance (keep in mind you will loop in the reverse order).
The error was not how I was trying to get the string out of my List. The error was how I was trying to attach it to my message.
for (int i = 0; i < filesToSend.Count; i++)
{
string filePath = filesToSend[i];
Attachment attached = new Attachment(filePath);
attached.Name = filePath;
message.Attachments.Add(attached);
}
Thats the way it works for me. Thank you all
Related
This is my property class:
class Actions
{
public string[] Style { get; set; }
}
and this is my main method:
Actions action = new Actions();
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
foreach (var item in list)
{
for (int i = 0; i < action.Style.Length; i++)
{
action.Style[i] = item.ToString();
Console.WriteLine(action.Style[i]);
}
}
How do I fill the property with list items?
This gives me a exception:
"object reference not set to an instance of an object".
There is no need to add your items one by one, you could just use the ToArray() method of your list like so:
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
Actions action = new Actions {
Style = list.ToArray()
};
As has already been pointed out, Style is always null, given the code you have shared. #Eldeniz and #paul have shared different ways to fix that. Obviously, your sample code is just a sample fragment, so here are 2 other options you could consider if the previous two don't work for whatever reason (I'm just free-handing this, please excuse any typos).
1) You can have your Actions class always return a not-null object
class Actions
{
private string[] _style;
public string[] Style
{
get { return _style ?? new string[0]; }
set { _style = value; }
}
}
Note that this will allow you to always see the output of the style property as requested, assuming an empty array and null are, for your purposes, the same thing.
2) You can make your loop tolerant to null values
foreach (var item in list)
{
for (int i = 0; i < action?.Style.Length ?? 0; i++)
{
action.Style[i] = item.ToString();
Console.WriteLine(action.Style[i]);
}
}
Finally, just as a tip, if you have your debugger attached and you are stepping through your code, Visual Studio will help you pinpoint these sorts of errors pretty easily. Take the time to become friends with your debugger. If it gives you an error you don't understand, do a quick web search. Your future self will thank you.
You must create an instance of the Style property
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
Actions action = new Actions();
action.Style=new string[list.Count];
foreach (var item in list)
{
for (int i = 0; i < action.Style.Length; i++)
{
action.Style[i] = item.ToString();
Console.WriteLine(action.Style[i]);
}
}
I have made a collection to store objects of class sendviaemail
public List<SendViaEmail> email = new List<SendViaEmail>();
The class has a string variable to store emailid. I am adding one object to the list email.
email.Add(s);
Now the s object is of class type email and it contains an emailed(For eg sad#gmail.com)
When I use foreach loop to iterate through all the values in object and add all the emailids in listbox but no data is displayed in listbox
SendViaEmail s = new SendViaEmail();
for (int i = 0; i < s.email.Count(); i++)
{
listBox1.Items.Add(s.email);
}
I tried debugging and in the s object the email = null .
I don't think the code to store email address in the collection works because I m getting null value when I retrieve data.
I m having the email address in string.
How to store string in collection which can hold only objects of class
Firstly, you should be using a Typed List.
That means the List can only contain elements of the SendViaEmail object.
public List<SendViaEmail> email = new List<SendViaEmail>();
You can then add SendViaEmail objects to it.
SendViaEmail s = new SendViaEmail();
email.add(s);
Your loop structure can now take advantage of foreach:
foreach(SendViaEmail s in email){
listBox1.Items.Add(s.email);
}
Try setting the DisplayMember property of listBox1 to "" and then "email".
listBox1.DisplayMember = "";
listBox1.DisplayMember = "email";
This should refresh the view.
Use a foreach loop rather than a for.
foreach (string email in s.email)
{
listBox1.Items.Add(email);
}
If you want to use a for loop, you need to use the index to get the current item.
for (int i = 0; i < s.email.Count(); i++)
{
listBox1.Items.Add(s.email[i]);
}
You are adding the list multiple times, instead of adding each of its element.
This is what you are supposed to do :
SendViaEmail s = new SendViaEmail();
for (int i = 0; i < s.email.Count(); i++)
{
listBox1.Items.Add(s.email[i]);
}
Alternatively, you could use foreach as well :
SendViaEmail s = new SendViaEmail();
foreach (var item in s.email)
{
listBox1.Items.Add(item);
}
I'm after some help with how to query a list and return back the index, but not using Linq. I've seen many example where Linq is used, but the software I'm writing the query into doesn't support Linq. :(
So example to get us going:
List<string> location = new List<string>();
location.add(#"C:\test\numbers\FileName_IgnoreThis_1.jpg");
location.add(#"C:\test\numbers\FileName_IgnoreThis_2.jpg");
location.add(#"C:\test\numbers\FileName_ImAfterThis_3.jpg");
location.add(#"C:\test\numbers\FileName_IgnoreThis_4.jpg");
location.add(#"C:\test\numbers\FileName_ImAfterThis_5.jpg");
So this list will be populated with probably a few hundred records, what I need to do is query the list for the text "ImAfterThis" then return the index number location for this item into a string array but without using Linq.
The desired result would be 2 and 4 being added to the string array.
I was thinking of doing a for loop through the list, but is there a better way to achieve this?
List<int> results = new List<int>();
int i = 0;
foreach (string value in location)
{
if (value.Contains("IAfterThis"))
{
results.Add(i);
Console.WriteLine("Found in Index: " + i);
}
i++;
}
Console.ReadLine();
Thanks in advance.
If you want to get just the first occurrence you could simply use the IndexOf() method.
If you want all occurrences of string "whatever" then a for loop would certainly work for you. For the sake of argument here I've capture the indexes in another list:
string MyString = "whatever";
List<int> indexes = new List();
for (int i = 0; i < location.Count; i++)
{
if (location[i] == MyString)
{
indexes.Add(i);
}
}
In an ASP.NET application I want to process uploaded files. The HttpContext.Current.Request.Files.AllKeys contains the following:
[0]File2
[1]File2
[2]flTeklif
[3]flTeklif
[4]flTeklif
[5]flTeklif
How can I select only the uploaded files that have key flTeklif into a List<HttpPostedFile>?
I tried this:
var uploads = HttpContext.Current.Request.Files.AllKeys
.Where(s=>s.stringname == "flTeklif")
But that only selects the keys, not the files. How can I select the Files.Where(key == "flTeklif")?
HttpRequest.Files is an HttpFileCollection, whose AllKeys property is a string array.
So you can just use AllKeys.Where(s => s == "flTeklif").
So far for the literal interpretation of your question, which is probably why you're pretty heavily down- and closevoted as it doesn't really make any sense.
If your actual question is "How can I select the files that have flTeklif as their key", use:
var files = HttpContext.Current.Request.Files;
var result = new List<HttpPostedFile>();
for (int i = 0; i < files.AllKeys.Count; i++)
{
if (files.AllKeys[i] == "flTeklif")
{
result.Add(files.AllKeys[i]);
}
}
Then result will contain the files you're interested in.
OK i understand. may be
HttpContext.Current.Request.Files.Cast<HttpPostedFile>().Where(c => c.FileName.Contains("flTeklif")).ToList();
Use HttpFileCollection.GetKey(Int32) method. There is no mention that AllKeys array order is the similiar to the files HttpFileCollection.
int i;
HttpFileCollection MyFileColl = Request.Files;
for( i= 0; i< MyFileColl.Count; i++)
{
if( MyFileColl.GetKey(i) == "flTeklif")
{
//...
}
}
You can use the following code to get a list of all uploaded files against a particular key
HttpFileCollection files = null;
try { files = HttpContext.Current.Request.Files; } catch { }
var allUploadedFilesAgainstThisKey = files.GetMultiple(singleKey); // singleKey is a string
// allUploadedFilesAgainstThisKey is of type IList<HttpPostedFile>
Here is my Code....
Microsoft.Office.Interop.Word.Application applicationObject = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document aDDoc = applicationObject.Documents.Open(FileName: FilePath);
Microsoft.Office.Interop.Word.Hyperlinks links = aDDoc.Hyperlinks;
for (int i = 0; i < links.Count; i++)
{
object index = (object)i;
string c = links[index].Target; //Here i am getting Com exception, see below
}
Com exception: the inner exception is null, Where the link count iam getting correct. Please tell me if any one have the idea how to retrieve the Hyperlink URL.
I was also facing the same issue but resolved using below code correction.
Instead of using:
for (int i = 0; i < links.Count; i++)
use this:
for (int i = 1; i <= links.Count; i++)
string c = links[i].Target
Should work fine.
try this:
foreach (Hyperlink link in links)
{
string c = link.Target;
}
Here is an example, where all fields' (all hyperlinks are fields) starting nodes are selected and then are executed in way you want. Check if you get the same com exception error on line with calling .Target property.