windows service and file format exception.
I have a windows service that reads the files in directory on one server and loads them into another. So I' m using these lines of code to accomplish this. The problem is that I' m getting the format of the path specified is not the correct format. I' m not able to correct this all help would greatly be accomplished.
thanks
amulu
protected void CopyImage()
{
// DAImage da = new DAImage();
string stylenum = "";
int itemid = 0;
stylenum = "200899-01";
string filenum = "";
string newfilename = Guid.NewGuid().ToString () ;
int index = 0;
try
{
string[] files = System.IO.Directory.GetFiles(("\\\\Ccsrv2\\publicfiles\\All\\MARKETING\\Product Images\\Style Images\\August 2008 Market\\Test"));
//(@"C:\\Documents and Settings\\mduddebanda\\My Documents\\My Pictures\\DeleteFolder");
foreach (string file in files)
{
index = file.IndexOf(".");
filenum = file.Remove(index, 3);
index = file.Length;
filenum = file.Replace("\\", "");
index = filenum.Length;
filenum = filenum.Substring(index - 13, 9);
//filenum=file.Remove(index);
//Response.Write(filenum);
DataTable dtitem = DAImage.GetItembyStylenum(filenum);
foreach (DataRow dr in dtitem.Rows)
{
stylenum = (dr[0].ToString());
itemid = Convert.ToInt16(dr[1].ToString());
}
if (filenum == stylenum) // get the stylenum in the required format from the file here
{
//Get the image id for the image to be inserted into the table.
DataTable dt = DAImage.GetImageId();
string result = "";
foreach (DataRow dr in dt.Rows)
{
result = dr[0].ToString();
}
// System.IO.Directory.CreateDirectory(@"C:\\Documents and Settings\\mduddebanda\\My Documents\\My Pictures\\DeleteFolder\\tbl" + itemid);
//System.IO.File.Copy(file, @"\\Pdserver.tsiag.com\d$\Library\tbl" + itemid + "\" + filenum + ".jpg");
System.IO.Directory.CreateDirectory(@"\\Pdserver\\d:$\\devlibrary\\tblItem" + itemid);
// System.IO.File.Copy(file, @"C:\\Documents and Settings\\mduddebanda\\My Documents\\My Pictures\\DeleteFolder\\tbl" + itemid + "\\" + newfilename + ".jpg");
// System.IO.File.Copy(file, "\\\\Pdserver\\d$\\devlibrary\\tblItem" + itemid + "\\" + newfilename.ToString() + ".jpg");
System.IO.File.Copy(file, @"\\Pdserver.tsiag.com\\d:$\\Library\\tbl" + itemid + "\\" + filenum + ".jpg");
DAImage.InsertImage("\\tblItem" + itemid + "\\" + newfilename.ToString() + ".jpg", itemid);
System.IO.File.Delete(file);
}
//select * from tblImage where imageid= (select max(imageid) from tblImage )
//Insert into the tblImage for that imageid and for that category.
//then delete this file from The j:/ directory.
}
}
catch (Exception ex)
{
throw ex;
}
}
Status:
Open Aug 04, 2008 - 10:24 AM
web development, information technology, .net
4answers
Answers
Aug 04, 2008 - 01:29 PM
Hi,
if you use "@" in front of a string every character inteh string is interpreted as a character and not as a control character. I.e. if you would do a "Console.WriteLine("Text1 \n\r Text2");" You would see
"Text1
Text2"
If you add a "@" in front of it so that it reads "Console.WriteLine(@"Text1 \n\r Text2");" You would see on your console window:
"Text1 \n\r Text2" because "\n\r" is not interpreted as a control character but as part of the string.
Here is a console app I wrote which should calrify things. I also show you another way to handle the filename and split it etc. by using a fileinfo object. Let me know if it helped you
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Version 1:");
//string[] files = System.IO.Directory.GetFiles(@"C:\temp");
string[] files = System.IO.Directory.GetFiles(@"\\192.168.1.4\temp");
foreach (string file in files)
{
Console.WriteLine(file);
}
Console.WriteLine("\n\rVersion 2:");
//string[] files = System.IO.Directory.GetFiles(@"C:\temp");
foreach (string file in files)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
Console.WriteLine(fileInfo.Name.Replace(fileInfo.Extension,String.Empty));
}
Console.WriteLine("Press Enter to exit...");
Console.Read();
}
}
Cheers
Peter
Aug 04, 2008 - 01:48 PM
thanks very much for your response in detail. I now get an exception , I granted all permissions for my machine on the server for this particular folder(so that my machine has access to only this folder). I still have this problem. All suggestion would be greatly appreciated.
Thanks
Service cannot be started. System.UnauthorizedAccessException: Access to the path '\\Pdserver.tsiag.com\D$\devlibrary\images\tblItem338' is denied.
at MyfirstService.Service1.CopyImage() in C:\Documents and Settings\mduddebanda\My Documents\Visual Studio 2005\Projects\MyfirstService\MyfirstService\Service1.cs:line 110
at MyfirstService.Service1.OnStart(String[] args) in C:\Documents and Settings\mduddebanda\My Documents\Visual Studio 2005\Projects\MyfirstService\MyfirstService\Service1.cs:line 21
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
Aug 05, 2008 - 05:30 PM
SOrry but I guess it is exactly what it says: "System.UnauthorizedAccessException: Access to the path '\\Pdserver.tsiag.com\D$\devlibrary\images\tblItem338' is denied. " You don't have access to the share. Can you try the IP address? Maybe the machine can't resolve your pdserver.tsiag.com? Under which user do you run your windows service? Does THIS user has access? If it is the local system user then the local system user needs access etc. Go to Administrative Tools, select your service and check under what user it runs!
Cheers
Peter
Mar 24, 2009 - 06:49 AM
The question looks to be abandoned by the user who asked it. If no action is taken within 2 days, a Quomon Moderator will consider closing the question and distributing the points.
The Quomon Team
Answer this question
Share Your Own Experience & Expertise
We look to ensure that every question is answered by the best people with relevant expertise and experience, the best answers include multiple perspectives. Do you have relevant expertise or experience to contribute your answer to any of these commonly asked questions?
Add New Comment