Vb.net 创建一个文本文件并写入其中

Vb.net 创建一个文本文件并写入其中,vb.net,Vb.net,我想创建一个文本文件并将一些文本写入该文件,但我的代码无法创建该文本文件 错误消息: UnauthorizedAccessExcepion was unhandled by user code Access to the path 'c:\save.txt' is denied. 我的代码: Dim fileLoc As String = "c:\save.txt" Dim fs As FileStream = Nothing

我想创建一个文本文件并将一些文本写入该文件,但我的代码无法创建该文本文件

错误消息:

    UnauthorizedAccessExcepion was unhandled by user code
 Access to the path 'c:\save.txt' is denied.
我的代码:

 Dim fileLoc As String = "c:\save.txt"
                    Dim fs As FileStream = Nothing
                    If (Not File.Exists(fileLoc)) Then
                        fs = File.Create(fileLoc)
                        Using fs

                        End Using
                    End If
                    If File.Exists(fileLoc) Then
                        Using sw As StreamWriter = New StreamWriter(fileLoc)
                            a = "Test: " + TextBox1.Text
                            c = "=============================================="
                            sw.Write(a)
                            sw.Write(c)
                        End Using
                    End If

在较新版本的Windows中,C:驱动器的根目录默认为只读。尝试将文件放入另一个文件夹

如果您有点痴迷,想直接写入C驱动器目录,可以使用以下方法:

Imports System.Security.Principal

Module VistaSecurity

    'Declare API
    Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    Private Const BCM_FIRST As Int32 = &H1600
    Private Const BCM_SETSHIELD As Int32 = (BCM_FIRST + &HC)

    Public Function IsVistaOrHigher() As Boolean
        Return Environment.OSVersion.Version.Major < 6
    End Function

    ' Checks if the process is elevated
    Public Function IsAdmin() As Boolean
        Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim p As WindowsPrincipal = New WindowsPrincipal(id)
        Return p.IsInRole(WindowsBuiltInRole.Administrator)
    End Function

    ' Add a shield icon to a button
    Public Sub AddShieldToButton(ByRef b As Button)
        b.FlatStyle = FlatStyle.System
        SendMessage(b.Handle, BCM_SETSHIELD, 0, &HFFFFFFFF)
    End Sub

    ' Restart the current process with administrator credentials
    Public Sub RestartElevated()
        Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
        startInfo.UseShellExecute = True
        startInfo.WorkingDirectory = Environment.CurrentDirectory
        startInfo.FileName = Application.ExecutablePath
        startInfo.Verb = "runas"
        Try
            Dim p As Process = Process.Start(startInfo)
        Catch ex As Exception
            Return 'If cancelled, do nothing
        End Try
        Application.Exit()
    End Sub

End Module
导入System.Security.Principal
模块VistaSecurity
'声明API
私有声明Ansi函数SendMessage Lib“user32.dll”别名“SendMessageA”(ByVal hwnd为整数,ByVal wMsg为整数,ByVal wParam为整数,ByVal lParam为字符串)为整数
私有常量BCM_首先为Int32=&H1600
私有常量BCM_设置屏蔽为Int32=(BCM_FIRST+&HC)
公共函数IsVistaOrHigher()作为布尔值
返回Environment.OSVersion.Version.Major<6
端函数
'检查进程是否提升
公共函数IsAdmin()为布尔值
Dim id为WindowsIdentity=WindowsIdentity.GetCurrent()
作为WindowsPrincipal的Dim p=新的WindowsPrincipal(id)
返回p.IsInRole(WindowsBuiltInRole.Administrator)
端函数
'将屏蔽图标添加到按钮
公共子添加屏蔽按钮(ByRef b As按钮)
b、 FlatStyle=FlatStyle.System
发送消息(b.手柄、BCM_设置屏蔽、0和HFFFF)
端接头
'使用管理员凭据重新启动当前进程
公共分店
Dim startInfo As PROCESSTARTINFO=新的PROCESSTARTINFO()
startInfo.UseShellExecute=True
startInfo.WorkingDirectory=Environment.CurrentDirectory
startInfo.FileName=Application.ExecutablePath
startInfo.Verb=“runas”
尝试
Dim p As进程=进程启动(startInfo)
特例
Return“如果取消,则不执行任何操作”
结束尝试
Application.Exit()
端接头
端模块

it work!:]另外,如何在我的文档文件中创建txt文件???
SaveFileDialog1.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim fileLoc As String=SaveFileDialog1.InitialDirectory+“\save.txt”?@Kwan Dim fileLoc As String=Environment.GetFolder Path(Environment.specialfolder.MyDocuments)和“\save.txt”