Não é possível criar nova dir com o arquivo usando recursos em vb.net

0
Dim sPath As String

    sPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))

    For i = 1 To 50
        Threading.Thread.Sleep(100)
        Application.DoEvents()

    Next

    If My.Computer.FileSystem.FileExists(sPath & "\Housing\Stored Information.xlsx") Then
        Dim APP As New Excel.Application
        workbook = APP.Workbooks.Open(sPath & "\Housing\Stored Information.xlsx")
        worksheet = workbook.Worksheets("Sheet1")
        APP.Visible = False
        MessageBox.Show("File Opened!" & Environment.NewLine & "Path: " & sPath & "\Housing\Stored Information.xlsx")
    Else
        My.Computer.FileSystem.WriteAllBytes(sPath & "\Housing\Stored Information.xlsx", My.Resources.StoredInformation, True)
        Dim APP As New Excel.Application
        workbook = APP.Workbooks.Open(sPath & "\Housing\Stored Information.xlsx")
        worksheet = workbook.Worksheets("Sheet1")
        APP.Visible = False
        MessageBox.Show("File Created!" & Environment.NewLine & "Path: " & sPath & "\Housing\Stored Information.xlsx")
    End If

Não está criando minha pasta "Housing" ou meu arquivo "Stored Information.xlsx" Alguém poderia, por favor, dar uma olhada e me dizer o que estou fazendo errado?

    
por Anthony Cox 08.11.2016 / 09:30

1 resposta

1

Eventualmente, acertou. A chave era usar CreateDirectory() para criar todo o caminho do diretório, incluindo o diretório Housing na parte inferior da hierarquia.

Dim sPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Housing")
Dim Fpath As String = sPath & "\Stored Information.xlsx"

IO.Directory.CreateDirectory(sPath) ' If location already exists it will not do anything

If My.Computer.FileSystem.FileExists(Fpath) = False Then
   My.Computer.FileSystem.WriteAllBytes(Fpath, My.Resources.StoredInformation, True) ' Don't want to append data (although that would not happen in this instance) so True is used for that.
End If

Dim APP As New Excel.Application
workbook = APP.Workbooks.Open(Fpath)
worksheet = workbook.Worksheets("Sheet1")
APP.Visible = False
MessageBox.Show("File Opened!" & Environment.NewLine & "Path: " & Fpath)
    
por 08.11.2016 / 18:20