首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建文本文件并写入该文件

创建文本文件并写入该文件
EN

Stack Overflow用户
提问于 2012-05-05 12:48:20
回答 2查看 22.3K关注 0票数 4

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

错误消息:

代码语言:javascript
运行
复制
    UnauthorizedAccessExcepion was unhandled by user code
 Access to the path 'c:\save.txt' is denied.

我的代码:

代码语言:javascript
运行
复制
 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
EN

Stack Overflow用户

发布于 2012-05-05 23:00:20

如果您有点着迷,并且想要直接写入C驱动器目录,您可以使用以下命令:

代码语言:javascript
运行
复制
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
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10458942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档