在 C#、Visual Studio 2010 中保存到文件,错误

发布于 2024-10-03 22:38:51 字数 8315 浏览 1 评论 0原文

大家好,

我的程序有一个小错误,当我尝试保存到文件时,会出现错误,提示“客户端不持有所需的权限”。我不知道如何解决这个问题,因为我正在我的笔记本电脑上运行它,只有我使用,除非我正确设置了管理员状态,否则我不知道发生了什么。

我在下面发布了我的代码只是为了确定

干杯。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
namespace WindowsFormsApplication1
{


    public partial class Form1 : Form
    {
        delegate void addlistitemcallback(string value);
        public static string inputdata;
        public static int MaximumSpeed, maximumRiderInput, RiderInput, Time, CurrentSpeed, DistanceTravelled, MaximumMotorOutput, MotorOutput, InputSpeed;
        public static string SaveDataString;
        public Thread Serial;
        public static SerialPort SerialData;
        public static string[] portlist = SerialPort.GetPortNames();
        public static string[] SaveData = new string[4];
        public static string directory = "C:\\";

        public Form1()
        {
            Serial = new Thread(ReadData);
            InitializeComponent();
            int Count = 0;
            for (Count = 0; Count < portlist.Length; Count++)
            {
                ComPortCombo.Items.Add(portlist[Count]);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void StartDataButton_Click(object sender, EventArgs e)
        {
            SerialData = new SerialPort(ComPortCombo.Text, 19200, Parity.None, 8, StopBits.One);
            SerialData.Open();
            SerialData.WriteLine("P");
            Serial.Start();
            StartDataButton.Enabled = false;
            EndDataButton.Enabled = true;
            ComPortCombo.Enabled = false;
            CurrentSpeed = 0;
            MaximumSpeed = 0;
            Time = 0;
            DistanceTravelled = 0;
            MotorOutput = 0;
            RiderInput = 0;
            SaveData[0] = "";
            SaveData[1] = "";
            SaveData[2] = "";
            SaveData[3] = "";
            SaveDataButton.Enabled = false;
            if (SerialData.IsOpen)
            {
                ComPortStatusLabel.Text = "OPEN";
                SerialData.NewLine = "/n";
                SerialData.WriteLine("0");
                SerialData.WriteLine("/n");
            }
        }
        private void EndDataButton_Click(object sender, EventArgs e)
        {
            SerialData.Close();
            SaveDataButton.Enabled = true;
            //SerialData.WriteLine("1");
            //SerialData.WriteLine("0");
            if (!SerialData.IsOpen)
            {
                ComPortStatusLabel.Text = "CLOSED";
            }
            int i = 0;
            for (i = 0; i < 4; i++)
            {
                if (i == 0)
                {
                    SaveDataString = "MaximumSpeed during the Ride was = " + Convert.ToString(MaximumSpeed) + "m/h";
                    SaveData[i] = SaveDataString;
                }
                if (i == 1)
                {
                    SaveDataString = "Total Distance Travelled = " + Convert.ToString(DistanceTravelled) + "m";
                    SaveData[i] = SaveDataString;
                }
                if (i == 2)
                {
                    SaveDataString = "Maximum Rider Input Power = " + Convert.ToString(maximumRiderInput) + "Watts";
                    SaveData[i] = SaveDataString;
                }
                if (i == 3)
                {
                    SaveDataString = "Maximum Motor Output Power = " + Convert.ToString(MaximumMotorOutput) + "Watts";
                    SaveData[i] = SaveDataString;
                }
            }

        }

        private void SaveDataButton_Click(object sender, EventArgs e)
        {
            //File.WriteAllBytes(directory + "image" + imageNO + ".txt", ); //saves the file to Disk    
            File.WriteAllLines("C:\\" + "BikeData.txt", SaveData);
        }

        public void updateSpeedtextbox(string value)
        {
            if (SpeedTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updateSpeedtextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                SpeedTextBox.Text = value;
            }


        }
        public void updatePowertextbox(string value)
        {
            if (RiderInputTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updatePowertextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                RiderInputTextBox.Text = value;
            }


        }
        public void updateDistancetextbox(string value)
        {
            if (DistanceTravelledTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updateDistancetextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                DistanceTravelledTextBox.Text = value;
            }


        }
        public void updateMotortextbox(string value)
        {
            if (MotorOutputTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updateMotortextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                MotorOutputTextBox.Text = value;
            }


        }
        public void ReadData()
        {
            int counter = 0;

            while (SerialData.IsOpen)
            {
                if (counter == 0)
                {
                    try
                    {
                        InputSpeed = Convert.ToInt16(SerialData.ReadChar());

                        if (CurrentSpeed > MaximumSpeed)
                        {
                            MaximumSpeed = CurrentSpeed;
                        }
                        updateSpeedtextbox("Current Wheel Speed = " + Convert.ToString(InputSpeed) + "Km/h");
                        DistanceTravelled = DistanceTravelled + (Convert.ToInt16(InputSpeed) * Time);
                        updateDistancetextbox("Total Distance Travelled = " + Convert.ToString(DistanceTravelled) + "Km");
                    }
                    catch (Exception) { }
                }
                if (counter == 1)
                {
                    try
                    {
                        RiderInput = Convert.ToInt16(SerialData.ReadChar());
                        if (RiderInput > maximumRiderInput)
                        {
                            maximumRiderInput = RiderInput;
                        }
                        updatePowertextbox("Current Rider Input Power =" + Convert.ToString(RiderInput) + "Watts");
                    }
                    catch (Exception) { }
                }
                if (counter == 2)
                {
                    try
                    {
                        MotorOutput = Convert.ToInt16(SerialData.ReadChar());
                        if (MotorOutput > MaximumMotorOutput)
                        {
                            MaximumMotorOutput = MotorOutput;
                        }

                        updateMotortextbox("Current Motor Output = " + Convert.ToString(MotorOutput) + "Watts");
                    }
                    catch (Exception) { }
                }
                counter++;
                if (counter == 3)
                {
                    counter = 0;
                }
            }
        }      


        private void Form1_Closed(object sender, EventArgs e)
        {
            if (SerialData.IsOpen)
            {
                SerialData.Close();
            }
        }

        private void ComPortCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartDataButton.Enabled = true;
        }

        private void DistanceTravelledTextBox_TextChanged(object sender, EventArgs e)
        {

        }





    }
}

g'day guys,

i have a small error with my program where when i try to save to file an error occurs which says "A required privilege is not held by the client." I not sure how to fix this as i am running it off of my laptop which only i use and unless i have set up administrator status correctly i dont know what is going on.

I posted my code below just to be sure

Cheers.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
namespace WindowsFormsApplication1
{


    public partial class Form1 : Form
    {
        delegate void addlistitemcallback(string value);
        public static string inputdata;
        public static int MaximumSpeed, maximumRiderInput, RiderInput, Time, CurrentSpeed, DistanceTravelled, MaximumMotorOutput, MotorOutput, InputSpeed;
        public static string SaveDataString;
        public Thread Serial;
        public static SerialPort SerialData;
        public static string[] portlist = SerialPort.GetPortNames();
        public static string[] SaveData = new string[4];
        public static string directory = "C:\\";

        public Form1()
        {
            Serial = new Thread(ReadData);
            InitializeComponent();
            int Count = 0;
            for (Count = 0; Count < portlist.Length; Count++)
            {
                ComPortCombo.Items.Add(portlist[Count]);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void StartDataButton_Click(object sender, EventArgs e)
        {
            SerialData = new SerialPort(ComPortCombo.Text, 19200, Parity.None, 8, StopBits.One);
            SerialData.Open();
            SerialData.WriteLine("P");
            Serial.Start();
            StartDataButton.Enabled = false;
            EndDataButton.Enabled = true;
            ComPortCombo.Enabled = false;
            CurrentSpeed = 0;
            MaximumSpeed = 0;
            Time = 0;
            DistanceTravelled = 0;
            MotorOutput = 0;
            RiderInput = 0;
            SaveData[0] = "";
            SaveData[1] = "";
            SaveData[2] = "";
            SaveData[3] = "";
            SaveDataButton.Enabled = false;
            if (SerialData.IsOpen)
            {
                ComPortStatusLabel.Text = "OPEN";
                SerialData.NewLine = "/n";
                SerialData.WriteLine("0");
                SerialData.WriteLine("/n");
            }
        }
        private void EndDataButton_Click(object sender, EventArgs e)
        {
            SerialData.Close();
            SaveDataButton.Enabled = true;
            //SerialData.WriteLine("1");
            //SerialData.WriteLine("0");
            if (!SerialData.IsOpen)
            {
                ComPortStatusLabel.Text = "CLOSED";
            }
            int i = 0;
            for (i = 0; i < 4; i++)
            {
                if (i == 0)
                {
                    SaveDataString = "MaximumSpeed during the Ride was = " + Convert.ToString(MaximumSpeed) + "m/h";
                    SaveData[i] = SaveDataString;
                }
                if (i == 1)
                {
                    SaveDataString = "Total Distance Travelled = " + Convert.ToString(DistanceTravelled) + "m";
                    SaveData[i] = SaveDataString;
                }
                if (i == 2)
                {
                    SaveDataString = "Maximum Rider Input Power = " + Convert.ToString(maximumRiderInput) + "Watts";
                    SaveData[i] = SaveDataString;
                }
                if (i == 3)
                {
                    SaveDataString = "Maximum Motor Output Power = " + Convert.ToString(MaximumMotorOutput) + "Watts";
                    SaveData[i] = SaveDataString;
                }
            }

        }

        private void SaveDataButton_Click(object sender, EventArgs e)
        {
            //File.WriteAllBytes(directory + "image" + imageNO + ".txt", ); //saves the file to Disk    
            File.WriteAllLines("C:\\" + "BikeData.txt", SaveData);
        }

        public void updateSpeedtextbox(string value)
        {
            if (SpeedTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updateSpeedtextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                SpeedTextBox.Text = value;
            }


        }
        public void updatePowertextbox(string value)
        {
            if (RiderInputTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updatePowertextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                RiderInputTextBox.Text = value;
            }


        }
        public void updateDistancetextbox(string value)
        {
            if (DistanceTravelledTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updateDistancetextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                DistanceTravelledTextBox.Text = value;
            }


        }
        public void updateMotortextbox(string value)
        {
            if (MotorOutputTextBox.InvokeRequired)
            {
                addlistitemcallback d = new addlistitemcallback(updateMotortextbox);
                Invoke(d, new object[] { value });
            }
            else
            {
                MotorOutputTextBox.Text = value;
            }


        }
        public void ReadData()
        {
            int counter = 0;

            while (SerialData.IsOpen)
            {
                if (counter == 0)
                {
                    try
                    {
                        InputSpeed = Convert.ToInt16(SerialData.ReadChar());

                        if (CurrentSpeed > MaximumSpeed)
                        {
                            MaximumSpeed = CurrentSpeed;
                        }
                        updateSpeedtextbox("Current Wheel Speed = " + Convert.ToString(InputSpeed) + "Km/h");
                        DistanceTravelled = DistanceTravelled + (Convert.ToInt16(InputSpeed) * Time);
                        updateDistancetextbox("Total Distance Travelled = " + Convert.ToString(DistanceTravelled) + "Km");
                    }
                    catch (Exception) { }
                }
                if (counter == 1)
                {
                    try
                    {
                        RiderInput = Convert.ToInt16(SerialData.ReadChar());
                        if (RiderInput > maximumRiderInput)
                        {
                            maximumRiderInput = RiderInput;
                        }
                        updatePowertextbox("Current Rider Input Power =" + Convert.ToString(RiderInput) + "Watts");
                    }
                    catch (Exception) { }
                }
                if (counter == 2)
                {
                    try
                    {
                        MotorOutput = Convert.ToInt16(SerialData.ReadChar());
                        if (MotorOutput > MaximumMotorOutput)
                        {
                            MaximumMotorOutput = MotorOutput;
                        }

                        updateMotortextbox("Current Motor Output = " + Convert.ToString(MotorOutput) + "Watts");
                    }
                    catch (Exception) { }
                }
                counter++;
                if (counter == 3)
                {
                    counter = 0;
                }
            }
        }      


        private void Form1_Closed(object sender, EventArgs e)
        {
            if (SerialData.IsOpen)
            {
                SerialData.Close();
            }
        }

        private void ComPortCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartDataButton.Enabled = true;
        }

        private void DistanceTravelledTextBox_TextChanged(object sender, EventArgs e)
        {

        }





    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2024-10-10 22:38:51

您可能没有对 C:\ 的写入权限。尝试将保存路径更改为“C:\Users\{YouName}\Documents\BikeData.txt”。

或者通过右键单击 Visual Studio 图标并选择“以管理员身份运行”,以管理权限启动 Visual Studio

You probably don't have write access to C:\. Try changing the save path to "C:\Users\{YouName}\Documents\BikeData.txt" instead.

Or start Visual Studio with administrative privileges by right clicking on its icon and choosing "Run as Administrator"

勿忘初心 2024-10-10 22:38:51

File.WriteAllLines("C:\" + "BikeData.txt", SaveData);

File.WriteAllLine(string,string[]),当用户无权在特定目录或驱动器中写入时,通过“SecurityException”,因此您必须授予写入权限,请参阅此链接 File.WriteAllLines

File.WriteAllLines("C:\" + "BikeData.txt", SaveData);

File.WriteAllLine(string,string[]), through "SecurityException" when user does not have rights to write in a particular directrory or drive so you have to give write permission, refer this link File.WriteAllLines

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文