How do I write the error faced into the log.txt?
Firstly, I get error file name from web config as follows:
string Errorlog = System.Configuration.ConfigurationManager.AppSettings["Errorlog.txt"];
Next, I try to get the full path of the text file; but i do not know which one will pull it out.
This is a few. I want to get the full path by not making it static.
//string path = Global.getLogFilePath();
//string path = Path.GetFileName(directoryFullPath);
//string path = openFileDialog.FileName;
//string path = Path.Combine(System.Environment.CurrentDirectory);
//string path = Path.GetFullPath("Errorlog.txt");
//string path = Directory.GetCurrentDirectory();
//string path = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName());
//string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
//string path = Environment.CurrentDirectory.ToString();
//string path = System.Environment.GetEnvironmentVariable("TEMP");
//if (!path.EndsWith("\\")) path += "\\";
Put log.txt in a Logs sub-folder of your web site and then you can get the absolute path like this:
string logFile = HttpContext.Current.Server.MapPath("~/Logs/log.txt");
Also don't forget to restrict the public access to the Logs folder to avoid anyone reading your application log files.
Related
I've tried this:
var systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var complete = Path.Combine(systemPath, extractfilename);
But it results in:
C:\ProgramData\Extract.txt
My expected output is:
C:\User\AppData\Extract.txt
You need to create file in Environment.SpecialFolder.ApplicationData folder.
There is another way also to get its value, so you can use it also and append the path.
e.g.
string path;
path = #"%AppData%\test";
Environment.ExpandEnvironmentVariables(path);
C:\User\AppData\Local
Use Environment.SpecialFolder.LocalApplicationData:
The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.
C:\User\AppData\Roaming
Use Environment.SpecialFolder.ApplicationData:
The directory that serves as a common repository for application-specific data for the current roaming user. A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.
This will create a folder named "MyName" in "%appdata%".
string directoryName = "MyName";
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string mainPath = Path.Combine(appDataPath, directoryName);
Directory.CreateDirectory(mainPath);
This will create a file in "%appdata%" called "MyFile.txt" which says "Hello World".
string text = "Hello Word";
string fileName = "MyFile.txt";
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string mainPath = Path.Combine(appDataPath, fileName);
File.WriteAllText(mainPath, text);
I have created 2 folders in my project named TempFile and TempFile\Sample. Here is the folder structure
How can I get the path of this folder and the file SampleExcel.xlsx (project\TempFile and project\TempFile\Sample\SampleExcel.xlsx resp) using c#. Also once I publish it to Azure will I need to change it?
Here is what I have tried:
public void Run([QueueTrigger("my-queuename", Connection = "")] string myQueueItem, ILogger log)
{
//Method 1
var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
//Method 2
var path = Environment.CurrentDirectory;
//Method 3
var filePath = Path.GetFullPath(#"TempFile\Sample" + "\\SampleExcel.xlsx");
}
The problem with these methods is that they return the path project\bin\Debug\netcoreapp3.1
How can I get the required path?
Any suggestions?
Use Environment.CurrentDirectory as:
filePath1 = Environment.CurrentDirectory +"\\TempFile\\" + tempFile + ".xlsx";
filePath2 = Environment.CurrentDirectory +"\\TempFile\\Sample\\SampleExcel.xlsx";
I'm Creating a web application using Visual Studio. in that I want to save pdf that are Uploaded, to a Specific Path. So I Used Following Code
String fileName = BalanceSheet.FileName;
String fName = "pdf / Accounts / BalanceSheet/ ";
String fname = Filename.Text;
String location = AppDomain.CurrentDomain.BaseDirectory + "/pdf/Accounts/BalanceSheet/";
String filePath = System.IO.Path.Combine(location,fileName);
BalanceSheet.SaveAs(MapPath(fName+fname));
here BalanceSheet is a FileUpload Control. when the Following Code get executed,BalanceSheet.SaveAs(MapPath(fName+fname)); it'Show an Exception Saying couldn't find Some parts of the Path.when I use BalanceSheet.SaveAs(MapPath(filePath));, it Shows an Exception Saying Expecting Virtual Path.
Control
try the below code:
BalanceSheet.SaveAs(Path.Combine(Server.MapPtah("~/"), fName + fname);
My application is running on the winCE platform and needs to search the file when it initialized, but it seems winCE is very different from windows.
Step1. Get the current directory
So first, I am using a code to get a directory:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + "\\";
The path is "\Program Files\SmartDeviceProject1\".
But when I use console.write it shows "\Program Files\SmartDeviceProject1\". So it missed "\"
Step2. Trying to get all the file name in the path
FileInfo[] fileinfo = dir.GetFiles();
Step3. Use a loop and IF to identify the file exist or not.
Here is some code.
string filename = "bridgetool";
// string path = Directory.GetCurrentDirectory(); // This is not working in winCE
// Get the current path
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + "\\";
Console.Write(path);
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] fileinfo = dir.GetFiles();
foreach (FileInfo file in fileinfo)
{
if (filename == file.ToString())
{
return true;
}
}
Console.Write("Cannot find");
return false;
I got the right current path but the problem is that the path I get and path I printed out is different. I am not sure if it is a problem.
Is there any way to search the file name in the specific path?
I want create directory (if not exist) in path AppData/Roaming/test. But my code doesn't work, I dont know why. Can you help me?
string path;
path = #"%AppData%\Roaming\test\";
path = Environment.ExpandEnvironmentVariables(path);
Console.WriteLine(path);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
This code doen't create dir.
%AppData% is a SpecialFolder.
change your code from:
path = #"%AppData%\Roaming\test\";
to:
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var path = Path.Combine(appDataPath, #"test\");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
You should really use Environment.SpecialFolders to reach special folders rather than explicitly hard-coding a path.
Something like this would do the trick:
string path = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "test");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}