Friend,
Make sure your txt data is uniform in future so its easy to write logic.
I am happy it helps you and I am suggesting you a logic for reading a file.
Before begin my assumptions on txt file are :
1] data written in file is such that each entry is on newline. {or some separator }
2] file is accessible and you have permissions to read it.
Now follow below links and implement it in your code.
links
http://msdn.microsoft.com/en-us/library/vstudio/ezwyzy7b(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.90).aspx
This is basic code for file access.
You can read all data in array or else, as below :
using (SPSite oSPsite = new SPSite("http://website url/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
SPList list = oSPWeb.Lists["MyList"];
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\timesheetfile.txt");
SPListItem itemToAdd = null;
foreach (string line in lines)
{
string[] arr = line.Split(",");
itemToAdd = list.Items.Add();
itemToAdd["EmpName"] = Convert.Tostring(arr[0]);
itemToAdd["EmpNumber"] = Convert.Tostring(arr[1]);
itemToAdd["TimeIn"] = Convert.Tostring(arr[2]);
itemToAdd["TimeOut"] = Convert.Tostring(arr[3]);
itemToAdd.Update();
}
list.Update();
oSPWeb.AllowUnsafeUpdates = false;
}
}
For scheduling :
I can suggest many way's. Prefer what you want.
Way 1] Create a Timer job : This will read .txt file and use server object model to perform CRUD operation. Use this solution in case you do not need instant entry. You can schedule it for hourly so it will read entry on hourly basis.
follow link to write and understand timer :
http://lamahashim.blogspot.in/2010/02/creating-timer-job-in-moss.html[
^]
Way 2] Create you own Windows utility {.exe} and schedule it in windows OS scheduler. .exe can be console application which contains server side code to perform CRUD operations. Here you need to add SharePoint dll.
Personally i do not prefer it because put .exe on production is not good idea.
Way 3] You can use default service provided by SharePoint like List.asmx
In this case you can create empty SharePoint project, add application page the add reference of list.asmx in it. Then read data from .txt file and upload into list.
Refer this blog :
http://blogs.msdn.com/b/pranab/archive/2008/11/24/sharepoint-2007-moss-wss-using-lists-asmx-getlistitems.aspx[
^]
Refer my answer as well :
C# code to upload document to Sharepoint without using Microsoft.Sharepoint DLL[
^]
Way 4] Create your own WCF service to perform CRUD operations and read txt file.
Host it in IIS or in share point to perform operations.
You can integrate it with Timer job or with custom utility {.exe}
Oh that was huge solution.
Hope you are good to go :)