小编Bre*_*ett的帖子

使用File.Create()后由另一个进程使用的文件

我正在尝试检测运行时是否存在文件,如果没有,则创建它.但是当我尝试写入时,我收到此错误:

该进程无法访问文件'myfile.ext',因为它正由另一个进程使用.

string filePath = string.Format(@"{0}\M{1}.dat", ConfigurationManager.AppSettings["DirectoryPath"], costCentre); 
if (!File.Exists(filePath)) 
{ 
    File.Create(filePath); 
} 

using (StreamWriter sw = File.AppendText(filePath)) 
{ 
    //write my text 
}
Run Code Online (Sandbox Code Playgroud)

关于如何修复它的任何想法?

c# file-io

110
推荐指数
6
解决办法
17万
查看次数

List <T>的动态类型?

我有一个方法返回DataSet表的List

public static List<string> GetListFromDataTable(DataSet dataSet, string tableName, string rowName)
    {
        int count = dataSet.Tables[tableName].Rows.Count;
        List<string> values = new List<string>();

        // Loop through the table and row and add them into the array
        for (int i = 0; i < count; i++)
        {
            values.Add(dataSet.Tables[tableName].Rows[i][rowName].ToString());
        }
        return values;
    }
Run Code Online (Sandbox Code Playgroud)

有没有办法可以动态设置列表的数据类型,并且这个方法适合所有数据类型,所以我可以在调用此方法时指定它应该是List<int> List<string>或者List<AnythingILike>

另外,在声明方法时返回类型是什么?

先谢谢,布雷特

c# list dynamic

7
推荐指数
1
解决办法
3176
查看次数

如果出现T的变化,则从List <T>中删除所有项目

现在已经挣扎了一段时间.

我在WebAPI世界中蘸着我的脚趾,我有一个List,可以包含同名但价格不同的产品.我需要做的是删除所有对产品的引用是出现价格变化.

例如.
name ="Cornflakes"价格="1.99M"
name ="Cornflakes" 价格="1.89M"
name ="Rice Krispies"价格="2.09M"
name ="Cornflakes"价格="2.09M"

我的最终名单中不应出现任何玉米片.

我已经写了大量的,但它很快就删除了产品,我不确定我应该删除它们.

public IEnumerable<Product> GetProductsByCategory(int Id)
    {
        List<Product> sourceProductList = products.Where(p => p.CategoryID == Id).ToList();
        List<Product> tempProducts = new List<Product>();
        List<Product> targetProductList = new List<Product>();

        foreach (var product in sourceProductList)
        {
            bool isInTempList = tempProducts.Any(x => x.Name == product.Name);
            if (!isInTempList)
            {
                tempProducts.Add(product);
            }
            else
            {
                Product tempProduct = product;
                bool isPriceDifferent = tempProducts.Where(y => y.Name == tempProduct.Name).Any(y => y.Price != tempProduct.Price);
                if …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc lambda asp.net-web-api

5
推荐指数
2
解决办法
248
查看次数

生成后打印图像[c#]

我有以下方法加载空白模板图像,在其上绘制相关信息并将其保存到另一个文件.我想稍微改变一下以实现以下目标:

  • 加载模板图像
  • 在上面绘制相关信息
  • 打印出来

我不想保存它,只需打印出来.这是我现有的方法:

public static void GenerateCard(string recipient, string nominee, string reason, out string filename)
    {
        // Get a bitmap.
        Bitmap bmp1 = new Bitmap("template.jpg");

        Graphics graphicImage;

        // Wrapped in a using statement to automatically take care of IDisposable and cleanup
        using (graphicImage = Graphics.FromImage(bmp1))
        {
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            Encoder myEncoder = Encoder.Quality;

            graphicImage.DrawString(recipient, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(480, 33)); …
Run Code Online (Sandbox Code Playgroud)

c# printing image file

4
推荐指数
1
解决办法
6634
查看次数

标签 统计

c# ×4

asp.net ×1

asp.net-mvc ×1

asp.net-web-api ×1

dynamic ×1

file ×1

file-io ×1

image ×1

lambda ×1

list ×1

printing ×1