shreyas adikumar

shreyas adikumar

  • NA
  • 85
  • 10.3k

Error while reading from server to text file in localdesktop

Oct 10 2019 5:50 AM
Hi everyone i am trying to read the file into text from server i am facing the following error.Before publishing in the sever my program is working fine in visual studio after publishing i am facing that error in program
 
This program refrence i am attaching
  1. string constr6 = ConfigurationSettings.AppSettings["MS"];  
  2. string physicalPath = @"D:\FlashdataLabel\text.txt";  
  3. File.WriteAllText(physicalPath, String.Empty);  
  4. FileStream stream = new FileStream(physicalPath, FileMode.Open, FileAccess.ReadWrite);  
  5. StreamWriter writer = new StreamWriter(stream);  
  6. SqlConnection con6 = new SqlConnection(constr6);  
  7. con6.ConnectionString = constr6;  
  8. SqlCommand com = new SqlCommand(" select PMax,VOC,ISC,VPM,IPM,PSV,AssignMSN,MSN_Mat,PSN from [LabelMSN2222]where PSN='" + txtpsn.Text + "' order by id desc", con6);  
  9. con6.Open();  
  10. SqlDataReader reader = com.ExecuteReader();  
  11. while (reader.Read())  
  12. {  
  13. writer.Write(reader["PMax"].ToString());  
  14. writer.Write(",");  
  15. writer.Write(reader["VOC"].ToString());  
  16. writer.Write(",");  
  17. writer.Write(reader["ISC"].ToString());  
  18. writer.Write(",");  
  19. writer.Write(reader["VPM"].ToString());  
  20. writer.Write(",");  
  21. writer.Write(reader["IPM"].ToString());  
  22. writer.Write(",");  
  23. writer.Write(reader["PSV"].ToString());  
  24. writer.Write(",");  
  25. writer.Write(reader["AssignMSN"].ToString());  
  26. writer.Write(",");  
  27. writer.Write(reader["MSN_Mat"].ToString());  
  28. writer.Write(",");  
  29. writer.Write(reader["PSN"].ToString());  
  30. //writer.WriteLine();  
  31. }  
  32. reader.Close();  
  33. writer.Close();  
  34. stream.Close();  
  35. con6.Close();  
  36. }  
Server Error in '/' Application.
Access to the path 'D:\FlashdataLabel\text.txt' is denied.
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access to the path 'D:\FlashdataLabel\text.txt' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
 
Source Error:
Line 151: string constr6 = ConfigurationSettings.AppSettings["MSdbconnection"];
Line 152: string physicalPath = @"D:\FlashdataLabel\text.txt";
Line 153: File.WriteAllText(physicalPath, String.Empty);
Line 154: FileStream stream = new FileStream(physicalPath, FileMode.Open, FileAccess.ReadWrite);
Line 155: StreamWriter writer = new StreamWriter(stream);
Source File: Line: 153
 
Stack Trace:
[UnauthorizedAccessException: Access to the path 'D:\FlashdataLabel\text.txt' is denied.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +417
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1217
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +144
System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) +123
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) +103
System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost) +81
UI_City_CityMaster.labelMsn() in c:\inetpub\wwwroot\PRISM\UI\Master\HCM\MSN1.aspx.cs:153
UI_City_CityMaster.LoadMsn() in c:\inetpub\wwwroot\PRISM\UI\Master\HCM\MSN1.aspx.cs:138
UI_City_CityMaster.txtpsn_TextChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\PRISM\UI\Master\HCM\MSN1.aspx.cs:212
System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +137
System.Web.UI.Page.RaiseChangedEvents() +156
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4593
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.328

Answers (3)