none
Filestream Access Denied

    Question

  • Hi All,

    I am having issues with my code. it is saying Access to the path is denied but i do not know how to add an account that has permissions to the folder in my code?

     Try
                'Set up as task scheduler on 
    
                'Dim DateRange As DateTime = DateTime.Now.AddDays(-1)
                ''Get all rows added in last 24 hours
                'Dim NewRows = (From R In HSContext.Inspections
                '               Where R.SyncDateTime >= DateRange
                '              Select R).ToList
                Dim NewRows = (From R In HSContext.Inspections
                   Where R.WisdomImport = "CT"
                  Select R).ToList
    
                'Create report for each individual inspection.
                For Each Row In NewRows
    
                    Dim Format As String = "PDF"
                    Dim Param As New ReportParameterCollection
                    Dim Param1 As New ReportParameter
    
                    Param1 = New ReportParameter("InspectionID", Row.InspectionId.ToString)
                    Param.Add(Param1)
    
                    Dim myreportviewer As New ReportViewer
                    Dim myserverreport As New ServerReport
    
                    myreportviewer.ProcessingMode = ProcessingMode.Remote
                    myserverreport = myreportviewer.ServerReport
                    myserverreport.ReportServerUrl = New Uri("http://cheryl/ReportServer")
                    myserverreport.ReportPath = "/Health and Safety Site Inspection/Auto Report"
    
                    myserverreport.SetParameters(Param)
    
                    Dim Result As Byte()
                    Result = myserverreport.Render("PDF", Nothing, Nothing, Nothing, ".pdf", Nothing, Nothing)
                    Dim Filename As String = ""
    
                    Dim Number As String = Replace(Replace(Replace(Row.Add1.ToString, "-", ""), " ", ""), "&", "_")
                    Dim rnumber As Integer
                    Dim resultN As Boolean = Int32.TryParse(Number, rnumber)
    
                    If resultN = False Then
                        Filename = "\\cyhfile01\shared\WPS\WPS_Shared\HS_Daily_Site_Inspections\App_Inspections\" + Row.PrSeqNo.ToString + "_" + Replace(Replace(Replace(Row.Add1.ToString, " ", ""), ",", ""), "&", "_") + "_" + CDate(Row.InspectDate).ToString("ddMMyyyy") + ".pdf"
                    ElseIf resultN = True Then
                        Filename = "\\cyhfile01\shared\WPS\WPS_Shared\HS_Daily_Site_Inspections\App_Inspections\" + Row.PrSeqNo.ToString + "_" + Replace(Replace(Replace(Row.Add1.ToString, " ", ""), ",", ""), "&", "_") + Replace(Replace(Replace(Row.Add2.ToString, " ", ""), ",", ""), "&", "_") + "_" + CDate(Row.InspectDate).ToString("ddMMyyyy") + ".pdf"
                    End If
    
                    Using fs As FileStream = New FileStream(Filename, FileMode.Create, FileAccess.Write)
                        fs.Write(Result, 0, Result.Length)
                    End Using
    
                    Row.WisdomImport = "CT-Complete"
                    HSContext.SaveChanges()
    
                Next
    
                Close()
    
            Catch ex As Exception
    
            End Try
        End Sub
    End Class

    Thank you in advance,

    Darren

    Monday, March 27, 2017 3:05 PM

All replies

  • Hi

    Have a look at:

            ' append or  overwrite 
            Dim app As Boolean = True
    
            Using fs As StreamWriter = New StreamWriter(Filename, app)
                fs.Write(Result, 0, Result.Length)
                ' or possibly fs.WriteLine(Result, 0, Result.Length)
                ' if you want each on a new line
            End Using
    


    Regards Les, Livingston, Scotland

    Monday, March 27, 2017 3:14 PM
  • Darren,

    Let's start with this:

    Remove (or comment out) the Try, Catch and End Try. Right now you're masking any exception (swallowing the exception) and that's not doing you any good here. Let it fail if it's going to - until you find and attend to the underlying cause.

    Is there a reason you're using a FileStream rather than IO.File.WriteAllBytes? It doesn't matter but as Les suggested, if the file exists then that's the likely cause.

    Not that you asked, but some of the coding needs to be done differently. The "Replace" that you're using is old (leftover from VB6). You can use the string class's version instead but as many as I see, you're better to set it up in a StringBuilder and use those methods. A StringBuilder is mutable where a string isn't.

    Food for thought. Start with letting it fail and let's see what the exception is.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, March 27, 2017 4:13 PM
  • Hi Les,

    Thank you for getting back to me! it still came up with the same error! but i have just found the issue! the person who wrote it was using his account to run it and he has left so his account no longer works!!! always the silly things

    Thanks Darren

    Tuesday, March 28, 2017 8:11 AM
  • Hi Charles,

    Thanks for your reply i found the issue, the person who wrote this used his account to run it, as he has left his account is no longer active hence it was failing! i should have checked this first but i didn't think to!!

    Thanks,

    Darren

    • Proposed as answer by Frank L. Smith Tuesday, March 28, 2017 4:39 PM
    Tuesday, March 28, 2017 8:12 AM
  • Hi Charles,

    Thanks for your reply i found the issue, the person who wrote this used his account to run it, as he has left his account is no longer active hence it was failing! i should have checked this first but i didn't think to!!

    Thanks,

    Darren

    My name is Frank, but I understand why you thought it to be otherwise.

    ;-)

    *****

    I'd still suggest that you look at the code and I think you'll find ways to improve performance if nothing else.

    Also though, don't leave the catch of your exception handling open like it currently is. That will come back to bite you at some point.

    I'm glad you have it all worked out though. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, March 28, 2017 4:39 PM